Skip to content

Commit

Permalink
Merge pull request #4369 from Marusyk/rmarusyk/4353
Browse files Browse the repository at this point in the history
Add DotNetListReference alias for dotnet list reference command
  • Loading branch information
devlead authored Oct 19, 2024
2 parents 5b60c51 + 10921ad commit a19ff93
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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.Reference.List;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Reference.List
{
internal sealed class DotNetReferenceListerFixture : DotNetFixture<DotNetReferenceListSettings>
{
public string Project { get; set; }

public IEnumerable<string> References { get; set; }

public void GivenProjectReferencesResult()
{
ProcessRunner.Process.SetStandardOutput(new string[]
{
"Project reference(s)",
"--------------------",
"..\\..\\Common\\Common.AspNetCore\\Common.AspNetCore.csproj",
"..\\..\\Common\\Common.Messaging\\Common.Messaging.csproj",
"..\\..\\Common\\Common.Utilities\\Common.Utilities.csproj"
});
}

public void GivenEmptyProjectReferencesResult()
{
ProcessRunner.Process.SetStandardOutput(new string[]
{
"There are no Project to Project references in project C:\\Cake\\Cake.Core\\."
});
}

protected override void RunTool()
{
var tool = new DotNetReferenceLister(FileSystem, Environment, ProcessRunner, Tools);
References = tool.List(Project, Settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// 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.Reference.List;
using Cake.Common.Tools.DotNet;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Reference.List
{
public sealed class DotNetReferenceListerTests
{
public sealed class TheListMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetReferenceListerFixture();
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 DotNetReferenceListerFixture();
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 DotNetReferenceListerFixture();
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Not_Add_Project_Argument()
{
// Given
var fixture = new DotNetReferenceListerFixture();
fixture.Project = null;

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

// Then
Assert.Equal("list reference", result.Args);
}

[Fact]
public void Should_Add_Project_Argument()
{
// Given
var fixture = new DotNetReferenceListerFixture();
fixture.Project = "ToDo.csproj";

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

// Then
Assert.Equal("list \"ToDo.csproj\" reference", result.Args);
}

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetReferenceListerFixture();
fixture.Project = "ToDo.csproj";
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;

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

// Then
var expected = "list \"ToDo.csproj\" reference --verbosity diagnostic";
Assert.Equal(expected, result.Args);
}

[Fact]
public void Should_Return_Correct_List_Of_References()
{
// Given
var fixture = new DotNetReferenceListerFixture();
fixture.GivenProjectReferencesResult();

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

// Then
Assert.Collection(fixture.References,
item =>
{
Assert.Equal(item, "..\\..\\Common\\Common.AspNetCore\\Common.AspNetCore.csproj");
},
item =>
{
Assert.Equal(item, "..\\..\\Common\\Common.Messaging\\Common.Messaging.csproj");
},
item =>
{
Assert.Equal(item, "..\\..\\Common\\Common.Utilities\\Common.Utilities.csproj");
});
}

[Fact]
public void Should_Return_Empty_List_Of_References()
{
// Given
var fixture = new DotNetReferenceListerFixture();
fixture.GivenEmptyProjectReferencesResult();

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

// Then
Assert.Empty(fixture.References);
}
}
}
}
89 changes: 89 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.Reference.List;
using Cake.Common.Tools.DotNet.Reference.Remove;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Common.Tools.DotNet.Run;
Expand Down Expand Up @@ -2628,6 +2629,94 @@ public static void DotNetRemoveReference(this ICakeContext context, string proje
remover.Remove(project, projectReferences, settings);
}

/// <summary>
/// Lists project-to-project references.
/// </summary>
/// <param name="context">The context.</param>
/// <returns>The list of project-to-project references.</returns>
/// <example>
/// <code>
/// var references = DotNetListReference();
///
/// foreach (var reference in references)
/// {
/// Information(reference);
/// }
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.List")]
public static IEnumerable<string> DotNetListReference(this ICakeContext context)
{
return context.DotNetListReference(null);
}

/// <summary>
/// Lists project-to-project references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The project file to operate on. If a file is not specified, the command will search the current directory for one.</param>
/// <returns>The list of project-to-project references.</returns>
/// <example>
/// <code>
/// var references = DotNetListReference("./app/app.csproj");
///
/// foreach (var reference in references)
/// {
/// Information(reference);
/// }
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.List")]
public static IEnumerable<string> DotNetListReference(this ICakeContext context, string project)
{
return context.DotNetListReference(project, null);
}

/// <summary>
/// Lists project-to-project references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The project file to operate on. If a file is not specified, the command will search the current directory for one.</param>
/// <param name="settings">The settings.</param>
/// <returns>The list of project-to-project references.</returns>
/// <example>
/// <code>
/// var settings = new DotNetReferenceListSettings
/// {
/// Verbosity = DotNetVerbosity.Diagnostic
/// };
///
/// var references = DotNetListReference("./app/app.csproj", settings);
///
/// foreach (var reference in references)
/// {
/// Information(reference);
/// }
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.List")]
public static IEnumerable<string> DotNetListReference(this ICakeContext context, string project, DotNetReferenceListSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings is null)
{
settings = new DotNetReferenceListSettings();
}

var lister = new DotNetReferenceLister(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return lister.List(project, settings);
}

/// <summary>
/// List packages on available from source using specified settings.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.

namespace Cake.Common.Tools.DotNet.Reference.List
{
/// <summary>
/// Contains settings used by <see cref="DotNetReferenceLister" />.
/// </summary>
public sealed class DotNetReferenceListSettings : DotNetSettings
{
}
}
Loading

0 comments on commit a19ff93

Please sign in to comment.