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;
|
2017-07-18 15:34:08 +00:00
|
|
|
|
using System.Globalization;
|
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-04-13 00:29:07 +00:00
|
|
|
|
protected const string DefaultFramework = "netcoreapp1.0";
|
|
|
|
|
protected const string DefaultLibraryFramework = "netstandard1.5";
|
2017-03-17 06:15:45 +00:00
|
|
|
|
protected const string ConsoleLoggerOutputNormal = "--logger console;verbosity=normal";
|
2015-12-15 01:39:29 +00:00
|
|
|
|
private TempRoot _temp;
|
2016-10-28 01:46:43 +00:00
|
|
|
|
private static TestAssets s_testAssets;
|
2016-10-11 00:13:46 +00:00
|
|
|
|
|
2017-07-18 15:34:08 +00:00
|
|
|
|
static TestBase()
|
|
|
|
|
{
|
|
|
|
|
// set culture of test process to match CLI sub-processes when the UI language is overriden.
|
|
|
|
|
string overriddenUILanguage = Environment.GetEnvironmentVariable("DOTNET_CLI_UI_LANGUAGE");
|
|
|
|
|
if (overriddenUILanguage != null)
|
|
|
|
|
{
|
|
|
|
|
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(overriddenUILanguage);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 22:17:20 +00:00
|
|
|
|
|
|
|
|
|
protected static string RepoRoot
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-10-11 00:13:46 +00:00
|
|
|
|
return RepoDirectoriesProvider.RepoRoot;
|
2016-02-11 22:17:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
protected static TestAssets TestAssets
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (s_testAssets == null)
|
|
|
|
|
{
|
|
|
|
|
var assetsRoot = Path.Combine(RepoRoot, "TestAssets");
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
s_testAssets = new TestAssets(
|
|
|
|
|
new DirectoryInfo(assetsRoot),
|
2017-03-17 00:05:18 +00:00
|
|
|
|
new FileInfo(new Muxer().MuxerPath),
|
2017-01-06 09:40:26 +00:00
|
|
|
|
new FileInfo(new RepoDirectoriesProvider().PjDotnet));
|
2016-10-28 01:46:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s_testAssets;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-04-02 04:52:08 +00:00
|
|
|
|
protected CommandResult 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-03-17 22:24:36 +00:00
|
|
|
|
var args = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (IsPortable(executablePath))
|
|
|
|
|
{
|
2016-03-17 23:39:48 +00:00
|
|
|
|
args.Add("exec");
|
2016-03-17 22:24:36 +00:00
|
|
|
|
args.Add(ArgumentEscaper.EscapeSingleArg(executablePath));
|
|
|
|
|
|
|
|
|
|
var muxer = new Muxer();
|
|
|
|
|
executablePath = muxer.MuxerPath;
|
|
|
|
|
}
|
2016-01-14 19:52:54 +00:00
|
|
|
|
|
|
|
|
|
var executableCommand = new TestCommand(executablePath);
|
|
|
|
|
|
2016-03-17 22:24:36 +00:00
|
|
|
|
var result = executableCommand.ExecuteWithCapturedOutput(string.Join(" ", args));
|
2016-01-14 19:52:54 +00:00
|
|
|
|
|
2016-04-02 04:52:08 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(expectedOutput))
|
|
|
|
|
{
|
|
|
|
|
result.Should().HaveStdOut(expectedOutput);
|
|
|
|
|
}
|
2016-01-14 19:52:54 +00:00
|
|
|
|
result.Should().NotHaveStdErr();
|
2016-02-11 22:17:20 +00:00
|
|
|
|
result.Should().Pass();
|
2016-04-02 04:52:08 +00:00
|
|
|
|
return result;
|
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;
|
|
|
|
|
}
|
2016-03-17 22:24:36 +00:00
|
|
|
|
|
|
|
|
|
private bool IsPortable(string executablePath)
|
|
|
|
|
{
|
2016-03-17 23:39:48 +00:00
|
|
|
|
var commandDir = Path.GetDirectoryName(executablePath);
|
2016-03-17 22:24:36 +00:00
|
|
|
|
|
2016-03-17 23:39:48 +00:00
|
|
|
|
var runtimeConfigPath = Directory.EnumerateFiles(commandDir)
|
2016-03-17 22:24:36 +00:00
|
|
|
|
.FirstOrDefault(x => x.EndsWith("runtimeconfig.json"));
|
|
|
|
|
|
2016-03-17 23:39:48 +00:00
|
|
|
|
if (runtimeConfigPath == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var runtimeConfig = new RuntimeConfig(runtimeConfigPath);
|
|
|
|
|
Console.WriteLine(runtimeConfig.Framework);
|
|
|
|
|
return runtimeConfig.IsPortable;
|
2016-03-17 22:24:36 +00:00
|
|
|
|
}
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|