Test Infrastructure Updates

Creates a TestDirectory abstraction under TestInstance to manage creation of test-specific working directories
Enables TestAssetManager to create TestDirectory instances
Enables fluent addition of Environment Variables to TestCommand
Adds PathUtility support for ensuring a directory exists
This commit is contained in:
Piotr Puszkiewicz 2016-04-22 12:17:36 -07:00 committed by PiotrP
parent 6c1ef959cc
commit 94e620088e
5 changed files with 183 additions and 2 deletions

View file

@ -59,9 +59,15 @@ namespace Microsoft.DotNet.Tools.Common
public static void EnsureParentDirectory(string filePath)
{
string directory = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directory))
EnsureDirectory(directory);
}
public static void EnsureDirectory(string directoryPath)
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directory);
Directory.CreateDirectory(directoryPath);
}
}

View file

@ -102,5 +102,12 @@ namespace Microsoft.DotNet.TestFramework
var testInstance = new TestInstance(testProjectDir, testDestination);
return testInstance;
}
public TestDirectory CreateTestDirectory([CallerMemberName] string callingMethod = "", string identifier = "")
{
string testDestination = Path.Combine(AppContext.BaseDirectory, callingMethod + identifier);
return new TestDirectory(testDestination);
}
}
}

View file

@ -0,0 +1,40 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace Microsoft.DotNet.TestFramework
{
public class TestDirectory
{
private readonly string _path;
internal TestDirectory(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("path");
}
_path = path;
EnsureExistsAndEmpty(_path);
}
public string Path { get { return _path; } }
private void EnsureExistsAndEmpty(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
Directory.CreateDirectory(path);
}
}
}

View file

@ -0,0 +1,121 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace Microsoft.DotNet.TestFramework
{
public class TestInstance: TestDirectory
{
private string _testAssetRoot;
internal TestInstance(string testAssetRoot, string testDestination) : base(testDestination)
{
if (string.IsNullOrEmpty(testAssetRoot))
{
throw new ArgumentException("testAssetRoot");
}
_testAssetRoot = testAssetRoot;
CopySource();
}
private void CopySource()
{
var sourceDirs = Directory.GetDirectories(_testAssetRoot, "*", SearchOption.AllDirectories)
.Where(dir =>
{
dir = dir.ToLower();
return !dir.EndsWith($"{System.IO.Path.DirectorySeparatorChar}bin")
&& !dir.Contains($"{System.IO.Path.DirectorySeparatorChar}bin{System.IO.Path.DirectorySeparatorChar}")
&& !dir.EndsWith($"{System.IO.Path.DirectorySeparatorChar}obj")
&& !dir.Contains($"{System.IO.Path.DirectorySeparatorChar}obj{System.IO.Path.DirectorySeparatorChar}");
});
foreach (string sourceDir in sourceDirs)
{
Directory.CreateDirectory(sourceDir.Replace(_testAssetRoot, Path));
}
var sourceFiles = Directory.GetFiles(_testAssetRoot, "*.*", SearchOption.AllDirectories)
.Where(file =>
{
file = file.ToLower();
return !file.EndsWith("project.lock.json")
&& !file.Contains($"{System.IO.Path.DirectorySeparatorChar}bin{System.IO.Path.DirectorySeparatorChar}")
&& !file.Contains($"{System.IO.Path.DirectorySeparatorChar}obj{System.IO.Path.DirectorySeparatorChar}");
});
foreach (string srcFile in sourceFiles)
{
string destFile = srcFile.Replace(_testAssetRoot, Path);
File.Copy(srcFile, destFile, true);
FixTimeStamp(srcFile, destFile);
}
}
public TestInstance WithLockFiles()
{
foreach (string lockFile in Directory.GetFiles(_testAssetRoot, "project.lock.json", SearchOption.AllDirectories))
{
string destinationLockFile = lockFile.Replace(_testAssetRoot, Path);
File.Copy(lockFile, destinationLockFile, true);
FixTimeStamp(lockFile, destinationLockFile);
}
return this;
}
public TestInstance WithBuildArtifacts()
{
var binDirs = Directory.GetDirectories(_testAssetRoot, "*", SearchOption.AllDirectories)
.Where(dir =>
{
dir = dir.ToLower();
return dir.EndsWith($"{System.IO.Path.DirectorySeparatorChar}bin")
|| dir.Contains($"{System.IO.Path.DirectorySeparatorChar}bin{System.IO.Path.DirectorySeparatorChar}")
|| dir.EndsWith($"{System.IO.Path.DirectorySeparatorChar}obj")
|| dir.Contains($"{System.IO.Path.DirectorySeparatorChar}obj{System.IO.Path.DirectorySeparatorChar}");
});
foreach (string dirPath in binDirs)
{
Directory.CreateDirectory(dirPath.Replace(_testAssetRoot, Path));
}
var binFiles = Directory.GetFiles(_testAssetRoot, "*.*", SearchOption.AllDirectories)
.Where(file =>
{
file = file.ToLower();
return file.Contains($"{System.IO.Path.DirectorySeparatorChar}bin{System.IO.Path.DirectorySeparatorChar}")
|| file.Contains($"{System.IO.Path.DirectorySeparatorChar}obj{System.IO.Path.DirectorySeparatorChar}");
});
foreach (string binFile in binFiles)
{
string destFile = binFile.Replace(_testAssetRoot, Path);
File.Copy(binFile, destFile, true);
FixTimeStamp(binFile, destFile);
}
return this;
}
public string TestRoot => Path;
private static void FixTimeStamp(string originalFile, string newFile)
{
// workaround for https://github.com/dotnet/corefx/issues/6083
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var originalTime = File.GetLastWriteTime(originalFile);
File.SetLastWriteTime(newFile, originalTime);
}
}
}
}

View file

@ -193,5 +193,12 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
process.Start();
return process;
}
public TestCommand WithEnvironmentVariable(string name, string value)
{
Environment.Add(name, value);
return this;
}
}
}