2016-10-28 01:46:43 +00:00
|
|
|
|
// 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 TestAssets
|
|
|
|
|
{
|
|
|
|
|
private DirectoryInfo _root;
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
private FileInfo _dotnetCsprojExe;
|
|
|
|
|
|
2017-08-30 00:59:34 +00:00
|
|
|
|
private string _testWorkingFolder;
|
2017-01-06 09:40:26 +00:00
|
|
|
|
|
2017-08-30 00:59:34 +00:00
|
|
|
|
public FileInfo DotnetCsprojExe => _dotnetCsprojExe;
|
2017-01-06 09:40:26 +00:00
|
|
|
|
|
2017-08-30 00:59:34 +00:00
|
|
|
|
public TestAssets(DirectoryInfo assetsRoot, FileInfo dotnetCsprojExe, string testWorkingFolder)
|
2016-10-28 01:46:43 +00:00
|
|
|
|
{
|
2017-01-06 09:40:26 +00:00
|
|
|
|
if (assetsRoot == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(assetsRoot));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dotnetCsprojExe == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(dotnetCsprojExe));
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
if (!assetsRoot.Exists)
|
|
|
|
|
{
|
|
|
|
|
throw new DirectoryNotFoundException($"Directory not found at '{assetsRoot}'");
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
if (!dotnetCsprojExe.Exists)
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("Csproj dotnet executable must exist", dotnetCsprojExe.FullName);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_root = assetsRoot;
|
2017-01-06 09:40:26 +00:00
|
|
|
|
|
|
|
|
|
_dotnetCsprojExe = dotnetCsprojExe;
|
2017-08-30 00:59:34 +00:00
|
|
|
|
_testWorkingFolder = testWorkingFolder;
|
2016-10-28 01:46:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TestAssetInfo Get(string name)
|
|
|
|
|
{
|
|
|
|
|
return Get(TestAssetKinds.TestProjects, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TestAssetInfo Get(string kind, string name)
|
|
|
|
|
{
|
|
|
|
|
var assetDirectory = new DirectoryInfo(Path.Combine(_root.FullName, kind, name));
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
return new TestAssetInfo(
|
|
|
|
|
assetDirectory,
|
|
|
|
|
name,
|
2017-08-30 00:59:34 +00:00
|
|
|
|
this);
|
2017-01-06 09:40:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 18:26:53 +00:00
|
|
|
|
public DirectoryInfo CreateTestDirectory(string testProjectName = "", [CallerMemberName] string callingMethod = "", string identifier = "")
|
2017-01-06 09:40:26 +00:00
|
|
|
|
{
|
2017-02-15 23:35:03 +00:00
|
|
|
|
var testDestination = GetTestDestinationDirectoryPath(testProjectName, callingMethod, identifier);
|
2017-01-06 09:40:26 +00:00
|
|
|
|
|
|
|
|
|
var testDirectory = new DirectoryInfo(testDestination);
|
|
|
|
|
|
|
|
|
|
testDirectory.EnsureExistsAndEmpty();
|
|
|
|
|
|
|
|
|
|
return testDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetTestDestinationDirectoryPath(string testProjectName, string callingMethod, string identifier)
|
|
|
|
|
{
|
|
|
|
|
#if NET451
|
|
|
|
|
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
#else
|
|
|
|
|
string baseDirectory = AppContext.BaseDirectory;
|
|
|
|
|
#endif
|
2017-08-30 00:59:34 +00:00
|
|
|
|
// Find the name of the assembly the test comes from based on the the base directory and how the output path has been constructed
|
|
|
|
|
string testAssemblyName = new DirectoryInfo(baseDirectory).Parent.Parent.Name;
|
|
|
|
|
|
2018-10-29 18:26:53 +00:00
|
|
|
|
string directory = Path.Combine(_testWorkingFolder, testAssemblyName, callingMethod + identifier);
|
|
|
|
|
if (!string.IsNullOrEmpty(testProjectName))
|
|
|
|
|
{
|
|
|
|
|
directory = Path.Combine(directory, testProjectName);
|
|
|
|
|
}
|
|
|
|
|
return directory;
|
2016-10-28 01:46:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|