dotnet-installer/test/Microsoft.DotNet.Tools.Tests.Utilities/TestBase.cs

174 lines
5.4 KiB
C#
Raw Normal View History

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;
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;
using Microsoft.DotNet.TestFramework;
2015-12-15 01:39:29 +00:00
namespace Microsoft.DotNet.Tools.Test.Utilities
{
2015-12-15 01:39:29 +00:00
/// <summary>
/// Base class for all unit test classes.
/// </summary>
public abstract class TestBase : IDisposable
{
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;
private static TestAssets s_testAssets;
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);
}
}
protected static string RepoRoot
{
get
{
return RepoDirectoriesProvider.RepoRoot;
}
}
2018-11-08 20:11:30 +00:00
public static TestAssets TestAssets
{
get
{
if (s_testAssets == null)
{
var assetsRoot = Path.Combine(RepoRoot, "TestAssets");
s_testAssets = new TestAssets(
new DirectoryInfo(assetsRoot),
new FileInfo(RepoDirectoriesProvider.DotnetUnderTest),
2018-10-29 18:26:53 +00:00
RepoDirectoriesProvider.TestWorkingFolder);
}
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()
{
if (_temp != null && !PreserveTemp())
{
2015-12-15 01:39:29 +00:00
_temp.Dispose();
}
}
// 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,
string executableName,
string expectedOutput)
2016-01-14 19:52:54 +00:00
{
var executablePath = Path.Combine(outputDir, executableName);
var args = new List<string>();
if (IsPortable(executablePath))
{
args.Add("exec");
args.Add(ArgumentEscaper.EscapeSingleArg(executablePath));
executablePath = RepoDirectoriesProvider.DotnetUnderTest;
}
2016-01-14 19:52:54 +00:00
var executableCommand = new TestCommand(executablePath);
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();
result.Should().Pass();
2016-04-02 04:52:08 +00:00
return result;
}
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
}
protected void TestNativeOutputExecutable(string outputDir, string executableName, string expectedOutput)
{
TestOutputExecutable(outputDir, executableName, expectedOutput, true);
}
protected string GetCompilationOutputPath(string outputDir, bool native)
{
2016-02-03 18:57:25 +00:00
var executablePath = outputDir;
if (native)
{
2016-02-03 18:57:25 +00:00
executablePath = Path.Combine(executablePath, "native");
}
return executablePath;
}
private bool IsPortable(string executablePath)
{
var commandDir = Path.GetDirectoryName(executablePath);
var runtimeConfigPath = Directory.EnumerateFiles(commandDir)
.FirstOrDefault(x => x.EndsWith("runtimeconfig.json"));
if (runtimeConfigPath == null)
{
return false;
}
var runtimeConfig = new RuntimeConfig(runtimeConfigPath);
Console.WriteLine(runtimeConfig.Framework);
return runtimeConfig.IsPortable;
}
2015-12-15 01:39:29 +00:00
}
}