Skip to content

Commit b4edd25

Browse files
committed
update tests for a lot of CLI parsing scenarios
1 parent 47e2081 commit b4edd25

File tree

7 files changed

+184
-151
lines changed

7 files changed

+184
-151
lines changed

test/dotnet.Tests/dotnet-msbuild/GivenDotnetCleanInvocation.cs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ namespace Microsoft.DotNet.Cli.MSBuild.Tests
88
[Collection(TestConstants.UsesStaticTelemetryState)]
99
public class GivenDotnetCleanInvocation : IClassFixture<NullCurrentSessionIdFixture>
1010
{
11-
const string ExpectedPrefix = "-maxcpucount -verbosity:m -tlp:default=auto -nologo -verbosity:normal -target:Clean";
11+
private const string NugetInteractiveProperty = "-property:NuGetInteractive=true";
12+
private static readonly string[] ExpectedPrefix = ["-maxcpucount", "-verbosity:m", "-tlp:default=auto", "-nologo", "-verbosity:normal", "-target:Clean", NugetInteractiveProperty];
13+
1214

1315
private static readonly string WorkingDirectory =
1416
TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetCleanInvocation));
@@ -18,33 +20,46 @@ public void ItAddsProjectToMsbuildInvocation()
1820
{
1921
var msbuildPath = "<msbuildpath>";
2022
CleanCommand.FromArgs(new string[] { "<project>" }, msbuildPath)
21-
.GetArgumentsToMSBuild().Should().Be("-maxcpucount -verbosity:m -tlp:default=auto -nologo -verbosity:normal <project> -target:Clean");
23+
.GetArgumentTokensToMSBuild()
24+
.Should()
25+
.BeEquivalentTo([.. ExpectedPrefix, "<project>"]);
2226
}
2327

2428
[Theory]
25-
[InlineData(new string[] { }, "")]
26-
[InlineData(new string[] { "-o", "<output>" }, "-property:OutputPath=<cwd><output> -property:_CommandLineDefinedOutputPath=true")]
27-
[InlineData(new string[] { "--output", "<output>" }, "-property:OutputPath=<cwd><output> -property:_CommandLineDefinedOutputPath=true")]
28-
[InlineData(new string[] { "--artifacts-path", "foo" }, "-property:ArtifactsPath=<cwd>foo")]
29-
[InlineData(new string[] { "-f", "<framework>" }, "-property:TargetFramework=<framework>")]
30-
[InlineData(new string[] { "--framework", "<framework>" }, "-property:TargetFramework=<framework>")]
31-
[InlineData(new string[] { "-c", "<configuration>" }, "-property:Configuration=<configuration>")]
32-
[InlineData(new string[] { "--configuration", "<configuration>" }, "-property:Configuration=<configuration>")]
33-
[InlineData(new string[] { "-v", "diag" }, "-verbosity:diag")]
34-
[InlineData(new string[] { "--verbosity", "diag" }, "-verbosity:diag")]
35-
[InlineData(new string[] { "--disable-build-servers" }, "--property:UseRazorBuildServer=false --property:UseSharedCompilation=false /nodeReuse:false")]
36-
37-
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
29+
[InlineData(new string[] { }, new string[] { })]
30+
[InlineData(new string[] { "-o", "<output>" },
31+
new string[] { "-property:OutputPath=<cwd><output>", "-property:_CommandLineDefinedOutputPath=true" })]
32+
[InlineData(new string[] { "--output", "<output>" },
33+
new string[] { "-property:OutputPath=<cwd><output>", "-property:_CommandLineDefinedOutputPath=true" })]
34+
[InlineData(new string[] { "--artifacts-path", "foo" },
35+
new string[] { "-property:ArtifactsPath=<cwd>foo" })]
36+
[InlineData(new string[] { "-f", "<framework>" },
37+
new string[] { "-property:TargetFramework=<framework>" })]
38+
[InlineData(new string[] { "--framework", "<framework>" },
39+
new string[] { "-property:TargetFramework=<framework>" })]
40+
[InlineData(new string[] { "-c", "<configuration>" },
41+
new string[] { "-property:Configuration=<configuration>" })]
42+
[InlineData(new string[] { "--configuration", "<configuration>" },
43+
new string[] { "-property:Configuration=<configuration>" })]
44+
[InlineData(new string[] { "-v", "diag" },
45+
new string[] { "-verbosity:diag" })]
46+
[InlineData(new string[] { "--verbosity", "diag" },
47+
new string[] { "-verbosity:diag" })]
48+
[InlineData(new string[] { "--disable-build-servers" },
49+
new string[] { "--property:UseRazorBuildServer=false", "--property:UseSharedCompilation=false", "/nodeReuse:false" })]
50+
public void MsbuildInvocationIsCorrect(string[] args, string[] expectedAdditionalArgs)
3851
{
3952
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
4053
{
41-
expectedAdditionalArgs =
42-
(string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}")
43-
.Replace("<cwd>", WorkingDirectory);
54+
expectedAdditionalArgs = expectedAdditionalArgs
55+
.Select(arg => arg.Replace("<cwd>", WorkingDirectory))
56+
.ToArray();
4457

4558
var msbuildPath = "<msbuildpath>";
4659
CleanCommand.FromArgs(args, msbuildPath)
47-
.GetArgumentsToMSBuild().Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}");
60+
.GetArgumentTokensToMSBuild()
61+
.Should()
62+
.BeEquivalentTo([.. ExpectedPrefix, .. expectedAdditionalArgs]);
4863
});
4964
}
5065
}

