Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
HttpCache: C:/Users/username/AppData/Local/NuGet/v3-cache,
GlobalPackages: C:/Users/username/.nuget/packages,
Temp: C:/Users/username/AppData/Local/Temp/NuGetScratch,
PluginsCache: C:/Users/username/AppData/Local/NuGet/plugins-cache
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
HttpCache: C:/Users/username/AppData/Local/NuGet/v3-cache,
GlobalPackages: null,
Temp: null,
PluginsCache: null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Null: {
HttpCache: null,
GlobalPackages: null,
Temp: null,
PluginsCache: null
},
Empty: {
HttpCache: null,
GlobalPackages: null,
Temp: null,
PluginsCache: null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tools.DotNet.NuGet.Locals;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.NuGet.Locals;

internal sealed class DotNetNuGetLocalserFixture : DotNetFixture<DotNetNuGetLocalsSettings>
{
public DotNetNuGetLocalsFolder Folder { get; set; } = DotNetNuGetLocalsFolder.All;

public bool Clear { get; set; }

public DotNetNuGetLocalsListResult Output { get; private set; }

protected override void RunTool()
{
var tool = new DotNetNuGetLocalser(FileSystem, Environment, ProcessRunner, Tools);
if (Clear)
{
tool.Clear(Folder, Settings);
return;
}

Output = tool.List(Folder, Settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tools.DotNet.NuGet.Locals;

namespace Cake.Common.Tests.Unit.Tools.DotNet.NuGet.Locals;

public sealed class DotNetNuGetLocalsParserTests
{
[Fact]
public async Task Should_Parse_List_All()
{
// Given
var lines = new[]
{
"http-cache: C:\\Users\\username\\AppData\\Local\\NuGet\\v3-cache",
"global-packages: C:\\Users\\username\\.nuget\\packages\\",
"temp: C:\\Users\\username\\AppData\\Local\\Temp\\NuGetScratch",
"plugins-cache: C:\\Users\\username\\AppData\\Local\\NuGet\\plugins-cache"
};

// When
var result = DotNetNuGetLocalsParser.Parse(lines);

// Then
await Verify(result);
}

[Fact]
public async Task Should_Parse_Single_Folder()
{
// Given
var lines = new[]
{
"http-cache: C:\\Users\\username\\AppData\\Local\\NuGet\\v3-cache"
};

// When
var result = DotNetNuGetLocalsParser.Parse(lines);

// Then
await Verify(result);
}

[Fact]
public async Task Should_Return_Empty_Result_For_Null_Or_Empty_Input()
{
// Given
IEnumerable<string> empty = [];

// When
var nullResult = DotNetNuGetLocalsParser.Parse(null);
var emptyResult = DotNetNuGetLocalsParser.Parse(empty);

// Then
await Verify(new { Null = nullResult, Empty = emptyResult });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tests.Fixtures.Tools.DotNet.NuGet.Locals;
using Cake.Common.Tools.DotNet.NuGet.Locals;
using Cake.Testing;

namespace Cake.Common.Tests.Unit.Tools.DotNet.NuGet.Locals;

public sealed class DotNetNuGetLocalserTests
{
public sealed class TheListMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetNuGetLocalserFixture();
fixture.GivenProcessCannotStart();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsCakeException(result, ".NET CLI: Process was not started.");
}

[Fact]
public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
{
// Given
var fixture = new DotNetNuGetLocalserFixture();
fixture.GivenProcessExitsWithCode(1);

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1).");
}

[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given
var fixture = new DotNetNuGetLocalserFixture();
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsArgumentNullException(result, "settings");
}

[Fact]
public void Should_Throw_If_Folder_Is_Invalid()
{
// Given
var fixture = new DotNetNuGetLocalserFixture();
fixture.Folder = (DotNetNuGetLocalsFolder)42;

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsArgumentOutOfRangeException(result, "folder");
}

[Theory]
[InlineData(DotNetNuGetLocalsFolder.All, "nuget locals all --list")]
[InlineData(DotNetNuGetLocalsFolder.HttpCache, "nuget locals http-cache --list")]
[InlineData(DotNetNuGetLocalsFolder.GlobalPackages, "nuget locals global-packages --list")]
[InlineData(DotNetNuGetLocalsFolder.Temp, "nuget locals temp --list")]
[InlineData(DotNetNuGetLocalsFolder.PluginsCache, "nuget locals plugins-cache --list")]
public void Should_Add_Folder_And_List_Arguments(DotNetNuGetLocalsFolder folder, string expected)
{
// Given
var fixture = new DotNetNuGetLocalserFixture { Folder = folder };

// When
var result = fixture.Run();

// Then
Assert.Equal(expected, result.Args);
}

[Fact]
public void Should_Add_Force_English_Output()
{
// Given
var fixture = new DotNetNuGetLocalserFixture();
fixture.Settings.ForceEnglishOutput = true;

// When
var result = fixture.Run();

// Then
Assert.Equal("nuget locals all --list --force-english-output", result.Args);
}
}

public sealed class TheClearMethod
{
[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given
var fixture = new DotNetNuGetLocalserFixture { Clear = true };
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsArgumentNullException(result, "settings");
}

[Theory]
[InlineData(DotNetNuGetLocalsFolder.All, "nuget locals all --clear")]
[InlineData(DotNetNuGetLocalsFolder.HttpCache, "nuget locals http-cache --clear")]
[InlineData(DotNetNuGetLocalsFolder.GlobalPackages, "nuget locals global-packages --clear")]
[InlineData(DotNetNuGetLocalsFolder.Temp, "nuget locals temp --clear")]
[InlineData(DotNetNuGetLocalsFolder.PluginsCache, "nuget locals plugins-cache --clear")]
public void Should_Add_Folder_And_Clear_Arguments(DotNetNuGetLocalsFolder folder, string expected)
{
// Given
var fixture = new DotNetNuGetLocalserFixture
{
Folder = folder,
Clear = true
};

// When
var result = fixture.Run();

// Then
Assert.Equal(expected, result.Args);
}

[Fact]
public void Should_Add_Force_English_Output()
{
// Given
var fixture = new DotNetNuGetLocalserFixture { Clear = true };
fixture.Settings.ForceEnglishOutput = true;

// When
var result = fixture.Run();

// Then
Assert.Equal("nuget locals all --clear --force-english-output", result.Args);
}
}
}
99 changes: 99 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.NuGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using Cake.Common.Tools.DotNet.NuGet.Delete;
using Cake.Common.Tools.DotNet.NuGet.Locals;
using Cake.Common.Tools.DotNet.NuGet.Push;
using Cake.Common.Tools.DotNet.NuGet.Source;
using Cake.Common.Tools.DotNet.NuGet.Why;
Expand Down Expand Up @@ -576,4 +577,102 @@ public static DotNetNuGetWhyResult DotNetNuGetWhy(this ICakeContext context, str
var whyer = new DotNetNuGetWhyer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return whyer.Why(project, package, settings);
}

/// <summary>
/// Lists local NuGet cache folders.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="folder">The local folder to list.</param>
/// <returns>The parsed local folder paths.</returns>
/// <example>
/// <code>
/// var result = DotNetNuGetLocalsList(DotNetNuGetLocalsFolder.All);
/// Information(result.GlobalPackages);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("NuGet")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Locals")]
public static DotNetNuGetLocalsListResult DotNetNuGetLocalsList(this ICakeContext context, DotNetNuGetLocalsFolder folder)
{
return context.DotNetNuGetLocalsList(folder, null);
}

/// <summary>
/// Lists local NuGet cache folders.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="folder">The local folder to list.</param>
/// <param name="settings">The settings.</param>
/// <returns>The parsed local folder paths.</returns>
/// <example>
/// <code>
/// var settings = new DotNetNuGetLocalsSettings
/// {
/// ForceEnglishOutput = true
/// };
///
/// var result = DotNetNuGetLocalsList(DotNetNuGetLocalsFolder.HttpCache, settings);
/// Information(result.HttpCache);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("NuGet")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Locals")]
public static DotNetNuGetLocalsListResult DotNetNuGetLocalsList(this ICakeContext context, DotNetNuGetLocalsFolder folder, DotNetNuGetLocalsSettings settings)
{
ArgumentNullException.ThrowIfNull(context);

settings ??= new DotNetNuGetLocalsSettings();

var localser = new DotNetNuGetLocalser(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return localser.List(folder, settings);
}

/// <summary>
/// Clears local NuGet cache folders.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="folder">The local folder to clear.</param>
/// <example>
/// <code>
/// DotNetNuGetLocalsClear(DotNetNuGetLocalsFolder.Temp);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("NuGet")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Locals")]
public static void DotNetNuGetLocalsClear(this ICakeContext context, DotNetNuGetLocalsFolder folder)
{
context.DotNetNuGetLocalsClear(folder, null);
}

/// <summary>
/// Clears local NuGet cache folders.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="folder">The local folder to clear.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetNuGetLocalsSettings
/// {
/// ForceEnglishOutput = true
/// };
///
/// DotNetNuGetLocalsClear(DotNetNuGetLocalsFolder.Temp, settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("NuGet")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Locals")]
public static void DotNetNuGetLocalsClear(this ICakeContext context, DotNetNuGetLocalsFolder folder, DotNetNuGetLocalsSettings settings)
{
ArgumentNullException.ThrowIfNull(context);

settings ??= new DotNetNuGetLocalsSettings();

var localser = new DotNetNuGetLocalser(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
localser.Clear(folder, settings);
}
}
Loading
Loading