-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathMultiArchInstallLocation.cs
More file actions
267 lines (237 loc) · 11.9 KB
/
MultiArchInstallLocation.cs
File metadata and controls
267 lines (237 loc) · 11.9 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli.Build.Framework;
using Microsoft.DotNet.CoreSetup.Test;
using Microsoft.DotNet.CoreSetup.Test.HostActivation;
using Xunit;
namespace HostActivation.Tests
{
public class MultiArchInstallLocation : IClassFixture<MultiArchInstallLocation.SharedTestState>
{
private SharedTestState sharedTestState;
public MultiArchInstallLocation(SharedTestState fixture)
{
sharedTestState = fixture;
}
[Fact]
public void EnvironmentVariable_CurrentArchitectureIsUsedIfEnvVarSet()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
var appExe = fixture.TestProject.AppExe;
var arch = fixture.RepoDirProvider.BuildArchitecture.ToUpper();
Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.DotNetRoot(fixture.BuiltDotnet.BinPath, arch)
.Execute()
.Should().Pass()
.And.HaveUsedDotNetRootInstallLocation(fixture.BuiltDotnet.BinPath, fixture.CurrentRid, arch);
}
[Fact]
public void EnvironmentVariable_IfNoArchSpecificEnvVarIsFoundDotnetRootIsUsed()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
var appExe = fixture.TestProject.AppExe;
var arch = fixture.RepoDirProvider.BuildArchitecture.ToUpper();
Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.DotNetRoot(fixture.BuiltDotnet.BinPath)
.Execute()
.Should().Pass()
.And.HaveUsedDotNetRootInstallLocation(fixture.BuiltDotnet.BinPath, fixture.CurrentRid);
}
[Fact]
public void EnvironmentVariable_ArchSpecificDotnetRootIsUsedOverDotnetRoot()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
var appExe = fixture.TestProject.AppExe;
var arch = fixture.RepoDirProvider.BuildArchitecture.ToUpper();
var dotnet = fixture.BuiltDotnet.BinPath;
Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.DotNetRoot("non_existent_path")
.DotNetRoot(dotnet, arch)
.Execute()
.Should().Pass()
.And.HaveUsedDotNetRootInstallLocation(dotnet, fixture.CurrentRid, arch)
.And.NotHaveStdErrContaining("Using environment variable DOTNET_ROOT=");
}
[Fact]
public void EnvironmentVariable_DotnetRootPathDoesNotExist()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
var appExe = fixture.TestProject.AppExe;
using (TestOnlyProductBehavior.Enable(appExe))
{
Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.DotNetRoot("non_existent_path")
.MultilevelLookup(false)
.EnvironmentVariable(
Constants.TestOnlyEnvironmentVariables.GloballyRegisteredPath,
sharedTestState.InstallLocation)
.Execute()
.Should().Pass()
.And.HaveStdErrContaining("Did not find [DOTNET_ROOT] directory [non_existent_path]")
// If DOTNET_ROOT points to a folder that does not exist, we fall back to the global install path.
.And.HaveUsedGlobalInstallLocation(sharedTestState.InstallLocation)
.And.HaveStdOutContaining("Hello World");
}
}
[Fact]
public void EnvironmentVariable_DotnetRootPathExistsButHasNoHost()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
var appExe = fixture.TestProject.AppExe;
var projDir = fixture.TestProject.ProjectDirectory;
using (TestOnlyProductBehavior.Enable(appExe))
{
Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.DotNetRoot(projDir)
.MultilevelLookup(false)
.EnvironmentVariable(
Constants.TestOnlyEnvironmentVariables.GloballyRegisteredPath,
sharedTestState.InstallLocation)
.Execute()
.Should().Fail()
.And.HaveUsedDotNetRootInstallLocation(projDir, fixture.CurrentRid)
// If DOTNET_ROOT points to a folder that exists we assume that there's a dotnet installation in it
.And.HaveStdErrContaining($"A fatal error occurred. The required library {RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform ("hostfxr")} could not be found.");
}
}
[Fact]
[SkipOnPlatform(TestPlatforms.Windows, "This test targets the install_location config file which is only used on Linux and macOS.")]
public void InstallLocationFile_ArchSpecificLocationIsPickedFirst()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
var appExe = fixture.TestProject.AppExe;
var arch1 = "someArch";
var path1 = "x/y/z";
var arch2 = fixture.RepoDirProvider.BuildArchitecture;
var path2 = "a/b/c";
using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(appExe))
{
registeredInstallLocationOverride.SetInstallLocation(new (string, string)[] {
(string.Empty, path1),
(arch1, path1),
(arch2, path2)
});
Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.DotNetRoot(null)
.Execute()
.Should().HaveFoundDefaultInstallLocationInConfigFile(path1)
.And.HaveFoundArchSpecificInstallLocationInConfigFile(path1, arch1)
.And.HaveFoundArchSpecificInstallLocationInConfigFile(path2, arch2)
.And.HaveUsedGlobalInstallLocation(path2);
}
}
[Fact]
[SkipOnPlatform(TestPlatforms.Windows, "This test targets the install_location config file which is only used on Linux and macOS.")]
public void InstallLocationFile_OnlyFirstLineMayNotSpecifyArchitecture()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
var appExe = fixture.TestProject.AppExe;
using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(appExe))
{
registeredInstallLocationOverride.SetInstallLocation(new (string, string)[] {
(string.Empty, "a/b/c"),
(string.Empty, "x/y/z"),
});
Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.DotNetRoot(null)
.Execute()
.Should().HaveFoundDefaultInstallLocationInConfigFile("a/b/c")
.And.HaveStdErrContaining($"Only the first line in '{registeredInstallLocationOverride.PathValueOverride}' may not have an architecture prefix.")
.And.HaveUsedConfigFileInstallLocation("a/b/c");
}
}
[Fact]
[SkipOnPlatform(TestPlatforms.Windows, "This test targets the install_location config file which is only used on Linux and macOS.")]
public void InstallLocationFile_ReallyLongInstallPathIsParsedCorrectly()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
var appExe = fixture.TestProject.AppExe;
using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(appExe))
{
var reallyLongPath =
"reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreally" +
"reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreally" +
"reallyreallyreallyreallyreallyreallyreallyreallyreallyreallylongpath";
registeredInstallLocationOverride.SetInstallLocation((string.Empty, reallyLongPath));
Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.DotNetRoot(null)
.Execute()
.Should().HaveFoundDefaultInstallLocationInConfigFile(reallyLongPath)
.And.HaveUsedConfigFileInstallLocation(reallyLongPath);
}
}
[Fact]
[SkipOnPlatform(TestPlatforms.Windows, "This test targets the install_location config file which is only used on Linux and macOS.")]
public void InstallLocationFile_MissingFile()
{
var fixture = sharedTestState.PortableAppFixture.Copy();
var appExe = fixture.TestProject.AppExe;
string testArtifactsPath = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "missingInstallLocation"));
using (new TestArtifact(testArtifactsPath))
using (var testOnlyProductBehavior = TestOnlyProductBehavior.Enable(appExe))
{
Directory.CreateDirectory(testArtifactsPath);
string directory = Path.Combine(testArtifactsPath, "installLocationOverride");
Directory.CreateDirectory(directory);
string nonExistentLocationFile = Path.Combine(directory, "install_location");
string defaultInstallLocation = Path.Combine(testArtifactsPath, "defaultInstallLocation");
Command.Create(appExe)
.CaptureStdErr()
.EnvironmentVariable(
Constants.TestOnlyEnvironmentVariables.InstallLocationFilePath,
nonExistentLocationFile)
.EnvironmentVariable(
Constants.TestOnlyEnvironmentVariables.DefaultInstallPath,
defaultInstallLocation)
.DotNetRoot(null)
.Execute()
.Should().NotHaveStdErrContaining("The install_location file");
}
}
public class SharedTestState : IDisposable
{
public string BaseDirectory { get; }
public TestProjectFixture PortableAppFixture { get; }
public RepoDirectoriesProvider RepoDirectories { get; }
public string InstallLocation { get; }
public SharedTestState()
{
RepoDirectories = new RepoDirectoriesProvider();
var fixture = new TestProjectFixture("PortableApp", RepoDirectories);
fixture
.EnsureRestored()
// App Host generation is turned off by default on macOS
.PublishProject(extraArgs: "/p:UseAppHost=true");
PortableAppFixture = fixture;
BaseDirectory = Path.GetDirectoryName(PortableAppFixture.SdkDotnet.GreatestVersionHostFxrFilePath);
InstallLocation = fixture.BuiltDotnet.BinPath;
}
public void Dispose()
{
PortableAppFixture.Dispose();
}
}
}
}