Refactor TestFramework

Remove TestScenario and add TestAssetsManager which manages the TestAssets
folder.
This commit is contained in:
Sridhar Periyasamy 2016-02-11 14:08:37 -08:00
parent d1d14283a1
commit b239c548a4
4 changed files with 118 additions and 132 deletions

View file

@ -0,0 +1,101 @@
// 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.CompilerServices;
using System.Threading.Tasks;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.TestFramework
{
public class TestAssetsManager
{
public string AssetsRoot
{
get; private set;
}
public TestAssetsManager(string assetsRoot, bool skipRestore = true, bool skipBuild = true)
{
if (!Directory.Exists(assetsRoot))
{
throw new DirectoryNotFoundException($"Directory not found at '{assetsRoot}'");
}
AssetsRoot = assetsRoot;
if (!skipRestore)
{
Restore();
}
if (!skipBuild)
{
Build();
}
}
private void Restore()
{
string[] restoreArgs = new string[] { "restore", AssetsRoot };
Console.WriteLine("Executing - 'dotnet {0}'", string.Join(" ", restoreArgs));
var commandResult = Command.Create("dotnet", restoreArgs)
.CaptureStdOut()
.CaptureStdErr()
.Execute();
int exitCode = commandResult.ExitCode;
if (exitCode != 0)
{
Console.WriteLine(commandResult.StdOut);
Console.WriteLine(commandResult.StdErr);
string message = string.Format("Command Failed - 'dotnet {0}' with exit code - {1}", string.Join(" ", restoreArgs), exitCode);
throw new Exception(message);
}
}
private void Build()
{
var projects = Directory.GetFiles(AssetsRoot, "project.json", SearchOption.AllDirectories);
foreach (var project in projects)
{
string[] buildArgs = new string[] { "build", project };
Console.WriteLine("Executing - 'dotnet {0}'", string.Join(" ", buildArgs));
var commandResult = Command.Create("dotnet", buildArgs)
.CaptureStdOut()
.CaptureStdErr()
.Execute();
int exitCode = commandResult.ExitCode;
if (exitCode != 0)
{
Console.WriteLine(commandResult.StdOut);
Console.WriteLine(commandResult.StdErr);
string message = string.Format("Command Failed - 'dotnet {0}' with exit code - {1}", string.Join(" ", buildArgs), exitCode);
throw new Exception(message);
}
}
}
public TestInstance CreateTestInstance(string testProjectName, [CallerMemberName] string callingMethod = "", string identifier = "")
{
string testProjectDir = Path.Combine(AssetsRoot, testProjectName);
if (!Directory.Exists(testProjectDir))
{
throw new Exception($"Cannot find '{testProjectName}' at '{AssetsRoot}'");
}
string testDestination = Path.Combine(AppContext.BaseDirectory, callingMethod + identifier, testProjectName);
var testInstance = new TestInstance(testProjectDir, testDestination);
return testInstance;
}
}
}

View file

