-
-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3767 from twenzel/feature/SupportDotNetFormat
GH3479: Add alias for dotnet format command
- Loading branch information
Showing
9 changed files
with
660 additions
and
13 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/Cake.Common.Tests/Fixtures/Tools/DotNet/DotNetFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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; | ||
using Cake.Core.IO; | ||
using Cake.Testing.Fixtures; | ||
|
||
namespace Cake.Common.Tests.Fixtures.Tools.DotNet | ||
{ | ||
internal abstract class DotNetFixture<TSettings> : ToolFixture<TSettings, ToolFixtureResult> | ||
where TSettings : DotNetSettings, new() | ||
{ | ||
protected DotNetFixture() | ||
: base("dotnet.exe") | ||
{ | ||
ProcessRunner.Process.SetStandardOutput(new string[] { }); | ||
} | ||
|
||
protected override ToolFixtureResult CreateResult(FilePath path, ProcessSettings process) | ||
{ | ||
return new ToolFixtureResult(path, process); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Cake.Common.Tests/Fixtures/Tools/DotNet/Format/DotNetFormatterFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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.Format; | ||
|
||
namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Format | ||
{ | ||
internal sealed class DotNetFormatterFixture : DotNetFixture<DotNetFormatSettings> | ||
{ | ||
public string Root { get; set; } | ||
|
||
public string Subcommand { get; set; } | ||
|
||
protected override void RunTool() | ||
{ | ||
var tool = new DotNetFormatter(FileSystem, Environment, ProcessRunner, Tools); | ||
tool.Format(Root, Subcommand, Settings); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/Cake.Common.Tests/Unit/Tools/DotNet/Format/DotNetFormatTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// 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.Format; | ||
using Cake.Common.Tools.DotNet.Format; | ||
using Cake.Testing; | ||
using Xunit; | ||
|
||
namespace Cake.Common.Tests.Unit.Tools.DotNet.Format | ||
{ | ||
public sealed class DotNetFormatTests | ||
{ | ||
public sealed class TheFormatMethod | ||
{ | ||
[Fact] | ||
public void Should_Throw_If_Settings_Are_Null() | ||
{ | ||
// Given | ||
var fixture = new DotNetFormatterFixture(); | ||
fixture.Root = "./src/project"; | ||
fixture.Settings = null; | ||
fixture.GivenDefaultToolDoNotExist(); | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
AssertEx.IsArgumentNullException(result, "settings"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_Root_Is_Null() | ||
{ | ||
// Given | ||
var fixture = new DotNetFormatterFixture(); | ||
fixture.Settings = new DotNetFormatSettings(); | ||
fixture.GivenDefaultToolDoNotExist(); | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
AssertEx.IsArgumentNullException(result, "root"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_Process_Was_Not_Started() | ||
{ | ||
// Given | ||
var fixture = new DotNetFormatterFixture(); | ||
fixture.Root = "./src/project"; | ||
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 DotNetFormatterFixture(); | ||
fixture.Root = "./src/project"; | ||
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_Add_Mandatory_Arguments() | ||
{ | ||
// Given | ||
var fixture = new DotNetFormatterFixture(); | ||
fixture.Root = "./src/project"; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal("format \"./src/project\"", result.Args); | ||
} | ||
|
||
[Fact] | ||
public void Should_Add_Additional_Arguments() | ||
{ | ||
// Given | ||
var fixture = new DotNetFormatterFixture(); | ||
fixture.Settings.Diagnostics.Add("CS123"); | ||
fixture.Settings.Diagnostics.Add("CA555"); | ||
fixture.Settings.Severity = DotNetFormatSeverity.Warning; | ||
fixture.Settings.NoRestore = true; | ||
fixture.Settings.VerifyNoChanges = true; | ||
fixture.Settings.Include.Add("./src/"); | ||
fixture.Settings.Include.Add("./tests/"); | ||
fixture.Settings.Exclude.Add("./src/submodule-a/"); | ||
fixture.Settings.IncludeGenerated = true; | ||
fixture.Settings.Verbosity = Common.Tools.DotNet.DotNetVerbosity.Diagnostic; | ||
fixture.Settings.BinaryLog = "./temp/b.log"; | ||
fixture.Settings.Report = "./temp/report.json"; | ||
fixture.Root = "./src/project"; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
var expected = "format \"./src/project\" --diagnostics CS123 CA555 --severity warn --no-restore --verify-no-changes --include ./src/ ./tests/ --exclude ./src/submodule-a/ --include-generated"; | ||
expected += " --binarylog \"/Working/temp/b.log\" --report \"/Working/temp/report.json\" --verbosity diagnostic"; | ||
Assert.Equal(expected, result.Args); | ||
} | ||
|
||
[Theory] | ||
[InlineData("./src/project", "format \"./src/project\"")] | ||
[InlineData("./src/cake build/", "format \"./src/cake build/\"")] | ||
[InlineData("./src/cake build/cake cli", "format \"./src/cake build/cake cli\"")] | ||
public void Should_Quote_Root_Path(string text, string expected) | ||
{ | ||
// Given | ||
var fixture = new DotNetFormatterFixture(); | ||
fixture.Root = text; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expected, result.Args); | ||
} | ||
|
||
[Fact] | ||
public void Should_Add_Subcommand() | ||
{ | ||
// Given | ||
var fixture = new DotNetFormatterFixture(); | ||
fixture.Root = "./src/project"; | ||
fixture.Subcommand = "style"; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal("format style \"./src/project\"", result.Args); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.