2015-12-15 01:39:29 +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;
|
2016-01-14 19:52:54 +00:00
|
|
|
|
using System.IO;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-01-14 19:52:54 +00:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-02-11 22:17:20 +00:00
|
|
|
|
using Microsoft.DotNet.TestFramework;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
|
{
|
2016-02-11 22:17:20 +00:00
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base class for all unit test classes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class TestBase : IDisposable
|
|
|
|
|
{
|
2016-03-01 23:35:32 +00:00
|
|
|
|
protected const string DefaultFramework = "netstandardapp1.5";
|
2015-12-15 01:39:29 +00:00
|
|
|
|
private TempRoot _temp;
|
2016-02-11 22:17:20 +00:00
|
|
|
|
private static TestAssetsManager s_testsAssetsMgr;
|
|
|
|
|
private static string s_repoRoot;
|
|
|
|
|
|
|
|
|
|
protected static string RepoRoot
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(s_repoRoot))
|
|
|
|
|
{
|
|
|
|
|
return s_repoRoot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string directory = AppContext.BaseDirectory;
|
|
|
|
|
|
|
|
|
|
while (!Directory.Exists(Path.Combine(directory, ".git")) && directory != null)
|
|
|
|
|
{
|
|
|
|
|
directory = Directory.GetParent(directory).FullName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (directory == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Cannot find the git repository root");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s_repoRoot = directory;
|
|
|
|
|
return s_repoRoot;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static TestAssetsManager TestAssetsManager
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (s_testsAssetsMgr == null)
|
|
|
|
|
{
|
|
|
|
|
string assetsRoot = Path.Combine(RepoRoot, "TestAssets", "TestProjects");
|
|
|
|
|
s_testsAssetsMgr = new TestAssetsManager(assetsRoot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s_testsAssetsMgr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-15 01:39:29 +00:00
|
|
|
|
|
|
|
|
|
protected TestBase()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetUniqueName()
|
|
|
|
|
{
|
|
|
|
|
return Guid.NewGuid().ToString("D");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TempRoot Temp
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_temp == null)
|
|
|
|
|
{
|
|
|
|
|
_temp = new TempRoot();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Dispose()
|
|
|
|
|
{
|
2016-01-08 19:03:14 +00:00
|
|
|
|
if (_temp != null && !PreserveTemp())
|
|
|
|
|
{
|
2015-12-15 01:39:29 +00:00
|
|
|
|
_temp.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-08 19:03:14 +00:00
|
|
|
|
|
|
|
|
|
// Quick-n-dirty way to allow the temp output to be preserved when running tests
|
|
|
|
|
private bool PreserveTemp()
|
|
|
|
|
{
|
|
|
|
|
var val = Environment.GetEnvironmentVariable("DOTNET_TEST_PRESERVE_TEMP");
|
|
|
|
|
return !string.IsNullOrEmpty(val) && (
|
|
|
|
|
string.Equals("true", val, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
string.Equals("1", val, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
string.Equals("on", val, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
2016-01-14 19:52:54 +00:00
|
|
|
|
|
2016-01-26 14:39:13 +00:00
|
|
|
|
protected void TestExecutable(string outputDir,
|
2016-01-20 23:41:46 +00:00
|
|
|
|
string executableName,
|
2016-01-26 14:39:13 +00:00
|
|
|
|
string expectedOutput)
|
2016-01-14 19:52:54 +00:00
|
|
|
|
{
|
2016-01-26 14:39:13 +00:00
|
|
|
|
var executablePath = Path.Combine(outputDir, executableName);
|
2016-01-14 19:52:54 +00:00
|
|
|
|
|
|
|
|
|
var executableCommand = new TestCommand(executablePath);
|
|
|
|
|
|
|
|
|
|
var result = executableCommand.ExecuteWithCapturedOutput("");
|
|
|
|
|
|
|
|
|
|
result.Should().HaveStdOut(expectedOutput);
|
|
|
|
|
result.Should().NotHaveStdErr();
|
2016-02-11 22:17:20 +00:00
|
|
|
|
result.Should().Pass();
|
2016-01-26 14:39:13 +00:00
|
|
|
|
}
|
2016-02-11 22:17:20 +00:00
|
|
|
|
|
2016-01-26 14:39:13 +00:00
|
|
|
|
protected void TestOutputExecutable(
|
|
|
|
|
string outputDir,
|
|
|
|
|
string executableName,
|
|
|
|
|
string expectedOutput,
|
|
|
|
|
bool native = false)
|
|
|
|
|
{
|
|
|
|
|
TestExecutable(GetCompilationOutputPath(outputDir, native), executableName, expectedOutput);
|
2016-01-14 19:52:54 +00:00
|
|
|
|
}
|
2016-01-20 23:41:46 +00:00
|
|
|
|
|
2016-01-21 23:01:21 +00:00
|
|
|
|
protected void TestNativeOutputExecutable(string outputDir, string executableName, string expectedOutput)
|
2016-01-20 23:41:46 +00:00
|
|
|
|
{
|
|
|
|
|
TestOutputExecutable(outputDir, executableName, expectedOutput, true);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 23:01:21 +00:00
|
|
|
|
protected string GetCompilationOutputPath(string outputDir, bool native)
|
2016-01-20 23:41:46 +00:00
|
|
|
|
{
|
2016-02-03 18:57:25 +00:00
|
|
|
|
var executablePath = outputDir;
|
2016-01-20 23:41:46 +00:00
|
|
|
|
if (native)
|
|
|
|
|
{
|
2016-02-03 18:57:25 +00:00
|
|
|
|
executablePath = Path.Combine(executablePath, "native");
|
2016-01-20 23:41:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return executablePath;
|
|
|
|
|
}
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|