Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .NET package search to Cake scripts #4320

Merged
merged 2 commits into from
Sep 26, 2024
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,52 @@
// 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 System.Collections.Generic;
using Cake.Common.Tools.DotNet.Package.Search;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Package.Search
{
internal class DotNetPackageSearcherFixture : DotNetFixture<DotNetPackageSearchSettings>
{
public string SearchTerm { get; set; }

public IEnumerable<DotNetPackageSearchItem> Result { get; private set; }

protected override void RunTool()
{
var tool = new DotNetPackageSearcher(FileSystem, Environment, ProcessRunner, Tools);
Result = tool.Search(SearchTerm, Settings);
}

internal void GivenNormalPackageResult()
{
ProcessRunner.Process.SetStandardOutput(new string[]
{
"{",
" \"version\": 2,",
" \"problems\": [],",
" \"searchResult\": [",
" {",
" \"sourceName\": \"nuget.org\",",
" \"packages\": [",
" {",
" \"id\": \"Cake\",",
" \"latestVersion\": \"0.22.2\"",
" },",
" {",
" \"id\": \"Cake.Core\",",
" \"latestVersion\": \"0.22.2\"",
" },",
" {",
" \"id\": \"Cake.CoreCLR\",",
" \"latestVersion\": \"0.22.2\"",
" }",
" ]",
" }",
" ]",
"}",
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Cake.Common.Tools.DotNet.Package.Search;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Package.Search
{
public sealed class DotNetPackageSearchSettingsTests
{
public sealed class TheConstructor
{
[Fact]
public void Should_Set_ExactMatch_To_False_By_Default()
{
// Given, When
var settings = new DotNetPackageSearchSettings();

// Then
Assert.False(settings.ExactMatch);
}

[Fact]
public void Should_Set_Take_To_Null_By_Default()
{
// Given, When
var settings = new DotNetPackageSearchSettings();

// Then
Assert.Null(settings.Take);
}

[Fact]
public void Should_Set_Skip_To_Null_By_Default()
{
// Given, When
var settings = new DotNetPackageSearchSettings();

// Then
Assert.Null(settings.Skip);
}

[Fact]
public void Should_Set_Prerelease_To_False_By_Default()
{
// Given, When
var settings = new DotNetPackageSearchSettings();

// Then
Assert.False(settings.Prerelease);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// 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.Package.Search;
using Cake.Common.Tools.DotNet;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Package.Search
{
public sealed class DotNetPackageSearcherTests
{
public sealed class TheSearchMethod
{
[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.Settings = null;

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

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

[Fact]
public void Should_Add_Mandatory_Arguments()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.SearchTerm = "Cake";
fixture.GivenNormalPackageResult();

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

// Then
Assert.Equal("package search \"Cake\" --verbosity normal --format json", result.Args);
}

[Fact]
public void Should_Add_ExactMatch_To_Arguments_If_True()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.SearchTerm = "Cake";
fixture.Settings.ExactMatch = true;
fixture.GivenNormalPackageResult();

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

// Then
Assert.Equal("package search \"Cake\" --exact-match --verbosity normal --format json", result.Args);
}

[Fact]
public void Should_Add_Prerelease_To_Arguments_If_True()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.SearchTerm = "Cake";
fixture.Settings.Prerelease = true;
fixture.GivenNormalPackageResult();

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

// Then
Assert.Equal("package search \"Cake\" --prerelease --verbosity normal --format json", result.Args);
}

[Fact]
public void Should_Add_Take_To_Arguments_If_True()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.SearchTerm = "Cake";
fixture.Settings.Take = 10;
fixture.GivenNormalPackageResult();

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

// Then
Assert.Equal("package search \"Cake\" --take 10 --verbosity normal --format json", result.Args);
}

[Fact]
public void Should_Add_Skip_To_Arguments_If_True()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.SearchTerm = "Cake";
fixture.Settings.Skip = 10;
fixture.GivenNormalPackageResult();

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

// Then
Assert.Equal("package search \"Cake\" --skip 10 --verbosity normal --format json", result.Args);
}

[Fact]
public void Should_Add_Sources_To_Arguments_If_Set()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.SearchTerm = "Cake";
fixture.Settings.Sources = new[] { "A", "B", "C", };
fixture.GivenNormalPackageResult();

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

// Then
Assert.Equal("package search \"Cake\" --source \"A\" --source \"B\" --source \"C\" --verbosity normal --format json", result.Args);
}

[Fact]
public void Should_Add_ConfigFile_To_Arguments_If_Set()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.SearchTerm = "Cake";
fixture.Settings.ConfigFile = "./nuget.config";
fixture.GivenNormalPackageResult();

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

// Then
Assert.Equal("package search \"Cake\" --configfile \"/Working/nuget.config\" " +
"--verbosity normal --format json", result.Args);
}

[Fact]
public void Should_Return_Correct_List_Of_DotNetPackageSearchItems()
{
// Given
var fixture = new DotNetPackageSearcherFixture();
fixture.SearchTerm = "Cake";
fixture.GivenNormalPackageResult();

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

// Then
Assert.Collection(fixture.Result,
item =>
{
Assert.Equal(item.Name, "Cake");
Assert.Equal(item.Version, "0.22.2");
},
item =>
{
Assert.Equal(item.Name, "Cake.Core");
Assert.Equal(item.Version, "0.22.2");
},
item =>
{
Assert.Equal(item.Name, "Cake.CoreCLR");
Assert.Equal(item.Version, "0.22.2");
});
}
}
}
}
95 changes: 95 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Cake.Common.Tools.DotNet.Pack;
using Cake.Common.Tools.DotNet.Package.Add;
using Cake.Common.Tools.DotNet.Package.Remove;
using Cake.Common.Tools.DotNet.Package.Search;
using Cake.Common.Tools.DotNet.Publish;
using Cake.Common.Tools.DotNet.Reference.Add;
using Cake.Common.Tools.DotNet.Restore;
Expand Down Expand Up @@ -2527,5 +2528,99 @@ public static void DotNetAddReference(this ICakeContext context, string project,
var adder = new DotNetReferenceAdder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
adder.Add(project, projectReferences, settings);
}

/// <summary>
/// List packages on available from source using specified settings.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="searchTerm">The search term.</param>
/// <param name="settings">The settings.</param>
/// <returns>List of packages with their version.</returns>
/// <example>
/// <code>
/// var packageList = DotNetPackageSearch("Cake", new DotNetPackageSearchSettings {
/// AllVersions = false,
/// Prerelease = false
/// });
/// foreach(var package in packageList)
/// {
/// Information("Found package {0}, version {1}", package.Name, package.Version);
/// }
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Search")]
public static IEnumerable<DotNetPackageSearchItem> DotNetSearchPackage(this ICakeContext context, string searchTerm, DotNetPackageSearchSettings settings)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var runner = new DotNetPackageSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return runner.Search(searchTerm, settings);
}

/// <summary>
/// List packages on available from source using specified settings.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="searchTerm">The package Id.</param>
/// <returns>List of packages with their version.</returns>
/// <example>
/// <code>
/// var packageList = DotNetPackageSearch("Cake", new DotNetPackageSearchSettings {
/// AllVersions = false,
/// Prerelease = false
/// });
/// foreach(var package in packageList)
/// {
/// Information("Found package {0}, version {1}", package.Name, package.Version);
/// }
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Search")]
public static IEnumerable<DotNetPackageSearchItem> DotNetSearchPackage(this ICakeContext context, string searchTerm)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var runner = new DotNetPackageSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return runner.Search(searchTerm, new DotNetPackageSearchSettings());
}

/// <summary>
/// List packages on available from source using specified settings.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="settings">The settings.</param>
/// <returns>List of packages with their version.</returns>
/// <example>
/// <code>
/// var packageList = DotNetPackageSearch("Cake", new DotNetPackageSearchSettings {
/// AllVersions = false,
/// Prerelease = false
/// });
/// foreach(var package in packageList)
/// {
/// Information("Found package {0}, version {1}", package.Name, package.Version);
/// }
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Search")]
public static IEnumerable<DotNetPackageSearchItem> DotNetSearchPackage(this ICakeContext context, DotNetPackageSearchSettings settings)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var runner = new DotNetPackageSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return runner.Search(null, settings);
}
}
}
Loading
Loading