2015-12-14 17:39:29 -08: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 08:34:08 -07:00
|
|
|
|
using System.Globalization;
|
2016-01-14 11:52:54 -08:00
|
|
|
|
using System.IO;
|
2015-12-14 17:39:29 -08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-01-14 11:52:54 -08:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-02-11 14:17:20 -08:00
|
|
|
|
using Microsoft.DotNet.TestFramework;
|
2015-12-14 17:39:29 -08:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
|
{
|
2016-02-11 14:17:20 -08:00
|
|
|
|
|
2015-12-14 17:39:29 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base class for all unit test classes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class TestBase : IDisposable
|
|
|
|
|
{
|
2016-04-12 17:29:07 -07:00
|
|
|
|
protected const string DefaultFramework = "netcoreapp1.0";
|
|
|
|
|
protected const string DefaultLibraryFramework = "netstandard1.5";
|
2017-03-16 23:15:45 -07:00
|
|
|
|
protected const string ConsoleLoggerOutputNormal = "--logger console;verbosity=normal";
|
2015-12-14 17:39:29 -08:00
|
|
|
|
private TempRoot _temp;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
private static TestAssets s_testAssets;
|
2016-10-10 17:13:46 -07:00
|
|
|
|
|
2017-07-18 08:34:08 -07: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 14:17:20 -08:00
|
|
|
|
|
|
|
|
|
protected static string RepoRoot
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-10-10 17:13:46 -07:00
|
|
|
|
return RepoDirectoriesProvider.RepoRoot;
|
2016-02-11 14:17:20 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 12:11:30 -08:00
|
|
|
|
public static TestAssets TestAssets
|
2016-10-27 18:46:43 -07:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (s_testAssets == null)
|
|
|
|
|
{
|
|
|
|
|
var assetsRoot = Path.Combine(RepoRoot, "TestAssets");
|
|
|
|
|
|
2017-01-06 01:40:26 -08:00
|
|
|
|
s_testAssets = new TestAssets(
|
|
|
|
|
new DirectoryInfo(assetsRoot),
|
2018-11-20 10:53:39 -08:00
|
|
|
|
new FileInfo(RepoDirectoriesProvider.DotnetUnderTest),
|
2018-10-29 11:26:53 -07:00
|
|
|
|
RepoDirectoriesProvider.TestWorkingFolder);
|
2016-10-27 18:46:43 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s_testAssets;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-14 17:39:29 -08: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 11:03:14 -08:00
|
|
|
|
if (_temp != null && !PreserveTemp())
|
|
|
|
|
{
|
2015-12-14 17:39:29 -08:00
|
|
|
|
_temp.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-08 11:03:14 -08: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 11:52:54 -08:00
|
|
|
|
|
2016-04-01 21:52:08 -07:00
|
|
|
|
protected CommandResult TestExecutable(string outputDir,
|
2016-01-20 15:41:46 -08:00
|
|
|
|
string executableName,
|
2016-01-26 06:39:13 -08:00
|
|
|
|
string expectedOutput)
|
2016-01-14 11:52:54 -08:00
|
|
|
|
{
|
2016-01-26 06:39:13 -08:00
|
|
|
|
var executablePath = Path.Combine(outputDir, executableName);
|
2016-03-17 15:24:36 -07:00
|
|
|
|
var args = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (IsPortable(executablePath))
|
|
|
|
|
{
|
2016-03-17 16:39:48 -07:00
|
|
|
|
args.Add("exec");
|
2016-03-17 15:24:36 -07:00
|
|
|
|
args.Add(ArgumentEscaper.EscapeSingleArg(executablePath));
|
|
|
|
|
|
2018-11-20 10:53:39 -08:00
|
|
|
|
executablePath = RepoDirectoriesProvider.DotnetUnderTest;
|
2016-03-17 15:24:36 -07:00
|
|
|
|
}
|
2016-01-14 11:52:54 -08:00
|
|
|
|
|
|
|
|
|
var executableCommand = new TestCommand(executablePath);
|
|
|
|
|
|
2016-03-17 15:24:36 -07:00
|
|
|
|
var result = executableCommand.ExecuteWithCapturedOutput(string.Join(" ", args));
|
2016-01-14 11:52:54 -08:00
|
|
|
|
|
2016-04-01 21:52:08 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(expectedOutput))
|
|
|
|
|
{
|
|
|
|
|
result.Should().HaveStdOut(expectedOutput);
|
|
|
|
|
}
|
2016-01-14 11:52:54 -08:00
|
|
|
|
result.Should().NotHaveStdErr();
|
2016-02-11 14:17:20 -08:00
|
|
|
|
result.Should().Pass();
|
2016-04-01 21:52:08 -07:00
|
|
|
|
return result;
|
2016-01-26 06:39:13 -08:00
|
|
|
|
}
|
2016-02-11 14:17:20 -08:00
|
|
|
|
|
2016-01-26 06:39:13 -08:00
|
|
|
|
protected void TestOutputExecutable(
|
|
|
|
|
string outputDir,
|
|
|
|
|
string executableName,
|
|
|
|
|
string expectedOutput,
|
|
|
|
|
bool native = false)
|
|
|
|
|
{
|
|
|
|
|
TestExecutable(GetCompilationOutputPath(outputDir, native), executableName, expectedOutput);
|
2016-01-14 11:52:54 -08:00
|
|
|
|
}
|
2016-01-20 15:41:46 -08:00
|
|
|
|
|
2016-01-21 15:01:21 -08:00
|
|
|
|
protected void TestNativeOutputExecutable(string outputDir, string executableName, string expectedOutput)
|
2016-01-20 15:41:46 -08:00
|
|
|
|
{
|
|
|
|
|
TestOutputExecutable(outputDir, executableName, expectedOutput, true);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 15:01:21 -08:00
|
|
|
|
protected string GetCompilationOutputPath(string outputDir, bool native)
|
2016-01-20 15:41:46 -08:00
|
|
|
|
{
|
2016-02-03 10:57:25 -08:00
|
|
|
|
var executablePath = outputDir;
|
2016-01-20 15:41:46 -08:00
|
|
|
|
if (native)
|
|
|
|
|
{
|
2016-02-03 10:57:25 -08:00
|
|
|
|
executablePath = Path.Combine(executablePath, "native");
|
2016-01-20 15:41:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return executablePath;
|
|
|
|
|
}
|
2016-03-17 15:24:36 -07:00
|
|
|
|
|
|
|
|
|
private bool IsPortable(string executablePath)
|
|
|
|
|
{
|
2016-03-17 16:39:48 -07:00
|
|
|
|
var commandDir = Path.GetDirectoryName(executablePath);
|
2016-03-17 15:24:36 -07:00
|
|
|
|
|
2016-03-17 16:39:48 -07:00
|
|
|
|
var runtimeConfigPath = Directory.EnumerateFiles(commandDir)
|
2016-03-17 15:24:36 -07:00
|
|
|
|
.FirstOrDefault(x => x.EndsWith("runtimeconfig.json"));
|
|
|
|
|
|
2016-03-17 16:39:48 -07:00
|
|
|
|
if (runtimeConfigPath == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var runtimeConfig = new RuntimeConfig(runtimeConfigPath);
|
|
|
|
|
Console.WriteLine(runtimeConfig.Framework);
|
|
|
|
|
return runtimeConfig.IsPortable;
|
2016-03-17 15:24:36 -07:00
|
|
|
|
}
|
2015-12-14 17:39:29 -08:00
|
|
|
|
}
|
|
|
|
|
}
|