dotnet-installer/test/Microsoft.DotNet.Tools.Tests.Utilities/TestBase.cs
Livar Cunha 3a9525b5ac Merge branch 'rel/1.0.1' into merge_rel101_into_master
* rel/1.0.1: (66 commits)
  Update LZMA license with correct text
  Bump to 2.0.0-rc5-61427-04
  Remove duplicate installer suffix
  Add license text to LZMA SDK license notice
  Updating the SDK to 1.0.0-alpha-20170224-6
  Updating both platform abstractions and dependency model to 1.0.3.
  Bump Roslyn to 2.0.0-rc5-61424-02
  Update Stage0 to use the latest build.
  Update README with new distros.
  Back porting #5597 into rel/1.0.0
  Fixing the exclude pattern used by the Migration to exclude WEB SDK globs.
  Remove RID from test package creation
  Disable migrate and publish web app with content because CI does not have NPM
  Adding an E2E test for pack with content during migration.
  Fixing a failing test and adding a few more E2E tests around binplace content for migrated projects.
  Fix debian_config.json on ubuntu16.10
  Updating publish, pack and build of content to use None with Never/false/Never in their metadata for excluded items.
  Intermediate commit to get a WIP PR out. This adds the None Update with CopyToOutputDirectory set to Never.
  Switching the CopyToOutput for build and publish and the file for pack to use None Update instead of include. Also, fixed the exclude patterns for web apps.
  Do not migrate Content that is already included in the Web SDK for web apps.
  ...
2017-03-01 20:50:42 -08:00

164 lines
5 KiB
C#

// 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.Threading.Tasks;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.TestFramework;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
/// <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";
protected const string ConsoleLoggerOutputNormal = "--logger:console;verbosity=normal";
private TempRoot _temp;
private static TestAssets s_testAssets;
protected static string RepoRoot
{
get
{
return RepoDirectoriesProvider.RepoRoot;
}
}
protected static TestAssets TestAssets
{
get
{
if (s_testAssets == null)
{
var assetsRoot = Path.Combine(RepoRoot, "TestAssets");
s_testAssets = new TestAssets(
new DirectoryInfo(assetsRoot),
new FileInfo(new EnvironmentProvider().GetCommandPath("dotnet")),
new FileInfo(new RepoDirectoriesProvider().PjDotnet));
}
return s_testAssets;
}
}
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())
{
_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));
}
protected CommandResult TestExecutable(string outputDir,
string executableName,
string expectedOutput)
{
var executablePath = Path.Combine(outputDir, executableName);
var args = new List<string>();
if (IsPortable(executablePath))
{
args.Add("exec");
args.Add(ArgumentEscaper.EscapeSingleArg(executablePath));
var muxer = new Muxer();
executablePath = muxer.MuxerPath;
}
var executableCommand = new TestCommand(executablePath);
var result = executableCommand.ExecuteWithCapturedOutput(string.Join(" ", args));
if (!string.IsNullOrEmpty(expectedOutput))
{
result.Should().HaveStdOut(expectedOutput);
}
result.Should().NotHaveStdErr();
result.Should().Pass();
return result;
}
protected void TestOutputExecutable(
string outputDir,
string executableName,
string expectedOutput,
bool native = false)
{
TestExecutable(GetCompilationOutputPath(outputDir, native), executableName, expectedOutput);
}
protected void TestNativeOutputExecutable(string outputDir, string executableName, string expectedOutput)
{
TestOutputExecutable(outputDir, executableName, expectedOutput, true);
}
protected string GetCompilationOutputPath(string outputDir, bool native)
{
var executablePath = outputDir;
if (native)
{
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;
}
}
}