Skip to content

Commit 6097101

Browse files
authored
dotnet format fix (#561)
* dotnet format fix * Fix No_Matching_Placeholder_Positional_Is_Appended
1 parent 924fe92 commit 6097101

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
null
1+
Fix running `dotnet format` via `context.DotNet().Format()`

src/ModularPipelines.DotNet/Options/DotNetFormatOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public DotNetFormatOptions(
1010
string projectSolution
1111
)
1212
{
13-
CommandParts = ["format", "[options]", "[<PROJECT | SOLUTION>]"];
13+
CommandParts = ["format", "[<PROJECT | SOLUTION>]"];
1414

1515
ProjectSolution = projectSolution;
1616
}
1717

1818
public DotNetFormatOptions()
1919
{
20-
CommandParts = ["format", "[options]", "[<PROJECT | SOLUTION>]"];
20+
CommandParts = ["format"];
2121
}
2222

2323
[PositionalArgument(PlaceholderName = "[<PROJECT | SOLUTION>]")]

src/ModularPipelines/Helpers/CommandOptionsObjectArgumentParser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ private static void AddPlaceholderArguments(List<string> precedingArguments, obj
5050
var indexOfMatchingPrecedingArgumentPlaceholder =
5151
precedingArguments.FindIndex(x => x == placeholderName);
5252

53-
if (indexOfMatchingPrecedingArgumentPlaceholder < 0)
53+
if (indexOfMatchingPrecedingArgumentPlaceholder < 0 && !string.IsNullOrEmpty(value))
5454
{
55-
throw new ArgumentException($"No matching placeholder found for property {positionalPlaceholderArgument.Name}");
55+
precedingArguments.Add(value);
56+
continue;
5657
}
5758

5859
if (string.IsNullOrWhiteSpace(value) && precedingArguments[indexOfMatchingPrecedingArgumentPlaceholder].StartsWith('<'))

test/ModularPipelines.UnitTests/CommandParserTests.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ await Assert.That(() => GetResult(new PlaceholderToolOptions(package!, "MyProjec
148148
}
149149

150150
[Test]
151-
public async Task No_Matching_Placeholder_Positional_Throws()
151+
public async Task No_Matching_Placeholder_Positional_Is_Appended()
152152
{
153-
await Assert.That(() => GetResult(new PlaceholderToolOptions2("ThisPackage", "MyProject.csproj")
153+
var result = await GetResult(new PlaceholderToolOptions3()
154154
{
155-
Source = "nuget.org"
156-
})).Throws.Exception().OfType<ArgumentException>();
155+
Project = "MyProject.csproj"
156+
});
157+
158+
await Assert.That(result.CommandInput).Is.EqualTo("dotnet add MyProject.csproj");
157159
}
158160

159161
private async Task<CommandResult> GetResult(CommandLineToolOptions options)
@@ -218,4 +220,14 @@ private record PlaceholderToolOptions2(string Package, string Project) : Command
218220
[CommandSwitch("--source")]
219221
public string? Source { get; set; }
220222
}
223+
224+
[CommandPrecedingArguments("add")]
225+
private record PlaceholderToolOptions3() : CommandLineToolOptions("dotnet")
226+
{
227+
[PositionalArgument(PlaceholderName = "[<PROJECT>]")]
228+
public string? Project { get; set; }
229+
230+
[CommandSwitch("--source")]
231+
public string? Source { get; set; }
232+
}
221233
}

test/ModularPipelines.UnitTests/Helpers/DotNetTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ private class DotNetVersionModule : Module<CommandResult>
2121
}, token: cancellationToken);
2222
}
2323
}
24+
25+
private class DotNetFormatModule : Module<CommandResult>
26+
{
27+
protected override async Task<CommandResult?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
28+
{
29+
return await context.DotNet().Format(new DotNetFormatOptions
30+
{
31+
ProjectSolution = context.Git().RootDirectory.FindFile(x => x.Name.Contains("TestsForTests")).AssertExists(),
32+
}, token: cancellationToken);
33+
}
34+
}
2435

2536
[Test]
2637
public async Task Has_Not_Errored()
@@ -36,6 +47,21 @@ public async Task Has_Not_Errored()
3647
await Assert.That(moduleResult.Value).Is.Not.Null();
3748
}
3849
}
50+
51+
[Test]
52+
public async Task Format_Has_Not_Errored()
53+
{
54+
var module = await RunModule<DotNetFormatModule>();
55+
56+
var moduleResult = await module;
57+
58+
await using (Assert.Multiple())
59+
{
60+
await Assert.That(moduleResult.ModuleResultType).Is.EqualTo(ModuleResultType.Success);
61+
await Assert.That(moduleResult.Exception).Is.Null();
62+
await Assert.That(moduleResult.Value).Is.Not.Null();
63+
}
64+
}
3965

4066
[Test]
4167
public async Task Standard_Output_Starts_With_Git_Version()

0 commit comments

Comments
 (0)