test/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public GivenDotnetOsArchOptions(ITestOutputHelper log) : base(log)
1414
{
1515
}
1616

17-
const string ExpectedPrefix = "-maxcpucount -verbosity:m -tlp:default=auto -nologo";
17+
private static readonly string[] ExpectedPrefix = ["-maxcpucount", "-verbosity:m", "-tlp:default=auto", "-nologo"];
18+
private const string NugetInteractiveProperty = "-property:NuGetInteractive=true";
19+
private static readonly string[] DefaultArgs = ["-restore", "-consoleloggerparameters:Summary", NugetInteractiveProperty];
1820

1921
private static readonly string WorkingDirectory =
2022
TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetBuildInvocation));
@@ -25,11 +27,12 @@ public void OsOptionIsCorrectlyResolved()
2527
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
2628
{
2729
var msbuildPath = "<msbuildpath>";
28-
var command = BuildCommand.FromArgs(new string[] { "--os", "os" }, msbuildPath);
30+
var command = BuildCommand.FromArgs(["--os", "os"], msbuildPath);
2931
var expectedArch = RuntimeInformation.ProcessArchitecture.Equals(Architecture.Arm64) ? "arm64" : Environment.Is64BitOperatingSystem ? "x64" : "x86";
30-
command.GetArgumentsToMSBuild()
32+
33+
command.GetArgumentTokensToMSBuild()
3134
.Should()
32-
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier=os-{expectedArch}");
35+
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, $"-property:RuntimeIdentifier=os-{expectedArch}"]);
3336
});
3437
}
3538

@@ -39,7 +42,7 @@ public void ArchOptionIsCorrectlyResolved()
3942
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
4043
{
4144
var msbuildPath = "<msbuildpath>";
42-
var command = BuildCommand.FromArgs(new string[] { "--arch", "arch" }, msbuildPath);
45+
var command = BuildCommand.FromArgs(["--arch", "arch"], msbuildPath);
4346
var expectedOs = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" :
4447
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
4548
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
@@ -49,9 +52,9 @@ public void ArchOptionIsCorrectlyResolved()
4952
// Not a supported OS for running test
5053
return;
5154
}
52-
command.GetArgumentsToMSBuild()
55+
command.GetArgumentTokensToMSBuild()
5356
.Should()
54-
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier={expectedOs}-arch");
57+
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, $"-property:RuntimeIdentifier={expectedOs}-arch"]);
5558
});
5659
}
5760

@@ -61,10 +64,10 @@ public void OSAndArchOptionsCanBeCombined()
6164
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
6265
{
6366
var msbuildPath = "<msbuildpath>";
64-
var command = BuildCommand.FromArgs(new string[] { "--arch", "arch", "--os", "os" }, msbuildPath);
65-
command.GetArgumentsToMSBuild()
67+
var command = BuildCommand.FromArgs(["--arch", "arch", "--os", "os"], msbuildPath);
68+
command.GetArgumentTokensToMSBuild()
6669
.Should()
67-
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier=os-arch");
70+
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, "-property:RuntimeIdentifier=os-arch"]);
6871
});
6972
}
7073

@@ -74,10 +77,17 @@ public void OptionsRespectUserSpecifiedSelfContained()
7477
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
7578
{
7679
var msbuildPath = "<msbuildpath>";
77-
var command = BuildCommand.FromArgs(new string[] { "--arch", "arch", "--os", "os", "--self-contained" }, msbuildPath);
78-
command.GetArgumentsToMSBuild()
80+
var command = BuildCommand.FromArgs(["--arch", "arch", "--os", "os", "--self-contained"], msbuildPath);
81+
string[] expectedArgs = [
82+
.. ExpectedPrefix,
83+
.. DefaultArgs,
84+
"-property:SelfContained=True",
85+
"-property:_CommandLineDefinedSelfContained=true",
86+
"-property:RuntimeIdentifier=os-arch"
87+
];
88+
command.GetArgumentTokensToMSBuild()
7989
.Should()
80-
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:SelfContained=True -property:_CommandLineDefinedSelfContained=true -property:RuntimeIdentifier=os-arch");
90+
.BeEquivalentTo(expectedArgs);
8191
});
8292
}
8393

