/
AcceptanceTests.cs
178 lines (142 loc) · 6.76 KB
/
AcceptanceTests.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.VisualStudio.TestAdapter.Tests.Acceptance.WorkspaceTools;
namespace NUnit.VisualStudio.TestAdapter.Tests.Acceptance
{
[Category("Acceptance")]
public abstract class AcceptanceTests
{
public static string NuGetPackageId { get; } = "NUnit3TestAdapter";
public static string NuGetPackageVersion => Initialization.Value.nupkgVersion;
public const string LowestNetfxTarget = "net35";
public const string LegacyProjectTargetFrameworkVersion = "v3.5";
public static IEnumerable<string> TargetFrameworks => new[]
{
LowestNetfxTarget,
"netcoreapp2.1"
};
public static IEnumerable<string> DotNetCliTargetFrameworks => new[]
{
"netcoreapp2.1",
"netcoreapp3.1"
};
private static readonly Lazy<(IsolatedWorkspaceManager manager, string nupkgVersion, bool keepWorkspaces)> Initialization = new (() =>
{
var directory = TestContext.Parameters["ProjectWorkspaceDirectory"]
?? TryAutoDetectProjectWorkspaceDirectory()
?? throw new InvalidOperationException("The test parameter ProjectWorkspaceDirectory must be set in order to run this test.");
var nupkgDirectory = TestContext.Parameters["TestNupkgDirectory"]
?? TryAutoDetectTestNupkgDirectory(NuGetPackageId)
?? throw new InvalidOperationException("The test parameter TestNupkgDirectory must be set in order to run this test.");
var nupkgVersion = TryGetTestNupkgVersion(nupkgDirectory, packageId: NuGetPackageId)
?? throw new InvalidOperationException($"No NuGet package with the ID {NuGetPackageId} was found in {nupkgDirectory}.");
var keepWorkspaces = TestContext.Parameters.Get("KeepWorkspaces", defaultValue: false);
var packageCachePath = Path.Combine(directory, ".isolatednugetcache");
ClearCachedTestNupkgs(packageCachePath);
var manager = new IsolatedWorkspaceManager(
reason: string.Join(
Environment.NewLine,
"Test assembly: " + typeof(AcceptanceTests).Assembly.Location,
"Runner process: " + Process.GetCurrentProcess().MainModule.FileName),
directory,
nupkgDirectory,
packageCachePath,
downloadCachePath: Path.Combine(directory, ".toolcache"));
if (keepWorkspaces) manager.PreserveDirectory("The KeepWorkspaces test parameter was set to true.");
return (manager, nupkgVersion, keepWorkspaces);
});
private static void ClearCachedTestNupkgs(string packageCachePath)
{
Utils.DeleteDirectoryRobust(Path.Combine(packageCachePath, NuGetPackageId));
}
private static readonly Dictionary<string, List<IsolatedWorkspace>> WorkspacesByTestId = new ();
protected static IsolatedWorkspace CreateWorkspace()
{
var test = TestContext.CurrentContext?.Test ?? throw new InvalidOperationException("There is no current test.");
var workspace = Initialization.Value.manager.CreateWorkspace(test.Name);
lock (WorkspacesByTestId)
{
if (!WorkspacesByTestId.TryGetValue(test.ID, out var workspaces))
WorkspacesByTestId.Add(test.ID, workspaces = new List<IsolatedWorkspace>());
workspaces.Add(workspace);
}
return workspace;
}
protected static void InconclusiveOnException(Action action)
{
Assume.That(action.Invoke, Throws.Nothing);
}
[TearDown]
public static void TearDown()
{
var test = TestContext.CurrentContext?.Test ?? throw new InvalidOperationException("There is no current test.");
List<IsolatedWorkspace> workspaces;
lock (WorkspacesByTestId)
{
if (!WorkspacesByTestId.TryGetValue(test.ID, out workspaces))
return;
WorkspacesByTestId.Remove(test.ID);
}
foreach (var workspace in workspaces)
workspace.Dispose();
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
{
Initialization.Value.manager.PreserveDirectory(
test.FullName + " failed:" + Environment.NewLine
+ TestContext.CurrentContext.Result.Message.TrimEnd() + Environment.NewLine);
}
else if (!Initialization.Value.keepWorkspaces)
{
foreach (var workspace in workspaces)
Utils.DeleteDirectoryRobust(workspace.Directory);
}
}
internal static void OnGlobalTeardown()
{
if (!Initialization.IsValueCreated) return;
Initialization.Value.manager.Dispose();
}
private static string TryAutoDetectProjectWorkspaceDirectory()
{
for (var directory = TestContext.CurrentContext.TestDirectory; directory != null; directory = Path.GetDirectoryName(directory))
{
if (File.Exists(Path.Combine(directory, "build.cake")))
{
return Path.Combine(directory, ".acceptance");
}
}
return null;
}
private static string TryAutoDetectTestNupkgDirectory(string packageId)
{
// Keep in sync with build.cake.
for (var directory = TestContext.CurrentContext.TestDirectory; directory != null; directory = Path.GetDirectoryName(directory))
{
var packagePath = Path.Combine(directory, "package");
try
{
if (Directory.EnumerateFiles(Path.Combine(directory, "package"), packageId + ".*.nupkg").Any())
return packagePath;
}
catch (DirectoryNotFoundException)
{
}
}
return null;
}
private static string TryGetTestNupkgVersion(string directory, string packageId)
{
var dir = new DirectoryInfo(directory);
var packages = dir.EnumerateFiles(packageId + ".*.nupkg").ToList();
var selected = packages.Count > 1 ? packages.OrderByDescending(f => f.LastWriteTime).First() : packages.SingleOrDefault();
var path = selected?.FullName;
return path is null ? null :
Path.GetFileNameWithoutExtension(path).Substring(packageId.Length + 1);
}
}
}