Skip to content

Commit

Permalink
Add --keep-workspaces Cake parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Feb 20, 2019
1 parent 05532de commit a79573a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
5 changes: 5 additions & 0 deletions KeepWorkspaces.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RunSettings>
<TestRunParameters>
<Parameter name="KeepWorkspaces" value="true" />
</TestRunParameters>
</RunSettings>
9 changes: 8 additions & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,14 @@ Task("Acceptance")
.Description("Ensures that known project configurations can use the produced NuGet package to restore, build, and run tests.")
.Does(() =>
{
VSTest(SRC_DIR + $"NUnit.TestAdapter.Tests.Acceptance/bin/{configuration}/net472/NUnit.VisualStudio.TestAdapter.Tests.Acceptance.dll");
var testAssembly = SRC_DIR + $"NUnit.TestAdapter.Tests.Acceptance/bin/{configuration}/net472/NUnit.VisualStudio.TestAdapter.Tests.Acceptance.dll";

var keepWorkspaces = Argument<bool?>("keep-workspaces", false) ?? true;

VSTest(testAssembly, new VSTestSettings
{
SettingsFile = keepWorkspaces ? (FilePath)"KeepWorkspaces.runsettings" : null
});
});

Task("Appveyor")
Expand Down
28 changes: 16 additions & 12 deletions src/NUnit.TestAdapter.Tests.Acceptance/AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class AcceptanceTests
"netcoreapp1.0"
};

private readonly static Lazy<(IsolatedWorkspaceManager manager, string nupkgVersion)> Initialization = new Lazy<(IsolatedWorkspaceManager, string)>(() =>
private readonly static Lazy<(IsolatedWorkspaceManager manager, string nupkgVersion, bool keepWorkspaces)> Initialization = new Lazy<(IsolatedWorkspaceManager, string, bool)>(() =>
{
var directory = TestContext.Parameters["ProjectWorkspaceDirectory"]
?? TryAutoDetectProjectWorkspaceDirectory()
Expand All @@ -38,19 +38,23 @@ public abstract class AcceptanceTests
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);

return (
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")),
nupkgVersion);
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)
Expand Down Expand Up @@ -93,7 +97,7 @@ public static void TearDown()
test.FullName + " failed:" + Environment.NewLine
+ TestContext.CurrentContext.Result.Message.TrimEnd() + Environment.NewLine);
}
else
else if (!Initialization.Value.keepWorkspaces)
{
foreach (var workspace in workspaces)
Utils.DeleteDirectoryRobust(workspace.Directory);
Expand Down

0 comments on commit a79573a

Please sign in to comment.