Skip to content

Commit

Permalink
dotnet format fix (#561)
Browse files Browse the repository at this point in the history
* dotnet format fix

* Fix No_Matching_Placeholder_Positional_Is_Appended
  • Loading branch information
thomhurst authored Aug 12, 2024
1 parent 924fe92 commit 6097101
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/ModularPipelines.Build/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
null
Fix running `dotnet format` via `context.DotNet().Format()`
4 changes: 2 additions & 2 deletions src/ModularPipelines.DotNet/Options/DotNetFormatOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public DotNetFormatOptions(
string projectSolution
)
{
CommandParts = ["format", "[options]", "[<PROJECT | SOLUTION>]"];
CommandParts = ["format", "[<PROJECT | SOLUTION>]"];

ProjectSolution = projectSolution;
}

public DotNetFormatOptions()
{
CommandParts = ["format", "[options]", "[<PROJECT | SOLUTION>]"];
CommandParts = ["format"];
}

[PositionalArgument(PlaceholderName = "[<PROJECT | SOLUTION>]")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ private static void AddPlaceholderArguments(List<string> precedingArguments, obj
var indexOfMatchingPrecedingArgumentPlaceholder =
precedingArguments.FindIndex(x => x == placeholderName);

if (indexOfMatchingPrecedingArgumentPlaceholder < 0)
if (indexOfMatchingPrecedingArgumentPlaceholder < 0 && !string.IsNullOrEmpty(value))
{
throw new ArgumentException($"No matching placeholder found for property {positionalPlaceholderArgument.Name}");
precedingArguments.Add(value);
continue;
}

if (string.IsNullOrWhiteSpace(value) && precedingArguments[indexOfMatchingPrecedingArgumentPlaceholder].StartsWith('<'))
Expand Down
20 changes: 16 additions & 4 deletions test/ModularPipelines.UnitTests/CommandParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ await Assert.That(() => GetResult(new PlaceholderToolOptions(package!, "MyProjec
}

[Test]
public async Task No_Matching_Placeholder_Positional_Throws()
public async Task No_Matching_Placeholder_Positional_Is_Appended()
{
await Assert.That(() => GetResult(new PlaceholderToolOptions2("ThisPackage", "MyProject.csproj")
var result = await GetResult(new PlaceholderToolOptions3()
{
Source = "nuget.org"
})).Throws.Exception().OfType<ArgumentException>();
Project = "MyProject.csproj"
});

await Assert.That(result.CommandInput).Is.EqualTo("dotnet add MyProject.csproj");
}

private async Task<CommandResult> GetResult(CommandLineToolOptions options)
Expand Down Expand Up @@ -218,4 +220,14 @@ private record PlaceholderToolOptions2(string Package, string Project) : Command
[CommandSwitch("--source")]
public string? Source { get; set; }
}

[CommandPrecedingArguments("add")]
private record PlaceholderToolOptions3() : CommandLineToolOptions("dotnet")
{
[PositionalArgument(PlaceholderName = "[<PROJECT>]")]
public string? Project { get; set; }

[CommandSwitch("--source")]
public string? Source { get; set; }
}
}
26 changes: 26 additions & 0 deletions test/ModularPipelines.UnitTests/Helpers/DotNetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ private class DotNetVersionModule : Module<CommandResult>
}, token: cancellationToken);
}
}

private class DotNetFormatModule : Module<CommandResult>
{
protected override async Task<CommandResult?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
{
return await context.DotNet().Format(new DotNetFormatOptions
{
ProjectSolution = context.Git().RootDirectory.FindFile(x => x.Name.Contains("TestsForTests")).AssertExists(),
}, token: cancellationToken);
}
}

[Test]
public async Task Has_Not_Errored()
Expand All @@ -36,6 +47,21 @@ public async Task Has_Not_Errored()
await Assert.That(moduleResult.Value).Is.Not.Null();
}
}

[Test]
public async Task Format_Has_Not_Errored()
{
var module = await RunModule<DotNetFormatModule>();

var moduleResult = await module;

await using (Assert.Multiple())
{
await Assert.That(moduleResult.ModuleResultType).Is.EqualTo(ModuleResultType.Success);
await Assert.That(moduleResult.Exception).Is.Null();
await Assert.That(moduleResult.Value).Is.Not.Null();
}
}

[Test]
public async Task Standard_Output_Starts_With_Git_Version()
Expand Down

0 comments on commit 6097101

Please sign in to comment.