@@ -87,7 +97,7 @@ public void OSOptionCannotBeCombinedWithRuntime()
8797
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
8898
{
8999
var msbuildPath = "<msbuildpath>";
90-
var exceptionThrown = Assert.Throws<GracefulException>(() => BuildCommand.FromArgs(new string[] { "--os", "os", "--runtime", "rid" }, msbuildPath));
100+
var exceptionThrown = Assert.Throws<GracefulException>(() => BuildCommand.FromArgs(["--os", "os", "--runtime", "rid"], msbuildPath));
91101
exceptionThrown.Message.Should().Be(CommonLocalizableStrings.CannotSpecifyBothRuntimeAndOsOptions);
92102
});
93103
}
@@ -98,7 +108,7 @@ public void ArchOptionCannotBeCombinedWithRuntime()
98108
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
99109
{
100110
var msbuildPath = "<msbuildpath>";
101-
var exceptionThrown = Assert.Throws<GracefulException>(() => BuildCommand.FromArgs(new string[] { "--arch", "arch", "--runtime", "rid" }, msbuildPath));
111+
var exceptionThrown = Assert.Throws<GracefulException>(() => BuildCommand.FromArgs(["--arch", "arch", "--runtime", "rid"], msbuildPath));
102112
exceptionThrown.Message.Should().Be(CommonLocalizableStrings.CannotSpecifyBothRuntimeAndArchOptions);
103113
});
104114
}
@@ -143,10 +153,10 @@ public void ArchOptionsAMD64toX64()
143153
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
144154
{
145155
var msbuildPath = "<msbuildpath>";
146-
var command = BuildCommand.FromArgs(new string[] { "--arch", "amd64", "--os", "os" }, msbuildPath);
147-
command.GetArgumentsToMSBuild()
156+
var command = BuildCommand.FromArgs(["--arch", "amd64", "--os", "os"], msbuildPath);
157+
command.GetArgumentTokensToMSBuild()
148158
.Should()
149-
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier=os-x64");
159+
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, "-property:RuntimeIdentifier=os-x64"]);
150160
});
151161
}
152162

@@ -160,11 +170,11 @@ public void ArchOptionIsResolvedFromRidUnderDifferentCulture()
160170
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
161171
{
162172
var msbuildPath = "<msbuildpath>";
163-
var command = BuildCommand.FromArgs(new string[] { "--os", "os" }, msbuildPath);
173+
var command = BuildCommand.FromArgs(["--os", "os"], msbuildPath);
164174
var expectedArch = RuntimeInformation.ProcessArchitecture.Equals(Architecture.Arm64) ? "arm64" : Environment.Is64BitOperatingSystem ? "x64" : "x86";
165-
command.GetArgumentsToMSBuild()
175+
command.GetArgumentTokensToMSBuild()
166176
.Should()
167-
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier=os-{expectedArch}");
177+
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, $"-property:RuntimeIdentifier=os-{expectedArch}"]);
168178
});
169179
}
170180
finally { CultureInfo.CurrentCulture = currentCultureBefore; }
@@ -180,7 +190,7 @@ public void OsOptionIsResolvedFromRidUnderDifferentCulture()
180190
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
181191
{
182192
var msbuildPath = "<msbuildpath>";
183-
var command = BuildCommand.FromArgs(new string[] { "--arch", "arch" }, msbuildPath);
193+
var command = BuildCommand.FromArgs(["--arch", "arch"], msbuildPath);
184194
var expectedOs = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" :
185195
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
186196
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
@@ -190,9 +200,9 @@ public void OsOptionIsResolvedFromRidUnderDifferentCulture()
190200
// Not a supported OS for running test
191201
return;
192202
}
193-
command.GetArgumentsToMSBuild()
203+
command.GetArgumentTokensToMSBuild()
194204
.Should()
195-
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier={expectedOs}-arch");
205+
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, $"-property:RuntimeIdentifier={expectedOs}-arch"]);
196206
});
197207
}
198208
finally { CultureInfo.CurrentCulture = currentCultureBefore; }

0 commit comments

Comments
 (0)