forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGivenDotnetOsArchOptions.cs
210 lines (192 loc) · 9.02 KB
/
GivenDotnetOsArchOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using Microsoft.DotNet.Cli.Utils;
using BuildCommand = Microsoft.DotNet.Cli.Commands.Build.BuildCommand;
namespace Microsoft.DotNet.Cli.MSBuild.Tests
{
public class GivenDotnetOsArchOptions : SdkTest
{
public GivenDotnetOsArchOptions(ITestOutputHelper log) : base(log)
{
}
private static readonly string[] ExpectedPrefix = ["-maxcpucount", "-verbosity:m", "-tlp:default=auto", "-nologo"];
private const string NugetInteractiveProperty = "-property:NuGetInteractive=false";
private static readonly string[] DefaultArgs = ["-restore", "-consoleloggerparameters:Summary", NugetInteractiveProperty];
private static readonly string WorkingDirectory =
TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetBuildInvocation));
[Fact]
public void OsOptionIsCorrectlyResolved()
{
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(["--os", "os"], msbuildPath);
var expectedArch = RuntimeInformation.ProcessArchitecture.Equals(Architecture.Arm64) ? "arm64" : Environment.Is64BitOperatingSystem ? "x64" : "x86";
command.GetArgumentTokensToMSBuild()
.Should()
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, $"-property:RuntimeIdentifier=os-{expectedArch}"]);
});
}
[Fact]
public void ArchOptionIsCorrectlyResolved()
{
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(["--arch", "arch"], msbuildPath);
var expectedOs = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" :
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
null;
if (expectedOs == null)
{
// Not a supported OS for running test
return;
}
command.GetArgumentTokensToMSBuild()
.Should()
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, $"-property:RuntimeIdentifier={expectedOs}-arch"]);
});
}
[Fact]
public void OSAndArchOptionsCanBeCombined()
{
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(["--arch", "arch", "--os", "os"], msbuildPath);
command.GetArgumentTokensToMSBuild()
.Should()
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, "-property:RuntimeIdentifier=os-arch"]);
});
}
[Fact]
public void OptionsRespectUserSpecifiedSelfContained()
{
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(["--arch", "arch", "--os", "os", "--self-contained"], msbuildPath);
string[] expectedArgs = [
.. ExpectedPrefix,
.. DefaultArgs,
"-property:SelfContained=True",
"-property:_CommandLineDefinedSelfContained=true",
"-property:RuntimeIdentifier=os-arch"
];
command.GetArgumentTokensToMSBuild()
.Should()
.BeEquivalentTo(expectedArgs);
});
}
[Fact]
public void OSOptionCannotBeCombinedWithRuntime()
{
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var exceptionThrown = Assert.Throws<GracefulException>(() => BuildCommand.FromArgs(["--os", "os", "--runtime", "rid"], msbuildPath));
exceptionThrown.Message.Should().Be(CliStrings.CannotSpecifyBothRuntimeAndOsOptions);
});
}
[Fact]
public void ArchOptionCannotBeCombinedWithRuntime()
{
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var exceptionThrown = Assert.Throws<GracefulException>(() => BuildCommand.FromArgs(["--arch", "arch", "--runtime", "rid"], msbuildPath));
exceptionThrown.Message.Should().Be(CliStrings.CannotSpecifyBothRuntimeAndArchOptions);
});
}
[WindowsOnlyTheory]
[InlineData("build")]
[InlineData("publish")]
[InlineData("test")]
[InlineData("run")]
public void CommandsRunWithOSOption(string command)
{
var testInstance = _testAssetsManager.CopyTestAsset("HelloWorld", identifier: command)
.WithSource();
new DotnetCommand(Log)
.WithWorkingDirectory(testInstance.Path)
.Execute(command, "--os", "win")
.Should()
.Pass();
}
[WindowsOnlyTheory]
[InlineData("build")]
[InlineData("publish")]
[InlineData("test")]
[InlineData("run")]
public void CommandsRunWithArchOption(string command)
{
var testInstance = _testAssetsManager.CopyTestAsset("HelloWorld", identifier: command)
.WithSource();
new DotnetCommand(Log)
.WithWorkingDirectory(testInstance.Path)
.Execute(command, "--arch", RuntimeInformation.ProcessArchitecture.Equals(Architecture.Arm64) ? "arm64" : Environment.Is64BitOperatingSystem ? "x64" : "x86")
.Should()
.Pass();
}
[Fact]
public void ArchOptionsAMD64toX64()
{
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(["--arch", "amd64", "--os", "os"], msbuildPath);
command.GetArgumentTokensToMSBuild()
.Should()
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, "-property:RuntimeIdentifier=os-x64"]);
});
}
[Fact]
public void ArchOptionIsResolvedFromRidUnderDifferentCulture()
{
CultureInfo currentCultureBefore = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo("th");
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(["--os", "os"], msbuildPath);
var expectedArch = RuntimeInformation.ProcessArchitecture.Equals(Architecture.Arm64) ? "arm64" : Environment.Is64BitOperatingSystem ? "x64" : "x86";
command.GetArgumentTokensToMSBuild()
.Should()
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, $"-property:RuntimeIdentifier=os-{expectedArch}"]);
});
}
finally { CultureInfo.CurrentCulture = currentCultureBefore; }
}
[Fact]
public void OsOptionIsResolvedFromRidUnderDifferentCulture()
{
CultureInfo currentCultureBefore = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo("th");
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(["--arch", "arch"], msbuildPath);
var expectedOs = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" :
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
null;
if (expectedOs == null)
{
// Not a supported OS for running test
return;
}
command.GetArgumentTokensToMSBuild()
.Should()
.BeEquivalentTo([.. ExpectedPrefix, .. DefaultArgs, $"-property:RuntimeIdentifier={expectedOs}-arch"]);
});
}
finally { CultureInfo.CurrentCulture = currentCultureBefore; }
}
}
}