Skip to content

Commit

Permalink
(chocolatey#3194) Add command to purge cached queries
Browse files Browse the repository at this point in the history
The changes in this commit adds a new command to Chocolatey CLI
that allows the user to clear any cached queries that have been saved
on their system.
This will clear both system and user level caches when running as an
administrator, and user level caches when running in a non-elevated
context.

Additionally, the ability to only remove expired caches is added as well
as just listing how many items has been cached.
  • Loading branch information
AdmiringWorm committed Jun 19, 2023
1 parent d64de0e commit 0d0db60
Show file tree
Hide file tree
Showing 8 changed files with 752 additions and 255 deletions.
9 changes: 9 additions & 0 deletions src/chocolatey.tests/TinySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ public class FactAttribute : ObservationAttribute
{
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class InlineDataAttribute : TestCaseAttribute
{
public InlineDataAttribute(params object[] data)
: base(data)
{
}
}

public class ExplicitAttribute : NUnit.Framework.ExplicitAttribute
{
}
Expand Down
1 change: 1 addition & 0 deletions src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="infrastructure.app\attributes\CommandForAttributeSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyListCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyApiKeyCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyConfigCommandSpecs.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
namespace chocolatey.tests.infrastructure.app.commands
{
using chocolatey.infrastructure.app.attributes;
using System.Collections.Generic;
using chocolatey.infrastructure.app.commands;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.filesystem;
using Moq;
using System.Reflection;
using System.Linq;
using FluentAssertions;
using chocolatey.infrastructure.commandline;
using chocolatey.infrastructure.app.domain;
using FluentAssertions.Execution;

public class ChocolateyCacheCommandSpecs
{
[ConcernFor("cache")]
public abstract class ChocolateyCacheCommandSpecsBase : TinySpec
{
protected ChocolateyCacheCommand Command;
protected ChocolateyConfiguration Configuration = new ChocolateyConfiguration();
protected Mock<IFileSystem> FileSystem = new Mock<IFileSystem>();

public override void Context()
{
Configuration.CommandName = "cache";
Command = new ChocolateyCacheCommand(FileSystem.Object);
}
}

public class WhenImplementingCommandFor : ChocolateyCacheCommandSpecsBase
{
private List<CommandForAttribute> _results;

public override void Because()
{
_results = Command.GetType().GetCustomAttributes<CommandForAttribute>().ToList();
}

[Fact]
public void ShouldImplementCache()
{
_results.Should().AllSatisfy(c => c.CommandName.Should().Be("cache"));
}

[Fact]
public void ShouldSetADescription()
{
_results.Should().AllSatisfy(c => c.Description.Should().NotBeNullOrEmpty());
}

[Fact]
public void ShouldSetVersionProperty()
{
_results.Should().AllSatisfy(c => c.Version.Should().Be("2.1.0"));
}
}

public class WhenConfiguringTheArgumentParser : ChocolateyCacheCommandSpecsBase
{
private OptionSet _optionSet;

public override void Context()
{
base.Context();
_optionSet = new OptionSet();
}

public override void Because()
{
Command.ConfigureArgumentParser(_optionSet, Configuration);
}

[Fact]
[InlineData("expired")]
public void ShouldAddOptionToOptionSet(string name)
{
_optionSet.Contains(name).Should().BeTrue("Option set should include the parameter {0}", name);
}
}

public class WhenParsingAdditionalParameters : ChocolateyCacheCommandSpecsBase
{
public override void Because()
{

}

public override void BeforeEachSpec()
{
Configuration.CacheCommand.Command = CacheCommandType.Unknown;
MockLogger.Reset();
}

[Fact]
public void ShouldHaveSetCacheCommandTypeToListOnUnusedSubCommand()
{
Command.ParseAdditionalArguments(new List<string>(), Configuration);
Configuration.CacheCommand.Command.Should().Be(CacheCommandType.List);
}

[InlineData("list", CacheCommandType.List)]
[InlineData("remove", CacheCommandType.Remove)]
[InlineData("unknown", CacheCommandType.List)]
public void ShouldHaveSetCacheCommandTypeToListOnListSubCommand(string testArg, CacheCommandType expectedType)
{
var unparsedArgs = new[] { testArg };
Command.ParseAdditionalArguments(unparsedArgs, Configuration);
Configuration.CacheCommand.Command.Should().Be(expectedType);
}

[Fact]
public void ShouldHaveSetCacheCommandTypeToListOnUnknownSubCommand()
{
var unparsedArgs = new[] { "some-command" };

Command.ParseAdditionalArguments(unparsedArgs, Configuration);

using (new AssertionScope())
{
Configuration.CacheCommand.Command.Should().Be(CacheCommandType.List);
MockLogger.Messages.Should().ContainKey(LogLevel.Warn.ToString())
.WhoseValue.Should().Contain("Unknown command 'some-command'. Setting to list.");
}
}
}
}
}
4 changes: 3 additions & 1 deletion src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.CodeAnalysis.BannedApiAnalyzers.3.3.3\build\Microsoft.CodeAnalysis.BannedApiAnalyzers.props" Condition="Exists('..\packages\Microsoft.CodeAnalysis.BannedApiAnalyzers.3.3.3\build\Microsoft.CodeAnalysis.BannedApiAnalyzers.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -201,10 +201,12 @@
</Compile>
<Compile Include="AssemblyExtensions.cs" />
<Compile Include="infrastructure.app\attributes\MultiServiceAttribute.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyListCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyTemplateCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyCommandBase.cs" />
<Compile Include="infrastructure.app\domain\ApiKeyCommandType.cs" />
<Compile Include="infrastructure.app\domain\CacheCommandType.cs" />
<Compile Include="infrastructure.app\domain\SourceTypes.cs" />
<Compile Include="infrastructure.app\domain\ChocolateyPackageMetadata.cs" />
<Compile Include="infrastructure.app\domain\TemplateCommandType.cs" />
Expand Down
Loading

0 comments on commit 0d0db60

Please # to comment.