@ -12,13 +12,13 @@ namespace Microsoft.DotNet.TestFramework
public class TestInstance
{
private string _testDestination;
private TestScenario _testScenario;
private string _testAssetRoot;
internal TestInstance(TestScenario testScenario, string testDestination)
internal TestInstance(string testAssetRoot, string testDestination)
{
if (testScenario == null)
if (string.IsNullOrEmpty(testAssetRoot))
{
throw new ArgumentNullException("testScenario");
throw new ArgumentException("testScenario");
}
if (string.IsNullOrEmpty(testDestination))
@ -26,7 +26,7 @@ namespace Microsoft.DotNet.TestFramework
throw new ArgumentException("testDestination");
}
_testScenario = testScenario;
_testAssetRoot = testAssetRoot;
_testDestination = testDestination;
if (Directory.Exists(testDestination))
@ -40,7 +40,7 @@ namespace Microsoft.DotNet.TestFramework
private void CopySource()
{
var sourceDirs = Directory.GetDirectories(_testScenario.SourceRoot, "*", SearchOption.AllDirectories)
var sourceDirs = Directory.GetDirectories(_testAssetRoot, "*", SearchOption.AllDirectories)
.Where(dir =>
{
dir = dir.ToLower();
@ -50,10 +50,10 @@ namespace Microsoft.DotNet.TestFramework
foreach (string sourceDir in sourceDirs)
{
Directory.CreateDirectory(sourceDir.Replace(_testScenario.SourceRoot, _testDestination));
Directory.CreateDirectory(sourceDir.Replace(_testAssetRoot, _testDestination));
}
var sourceFiles = Directory.GetFiles(_testScenario.SourceRoot, "*.*", SearchOption.AllDirectories)
var sourceFiles = Directory.GetFiles(_testAssetRoot, "*.*", SearchOption.AllDirectories)
.Where(file =>
{
file = file.ToLower();
@ -63,24 +63,24 @@ namespace Microsoft.DotNet.TestFramework
foreach (string srcFile in sourceFiles)
{
File.Copy(srcFile, srcFile.Replace(_testScenario.SourceRoot, _testDestination), true);
File.Copy(srcFile, srcFile.Replace(_testAssetRoot, _testDestination), true);
}
}
public TestInstance WithLockFiles()
{
foreach (string lockFile in Directory.GetFiles(_testScenario.SourceRoot, "project.lock.json", SearchOption.AllDirectories))
foreach (string lockFile in Directory.GetFiles(_testAssetRoot, "project.lock.json", SearchOption.AllDirectories))
{
string destinationLockFile = lockFile.Replace(_testScenario.SourceRoot, _testDestination);
string destinationLockFile = lockFile.Replace(_testAssetRoot, _testDestination);
File.Copy(lockFile, destinationLockFile, true);
}
return this;
}
public TestInstance WithBinaries()
public TestInstance WithBuildArtifacts()
{
var binDirs = Directory.GetDirectories(_testScenario.SourceRoot, "*", SearchOption.AllDirectories)
var binDirs = Directory.GetDirectories(_testAssetRoot, "*", SearchOption.AllDirectories)
.Where(dir =>
{
dir = dir.ToLower();
@ -90,10 +90,10 @@ namespace Microsoft.DotNet.TestFramework
foreach (string dirPath in binDirs)
{
Directory.CreateDirectory(dirPath.Replace(_testScenario.SourceRoot, _testDestination));
Directory.CreateDirectory(dirPath.Replace(_testAssetRoot, _testDestination));
}
var binFiles = Directory.GetFiles(_testScenario.SourceRoot, "*.*", SearchOption.AllDirectories)
var binFiles = Directory.GetFiles(_testAssetRoot, "*.*", SearchOption.AllDirectories)
.Where(file =>
{
file = file.ToLower();
@ -102,7 +102,7 @@ namespace Microsoft.DotNet.TestFramework
foreach (string binFile in binFiles)
{
File.Copy(binFile, binFile.Replace(_testScenario.SourceRoot, _testDestination), true);
File.Copy(binFile, binFile.Replace(_testAssetRoot, _testDestination), true);
}
return this;

View file

@ -1,115 +0,0 @@
// 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.CompilerServices;
using System.Threading.Tasks;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.TestFramework
{
public class TestScenario
{
private static IDictionary<string, TestScenario> _scenarioCache = new Dictionary<string, TestScenario>();
private TestScenario(string testSourceRoot, bool skipRestore, bool skipBuild)
{
SourceRoot = testSourceRoot;
Projects = GetAllProjects(SourceRoot);
if (!skipRestore)
{
Restore();
}
if (!skipBuild)
{
Build();
}
}
private IEnumerable<string> Projects
{
get; set;
}
public string SourceRoot
{
get;
private set;
}
public static TestScenario Create(string testSourceRoot, bool skipRestore = false, bool skipBuild = false)
{
TestScenario testScenario;
lock (_scenarioCache)
{
if (!_scenarioCache.TryGetValue(testSourceRoot, out testScenario))
{
testScenario = new TestScenario(testSourceRoot, skipRestore, skipBuild);
_scenarioCache.Add(testSourceRoot, testScenario);
}
}
return testScenario;
}
public TestInstance CreateTestInstance([CallerMemberName] string callingMethod = "", string identifier = "")
{
string projectName = new DirectoryInfo(SourceRoot).Name;
string testDestination = Path.Combine(AppContext.BaseDirectory, callingMethod + identifier, projectName);
var testInstance = new TestInstance(this, testDestination);
return testInstance;
}
internal void Build()
{
foreach (var project in Projects)
{
string[] buildArgs = new string[] { "build", project };
var commandResult = Command.Create("dotnet", buildArgs)
.CaptureStdOut()
.CaptureStdErr()
.Execute();
Console.WriteLine(commandResult.StdOut);
Console.WriteLine(commandResult.StdErr);
int exitCode = commandResult.ExitCode;
if (exitCode != 0)
{
string message = string.Format("Command Failed - 'dotnet {0}' with exit code - {1}", string.Join(" ", buildArgs), exitCode);
throw new Exception(message);
}
}
}
private static IEnumerable<string> GetAllProjects(string sourceRoot)
{
return Directory.GetFiles(sourceRoot, "project.json", SearchOption.AllDirectories);
}
internal void Restore()
{
string[] restoreArgs = new string[] { "restore", SourceRoot };
var commandResult = Command.Create("dotnet", restoreArgs)
.CaptureStdOut()
.CaptureStdErr()
.Execute();
Console.WriteLine(commandResult.StdOut);
Console.WriteLine(commandResult.StdErr);
int exitCode = commandResult.ExitCode;
if (exitCode != 0)
{
string message = string.Format("Command Failed - 'dotnet {0}' with exit code - {1}", string.Join(" ", restoreArgs), exitCode);
throw new Exception(message);
}
}
}
}

View file

@ -8,7 +8,7 @@
"dependencies": {
"Microsoft.DotNet.Cli.Utils": "1.0.0-*",
"NETStandard.Library": "1.0.0-rc2-23728"
"NETStandard.Library": "1.0.0-rc2-23811"
},
"frameworks": {