forked from microsoft/vstest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcceptanceTestBase.cs
More file actions
189 lines (161 loc) · 9.59 KB
/
AcceptanceTestBase.cs
File metadata and controls
189 lines (161 loc) · 9.59 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Microsoft.TestPlatform.TestUtilities;
public class AcceptanceTestBase : IntegrationTestBase
{
public const string DesktopTargetFramework = Net462TargetFramework;
public const string Net462TargetFramework = "net462";
public const string Net47TargetFramework = "net47";
public const string Net471TargetFramework = "net471";
public const string Net472TargetFramework = "net472";
public const string Net48TargetFramework = "net48";
public const string Core80TargetFramework = "net8.0";
public const string Core90TargetFramework = "net9.0";
public const string Core10TargetFramework = "net10.0";
public const string Core11TargetFramework = "net11.0";
public const string DesktopFrameworkArgValue = Net462FrameworkArgValue;
public const string Net462FrameworkArgValue = ".NETFramework,Version=v4.6.2";
public const string Net47FrameworkArgValue = ".NETFramework,Version=v4.7";
public const string Net471FrameworkArgValue = ".NETFramework,Version=v4.7.1";
public const string Net472FrameworkArgValue = ".NETFramework,Version=v4.7.2";
public const string Net48FrameworkArgValue = ".NETFramework,Version=v4.8";
public const string Core80FrameworkArgValue = ".NETCoreApp,Version=v8.0";
public const string Core90FrameworkArgValue = ".NETCoreApp,Version=v9.0";
public const string Core10FrameworkArgValue = ".NETCoreApp,Version=v10.0";
public const string Core11FrameworkArgValue = ".NETCoreApp,Version=v11.0";
public const string DesktopRunnerTargetRuntime = "win7-x64";
public const string CoreRunnerTargetRuntime = "";
public const string InIsolation = "/InIsolation";
public const string NETFX462_48 = "net462;net472;net48";
public const string NETFX462_NET11 = "net462;net472;net48;net8.0;net9.0;net10.0;net11.0";
public const string DEFAULT_RUNNER_NETFX = Net48TargetFramework;
public const string DEFAULT_HOST_NETFX = Net462TargetFramework;
public const string DEFAULT_RUNNER_NETCORE = Core80TargetFramework;
public const string DEFAULT_HOST_NETCORE = Core80TargetFramework;
/// <summary>
/// Our current defaults for .NET and .NET Framework.
/// </summary>
public const string DEFAULT_HOST_NETFX_AND_NET = "net462;net8.0";
public const string DEFAULT_HOST_NET = "net8.0";
public const string DEFAULT_RUNNER_NETFX_AND_NET = "net48;net10.0";
public const string DEFAULT_RUNNER_NET = "net10.0";
public const string LATEST_TO_LEGACY = "Latest;LatestPreview;LatestStable;RecentStable;MostDownloaded;PreviousStable;LegacyStable";
public const string LATEST_TO_RECENT_STABLE = "Latest;LatestPreview;LatestStable;RecentStable";
public const string LATESTPREVIEW_TO_LEGACY = "LatestPreview;LatestStable;RecentStable;MostDownloaded;PreviousStable;LegacyStable";
public const string LATEST = "Latest";
// "Special" version for runner, to take the latest from VSIX we don't ship any other component that way, so we need separate value to control it.
public const string LATESTVSIX = "LatestVsix";
public const string LATESTSTABLE = "LatestStable";
internal const string MSTEST = "MSTest";
public static string And(string left, string right)
{
return string.Join(";", left, right);
}
protected string FrameworkArgValue => DeriveFrameworkArgValue(_testEnvironment);
protected static void SetTestEnvironment(IntegrationTestEnvironment testEnvironment, RunnerInfo runnerInfo)
{
testEnvironment.VSTestConsoleInfo = runnerInfo.VSTestConsoleInfo;
// The order here matters, it changes how the resulting path is built when we resolve test dlls and other assets.
testEnvironment.DllInfos = new[] { runnerInfo.TestHostInfo, runnerInfo.AdapterInfo }.Where(d => d != null).Select(x => x!).ToList();
testEnvironment.DebugInfo = runnerInfo.DebugInfo;
testEnvironment.RunnerFramework = runnerInfo.RunnerFramework!;
testEnvironment.TargetFramework = runnerInfo.TargetFramework;
testEnvironment.InIsolationValue = runnerInfo.InIsolationValue;
}
protected static string DeriveFrameworkArgValue(IntegrationTestEnvironment testEnvironment)
=> testEnvironment.TargetFramework switch
{
Core80TargetFramework => Core80FrameworkArgValue,
Core90TargetFramework => Core90FrameworkArgValue,
Core10TargetFramework => Core10FrameworkArgValue,
Core11TargetFramework => Core11FrameworkArgValue,
Net462TargetFramework => Net462FrameworkArgValue,
Net47TargetFramework => Net47FrameworkArgValue,
Net471TargetFramework => Net471FrameworkArgValue,
Net472TargetFramework => Net472FrameworkArgValue,
Net48TargetFramework => Net48FrameworkArgValue,
_ => throw new NotSupportedException($"{testEnvironment.TargetFramework} is not supported TargetFramework value."),
};
protected bool IsDesktopTargetFramework()
=> _testEnvironment.TargetFramework == DesktopTargetFramework;
protected string GetTargetFrameworkForRunsettings()
{
string targetFramework = _testEnvironment.TargetFramework == DesktopTargetFramework ? "Framework45" : "FrameworkCore10";
return targetFramework;
}
/// <summary>
/// Empty runsettings, just with the RunSettings tag that we require.
/// </summary>
/// <returns></returns>
public static string GetEmptyRunsettings()
{
return "<RunSettings></RunSettings>";
}
/// <summary>
/// Almost empty runsettings, just specifying the target framework from the currently set test environment.
/// </summary>
public string GetDefaultRunSettings()
{
return GetRunSettingsWithTargetFramework(FrameworkArgValue);
}
/// <summary>
/// Almost empty runsettings, just specifying the given target framework.
/// Use the overload without any parameters to get the target framework from the currently set test environment.
/// </summary>
/// <returns></returns>
public static string GetRunSettingsWithTargetFramework(string targetFramework)
{
string runSettingsXml =
$@"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<TargetFrameworkVersion>{targetFramework}</TargetFrameworkVersion>
</RunConfiguration>
</RunSettings>";
return runSettingsXml;
}
protected string GetIsolatedTestAsset(string assetName, string targetFramework)
{
var projectPath = GetProjectFullPath(assetName);
foreach (var file in new FileInfo(projectPath).Directory!.EnumerateFiles())
{
TempDirectory.CopyFile(file.FullName);
if (file.Extension.Equals(".csproj", StringComparison.OrdinalIgnoreCase))
{
// Build just for the given tfm
var projFile = Path.Combine(TempDirectory.Path, Path.GetFileName(file.FullName));
var csprojContent = File.ReadAllText(projFile);
csprojContent = Regex.Replace(csprojContent, "<TargetFramework>.*?</TargetFramework>", $"<TargetFramework>{targetFramework}</TargetFramework>");
csprojContent = Regex.Replace(csprojContent, "<TargetFrameworks>.*?</TargetFrameworks>", $"<TargetFramework>{targetFramework}</TargetFramework>");
File.WriteAllText(projFile, csprojContent);
string root = IntegrationTestEnvironment.RepoRootDirectory;
var testAssetsRoot = Path.GetFullPath(Path.Combine(root, "test", "TestAssets"));
// Replace $(RepoRoot) with the path to temp directory in which we put the csproj,
// and write it into Directory.Build.props
var directoryBuildProps = Path.Combine(testAssetsRoot, "Directory.Build.props");
var newDirectoryBuildProps = Path.Combine(TempDirectory.Path, "Directory.Build.props");
var content = File.ReadAllText(directoryBuildProps).Replace("$(RepoRoot)", TempDirectory.Path + "/");
File.WriteAllText(newDirectoryBuildProps, content);
// Copy Directory.Build.targets
TempDirectory.CopyFile(Path.Combine(testAssetsRoot, "Directory.Build.targets"));
Directory.CreateDirectory(Path.Combine(TempDirectory.Path, "eng"));
File.Copy(Path.Combine(root, "eng", "Versions.props"),
Path.Combine(TempDirectory.Path, "eng", "Versions.props"));
File.Copy(Path.Combine(root, "eng", "Version.Details.props"),
Path.Combine(TempDirectory.Path, "eng", "Version.Details.props"));
// Copy NuGet.config
var nugetContent = File.ReadAllText(Path.Combine(root, "NuGet.config"))
// Point packages folder to vstest's local .packages cache used by test assets.
.Replace("</config>", $"""<add key="globalPackagesFolder" value="{IntegrationTestEnvironment.TestAssetsNuGetCacheDirectory}" /></config>""")
// and add local package source
.Replace("</packageSources>", $"""<add key="localy-built-packages" value="{IntegrationTestEnvironment.LocalPackageSource}" /></packageSources>""");
File.WriteAllText(Path.Combine(TempDirectory.Path, "NuGet.config"), nugetContent);
}
}
return Path.Combine(TempDirectory.Path, assetName);
}
}