-using System;
-using System.IO;
+using System.IO;
using System.Linq;
-using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli.Build.Framework;
-using Microsoft.DotNet.InternalAbstractions;
-using System.Collections.Generic;
+using Microsoft.DotNet.CoreSetup.Test;
namespace Microsoft.DotNet.Cli.Build
{
public partial class DotNetCli
{
public string BinPath { get; }
- public string GreatestVersionSharedFxPath { get; private set; }
- public string GreatestVersionHostFxrPath { get; private set; }
+ public string GreatestVersionSharedFxPath { get; }
+ public string GreatestVersionHostFxrPath { get; }
+ public string DotnetExecutablePath
+ {
+ get
+ {
+ return Path.Combine(BinPath, RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform("dotnet"));
+ }
+ }
public DotNetCli(string binPath)
{
BinPath = binPath;
- ComputeSharedFxPaths();
+
+ var sharedFxBaseDirectory = Path.Combine(BinPath, "shared", "Microsoft.NETCore.App");
+ if (!Directory.Exists(sharedFxBaseDirectory))
+ {
+ GreatestVersionSharedFxPath = null;
+ return;
+ }
+
+ var hostFxrBaseDirectory = Path.Combine(BinPath, "host", "fxr");
+
+ if (!Directory.Exists(hostFxrBaseDirectory))
+ {
+ GreatestVersionHostFxrPath = null;
+ return;
+ }
+
+ var sharedFxVersionDirectories = Directory.EnumerateDirectories(sharedFxBaseDirectory);
+
+ GreatestVersionSharedFxPath = sharedFxVersionDirectories
+ .OrderByDescending(p => p.ToLower())
+ .First();
+
+ var hostFxrVersionDirectories = Directory.EnumerateDirectories(hostFxrBaseDirectory);
+ GreatestVersionHostFxrPath = hostFxrVersionDirectories
+ .OrderByDescending(p => p.ToLower())
+ .First();
}
public Command Exec(string command, params string[] args)
newArgs.Insert(0, "-v");
}
- return Command.Create(Path.Combine(BinPath, $"dotnet{Constants.ExeSuffix}"), newArgs)
+ return Command.Create(DotnetExecutablePath, newArgs)
.EnvironmentVariable("DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "1");
}
public Command Publish(params string[] args) => Exec("publish", args);
public Command Store(params string[] args) => Exec("store", args);
-
- private void ComputeSharedFxPaths()
- {
- var sharedFxBaseDirectory = Path.Combine(BinPath, "shared", "Microsoft.NETCore.App");
- if ( ! Directory.Exists(sharedFxBaseDirectory))
- {
-
- GreatestVersionSharedFxPath = null;
- return;
- }
-
- var hostFxrBaseDirectory = Path.Combine(BinPath, "host", "fxr");
-
- if (!Directory.Exists(hostFxrBaseDirectory))
- {
- GreatestVersionHostFxrPath = null;
- return;
- }
-
- var sharedFxVersionDirectories = Directory.EnumerateDirectories(sharedFxBaseDirectory);
-
- GreatestVersionSharedFxPath = sharedFxVersionDirectories
- .OrderByDescending(p => p.ToLower())
- .First();
-
- var hostFxrVersionDirectories = Directory.EnumerateDirectories(hostFxrBaseDirectory);
- GreatestVersionHostFxrPath = hostFxrVersionDirectories
- .OrderByDescending(p => p.ToLower())
- .First();
- }
}
}
using System;
-using System.Collections.Generic;
using System.IO;
-using System.Linq;
-using System.Threading.Tasks;
namespace Microsoft.DotNet.CoreSetup.Test
{
public class TestProject : IDisposable
{
- private string _projectDirectory;
- private string _projectName;
- private string _outputDirectory;
- private string _exeExtension;
- private string _sharedLibraryExtension;
- private string _sharedLibraryPrefix;
+ public string ProjectDirectory { get; }
+ public string ProjectName { get; }
- private string _projectFile;
- private string _projectAssetsJson;
- private string _runtimeConfigJson;
- private string _runtimeDevConfigJson;
- private string _depsJson;
- private string _appDll;
- private string _appExe;
- private string _hostPolicyDll;
- private string _hostFxrDll;
- private string _assemblyName;
-
- public string ProjectDirectory => _projectDirectory;
- public string ProjectName => _projectName;
-
- public string OutputDirectory { get { return _outputDirectory; } set { _outputDirectory = value; } }
- public string ExeExtension { get { return _exeExtension; } set { _exeExtension = value; } }
- public string AssemblyName { get { return _assemblyName; } set { _assemblyName = value; } }
- public string ProjectFile { get { return _projectFile; } set { _projectFile = value; } }
- public string ProjectAssetsJson { get { return _projectAssetsJson; } set { _projectAssetsJson = value; } }
- public string RuntimeConfigJson { get { return _runtimeConfigJson; } set { _runtimeConfigJson = value; } }
- public string RuntimeDevConfigJson { get { return _runtimeDevConfigJson; } set { _runtimeDevConfigJson = value; } }
- public string DepsJson { get { return _depsJson; } set { _depsJson = value; } }
- public string AppDll { get { return _appDll; } set { _appDll = value; } }
- public string AppExe { get { return _appExe; } set { _appExe = value; } }
- public string HostPolicyDll { get { return _hostPolicyDll; } set { _hostPolicyDll = value; } }
- public string HostFxrDll { get { return _hostFxrDll; } set { _hostFxrDll = value; } }
+ public string OutputDirectory { get; set; }
+ public string AssemblyName { get; set; }
+ public string ProjectFile { get; set; }
+ public string ProjectAssetsJson { get; set; }
+ public string RuntimeConfigJson { get; set; }
+ public string RuntimeDevConfigJson { get; set; }
+ public string DepsJson { get; set; }
+ public string AppDll { get; set; }
+ public string AppExe { get; set; }
+ public string HostPolicyDll { get; set; }
+ public string HostFxrDll { get; set; }
public TestProject(
string projectDirectory,
- string exeExtension,
- string sharedLibraryExtension,
- string sharedLibraryPrefix,
string outputDirectory = null,
string assemblyName = null)
{
- _projectDirectory = projectDirectory;
- _exeExtension = exeExtension;
- _sharedLibraryExtension = sharedLibraryExtension;
- _sharedLibraryPrefix = sharedLibraryPrefix;
- _projectName = Path.GetFileName(_projectDirectory);
- _assemblyName = assemblyName ?? _projectName;
- _projectFile = Path.Combine(_projectDirectory, $"{_projectName}.csproj");
- _projectAssetsJson = Path.Combine(_projectDirectory, "obj", "project.assets.json");
+ ProjectDirectory = projectDirectory;
+ ProjectName = Path.GetFileName(ProjectDirectory);
+ AssemblyName = assemblyName ?? ProjectName;
+ ProjectFile = Path.Combine(ProjectDirectory, $"{ProjectName}.csproj");
+ ProjectAssetsJson = Path.Combine(ProjectDirectory, "obj", "project.assets.json");
- _outputDirectory = outputDirectory ?? Path.Combine(_projectDirectory, "bin");
- if (Directory.Exists(_outputDirectory))
+ OutputDirectory = outputDirectory ?? Path.Combine(ProjectDirectory, "bin");
+ if (Directory.Exists(OutputDirectory))
{
LoadOutputFiles();
}
{
if (!PreserveTestRuns())
{
- Directory.Delete(_projectDirectory, true);
+ Directory.Delete(ProjectDirectory, true);
}
}
public void CopyProjectFiles(string directory)
{
- CopyRecursive(_projectDirectory, directory, overwrite: true);
+ CopyRecursive(ProjectDirectory, directory, overwrite: true);
}
public void LoadOutputFiles()
{
- _appDll = Path.Combine(_outputDirectory, $"{_assemblyName}.dll");
- _appExe = Path.Combine(_outputDirectory, $"{_assemblyName}{_exeExtension}");
- _depsJson = Path.Combine(_outputDirectory, $"{_assemblyName}.deps.json");
- _runtimeConfigJson = Path.Combine(_outputDirectory, $"{_assemblyName}.runtimeconfig.json");
- _runtimeDevConfigJson = Path.Combine(_outputDirectory, $"{_assemblyName}.runtimeconfig.dev.json");
- _hostPolicyDll = Path.Combine(_outputDirectory, $"{_sharedLibraryPrefix}hostpolicy{_sharedLibraryExtension}");
- _hostFxrDll = Path.Combine(_outputDirectory, $"{_sharedLibraryPrefix}hostfxr{_sharedLibraryExtension}");
+ AppDll = Path.Combine(OutputDirectory, $"{AssemblyName}.dll");
+ AppExe = Path.Combine(OutputDirectory, RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform(AssemblyName));
+ DepsJson = Path.Combine(OutputDirectory, $"{AssemblyName}.deps.json");
+ RuntimeConfigJson = Path.Combine(OutputDirectory, $"{AssemblyName}.runtimeconfig.json");
+ RuntimeDevConfigJson = Path.Combine(OutputDirectory, $"{AssemblyName}.runtimeconfig.dev.json");
+ HostPolicyDll = Path.Combine(OutputDirectory, RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy"));
+ HostFxrDll = Path.Combine(OutputDirectory, RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr"));
}
public bool IsRestored()
{
- if (string.IsNullOrEmpty(_projectAssetsJson))
+ if (string.IsNullOrEmpty(ProjectAssetsJson))
{
return false;
}
- return File.Exists(_projectAssetsJson);
+ return File.Exists(ProjectAssetsJson);
}
private void CopyRecursive(string sourceDirectory, string destinationDirectory, bool overwrite = false)
{
private static readonly string s_testArtifactDirectoryEnvironmentVariable = "TEST_ARTIFACTS";
- private string _testProjectName;
- private string _exeExtension;
- private string _sharedLibraryExtension;
- private string _sharedLibraryPrefix;
- private string _testProjectSourceDirectory;
private string _testArtifactDirectory;
- private string _currentRid;
- private string _framework;
private string _assemblyName;
-
- private RepoDirectoriesProvider _repoDirectoriesProvider;
-
- private DotNetCli _sdkDotnet;
- private DotNetCli _builtDotnet;
-
private TestProject _sourceTestProject;
- private TestProject _testProject;
private List<TestProject> _copiedTestProjects = new List<TestProject>();
- public DotNetCli SdkDotnet => _sdkDotnet;
- public DotNetCli BuiltDotnet => _builtDotnet;
- public TestProject TestProject => _testProject;
+ public DotNetCli SdkDotnet { get; }
+ public DotNetCli BuiltDotnet { get; }
+ public TestProject TestProject { get; private set; }
+
+ public string CurrentRid { get; private set; }
+ public string Framework { get; private set; }
+ public RepoDirectoriesProvider RepoDirProvider { get; }
- public string CurrentRid => _currentRid;
- public string ExeExtension => _exeExtension;
- public string SharedLibraryExtension => _sharedLibraryExtension;
- public string SharedLibraryPrefix => _sharedLibraryPrefix;
- public string Framework => _framework;
- public RepoDirectoriesProvider RepoDirProvider => _repoDirectoriesProvider;
public TestProjectFixture(
string testProjectName,
RepoDirectoriesProvider repoDirectoriesProvider,
- string exeExtension = null,
- string sharedLibraryExtension = null,
- string sharedLibraryPrefix= null,
- string testProjectSourceDirectory = null,
- string testArtifactDirectory = null,
- string dotnetInstallPath = null,
- string currentRid = null,
- string builtDotnetOutputPath = null,
string framework = null,
string assemblyName = null)
{
ValidateRequiredDirectories(repoDirectoriesProvider);
- _testProjectName = testProjectName;
- _framework = framework ?? Environment.GetEnvironmentVariable("MNA_TFM");
-
- _exeExtension = exeExtension ?? RuntimeInformationExtensions.GetExeExtensionForCurrentOSPlatform();
- _sharedLibraryExtension = sharedLibraryExtension
- ?? RuntimeInformationExtensions.GetSharedLibraryExtensionForCurrentPlatform();
- _sharedLibraryPrefix = sharedLibraryPrefix
- ?? RuntimeInformationExtensions.GetSharedLibraryPrefixForCurrentPlatform();
+ Framework = framework ?? Environment.GetEnvironmentVariable("MNA_TFM");
- _repoDirectoriesProvider = repoDirectoriesProvider;
+ RepoDirProvider = repoDirectoriesProvider;
- _testProjectSourceDirectory = testProjectSourceDirectory
- ?? Path.Combine(repoDirectoriesProvider.RepoRoot, "src", "test", "Assets", "TestProjects");
_testArtifactDirectory = _testArtifactDirectory
?? Environment.GetEnvironmentVariable(s_testArtifactDirectoryEnvironmentVariable)
?? Path.Combine(AppContext.BaseDirectory, s_testArtifactDirectoryEnvironmentVariable);
- _sdkDotnet = new DotNetCli(dotnetInstallPath ?? repoDirectoriesProvider.DotnetSDK);
- _currentRid = currentRid ?? repoDirectoriesProvider.TargetRID;
+ SdkDotnet = new DotNetCli(repoDirectoriesProvider.DotnetSDK);
+ CurrentRid = repoDirectoriesProvider.TargetRID;
- _builtDotnet = new DotNetCli(repoDirectoriesProvider.BuiltDotnet);
+ BuiltDotnet = new DotNetCli(repoDirectoriesProvider.BuiltDotnet);
_assemblyName = assemblyName;
- InitializeTestProject(
- _testProjectName,
- _testProjectSourceDirectory,
+ var sourceTestProjectPath = Path.Combine(repoDirectoriesProvider.RepoRoot, "src", "test", "Assets", "TestProjects", testProjectName);
+ _sourceTestProject = new TestProject(
+ sourceTestProjectPath,
+ assemblyName: _assemblyName);
+
+ TestProject = CopyTestProject(
+ _sourceTestProject,
_testArtifactDirectory,
- _exeExtension,
- _sharedLibraryExtension,
- _sharedLibraryPrefix,
_assemblyName);
}
public TestProjectFixture(TestProjectFixture fixtureToCopy)
{
- _testProjectName = fixtureToCopy._testProjectName;
- _exeExtension = fixtureToCopy._exeExtension;
- _sharedLibraryExtension = fixtureToCopy._sharedLibraryExtension;
- _sharedLibraryPrefix = fixtureToCopy._sharedLibraryPrefix;
- _repoDirectoriesProvider = fixtureToCopy._repoDirectoriesProvider;
- _testProjectSourceDirectory = fixtureToCopy._testProjectSourceDirectory;
+ RepoDirProvider = fixtureToCopy.RepoDirProvider;
_testArtifactDirectory = fixtureToCopy._testArtifactDirectory;
- _sdkDotnet = fixtureToCopy._sdkDotnet;
- _currentRid = fixtureToCopy._currentRid;
- _builtDotnet = fixtureToCopy._builtDotnet;
+ SdkDotnet = fixtureToCopy.SdkDotnet;
+ CurrentRid = fixtureToCopy.CurrentRid;
+ BuiltDotnet = fixtureToCopy.BuiltDotnet;
_sourceTestProject = fixtureToCopy._sourceTestProject;
- _framework = fixtureToCopy._framework;
+ Framework = fixtureToCopy.Framework;
_assemblyName = fixtureToCopy._assemblyName;
- _testProject = CopyTestProject(
+ TestProject = CopyTestProject(
fixtureToCopy.TestProject,
_testArtifactDirectory,
- _exeExtension,
- _sharedLibraryExtension,
- _sharedLibraryPrefix,
_assemblyName);
- fixtureToCopy._copiedTestProjects.Add(_testProject);
+ fixtureToCopy._copiedTestProjects.Add(TestProject);
}
public void Dispose()
{
- if (_testProject != null)
+ if (TestProject != null)
{
- _testProject.Dispose();
- _testProject = null;
+ TestProject.Dispose();
+ TestProject = null;
}
foreach (var project in _copiedTestProjects)
_copiedTestProjects.Clear();
}
- private void InitializeTestProject(
- string testProjectName,
- string testProjectSourceDirectory,
- string testArtifactDirectory,
- string exeExtension,
- string sharedLibraryExtension,
- string sharedLibraryPrefix,
- string assemblyName)
- {
- var sourceTestProjectPath = Path.Combine(testProjectSourceDirectory, testProjectName);
- _sourceTestProject = new TestProject(
- sourceTestProjectPath,
- exeExtension,
- sharedLibraryExtension,
- sharedLibraryPrefix,
- assemblyName: assemblyName);
-
- _testProject = CopyTestProject(
- _sourceTestProject,
- testArtifactDirectory,
- exeExtension,
- sharedLibraryExtension,
- sharedLibraryPrefix,
- assemblyName);
- }
-
private TestProject CopyTestProject(
TestProject sourceTestProject,
string testArtifactDirectory,
- string exeExtension,
- string sharedLibraryExtension,
- string sharedLibraryPrefix,
string assemblyName)
{
string copiedTestProjectDirectory = CalculateTestProjectDirectory(
return new TestProject(
copiedTestProjectDirectory,
- exeExtension,
- sharedLibraryExtension,
- sharedLibraryPrefix,
assemblyName: assemblyName);
}
string framework = null,
string outputDirectory = null)
{
- dotnet = dotnet ?? _sdkDotnet;
- outputDirectory = outputDirectory ?? _testProject.OutputDirectory;
- _testProject.OutputDirectory = outputDirectory;
- framework = framework ?? _framework;
- _framework = framework;
+ dotnet = dotnet ?? SdkDotnet;
+ outputDirectory = outputDirectory ?? TestProject.OutputDirectory;
+ TestProject.OutputDirectory = outputDirectory;
+ framework = framework ?? Framework;
+ Framework = framework;
- var buildArgs = new List<string>();
- buildArgs.Add("--no-restore");
+ var buildArgs = new List<string>
+ {
+ "--no-restore"
+ };
if (runtime != null)
{
buildArgs.Add(framework);
}
- buildArgs.Add($"/p:MNAVersion={_repoDirectoriesProvider.MicrosoftNETCoreAppVersion}");
+ buildArgs.Add($"/p:MNAVersion={RepoDirProvider.MicrosoftNETCoreAppVersion}");
if (outputDirectory != null)
{
}
dotnet.Build(buildArgs.ToArray())
- .WorkingDirectory(_testProject.ProjectDirectory)
- .Environment("NUGET_PACKAGES", _repoDirectoriesProvider.NugetPackages)
+ .WorkingDirectory(TestProject.ProjectDirectory)
+ .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages)
.Environment("VERSION", "") // Generate with package version 1.0.0, not %VERSION%
.CaptureStdErr()
.CaptureStdOut()
.Execute()
.EnsureSuccessful();
- _testProject.LoadOutputFiles();
+ TestProject.LoadOutputFiles();
return this;
}
string manifest = null,
string outputDirectory = null)
{
- dotnet = dotnet ?? _sdkDotnet;
- outputDirectory = outputDirectory ?? _testProject.OutputDirectory;
- framework = framework ?? _framework;
- _framework = framework;
+ dotnet = dotnet ?? SdkDotnet;
+ outputDirectory = outputDirectory ?? TestProject.OutputDirectory;
+ framework = framework ?? Framework;
+ Framework = framework;
+
+ var storeArgs = new List<string>
+ {
+ "--runtime"
+ };
- var storeArgs = new List<string>();
- storeArgs.Add("--runtime");
if (runtime != null)
{
storeArgs.Add(runtime);
storeArgs.Add(outputDirectory);
}
- storeArgs.Add($"/p:MNAVersion={_repoDirectoriesProvider.MicrosoftNETCoreAppVersion}");
- storeArgs.Add($"/p:NETCoreAppFramework={_framework}");
+ storeArgs.Add($"/p:MNAVersion={RepoDirProvider.MicrosoftNETCoreAppVersion}");
+ storeArgs.Add($"/p:NETCoreAppFramework={Framework}");
// Ensure the project's OutputType isn't 'Exe', since that causes issues with 'dotnet store'
storeArgs.Add("/p:OutputType=Library");
dotnet.Store(storeArgs.ToArray())
- .WorkingDirectory(_testProject.ProjectDirectory)
- .Environment("NUGET_PACKAGES", _repoDirectoriesProvider.NugetPackages)
+ .WorkingDirectory(TestProject.ProjectDirectory)
+ .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages)
.CaptureStdErr()
.CaptureStdOut()
.Execute()
.EnsureSuccessful();
- _testProject.LoadOutputFiles();
+ TestProject.LoadOutputFiles();
return this;
}
string framework = null,
string outputDirectory = null)
{
- dotnet = dotnet ?? _sdkDotnet;
- outputDirectory = outputDirectory ?? _testProject.OutputDirectory;
- _testProject.OutputDirectory = outputDirectory;
- framework = framework ?? _framework;
- _framework = framework;
+ dotnet = dotnet ?? SdkDotnet;
+ outputDirectory = outputDirectory ?? TestProject.OutputDirectory;
+ TestProject.OutputDirectory = outputDirectory;
+ framework = framework ?? Framework;
+ Framework = framework;
- var publishArgs = new List<string>();
- publishArgs.Add("--no-restore");
+ var publishArgs = new List<string>
+ {
+ "--no-restore"
+ };
if (runtime != null)
{
publishArgs.Add(outputDirectory);
}
- publishArgs.Add($"/p:MNAVersion={_repoDirectoriesProvider.MicrosoftNETCoreAppVersion}");
+ publishArgs.Add($"/p:MNAVersion={RepoDirProvider.MicrosoftNETCoreAppVersion}");
dotnet.Publish(publishArgs.ToArray())
- .WorkingDirectory(_testProject.ProjectDirectory)
- .Environment("NUGET_PACKAGES", _repoDirectoriesProvider.NugetPackages)
+ .WorkingDirectory(TestProject.ProjectDirectory)
+ .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages)
.CaptureStdErr()
.CaptureStdOut()
.Execute()
.EnsureSuccessful();
- _testProject.LoadOutputFiles();
+ TestProject.LoadOutputFiles();
return this;
}
}
restoreArgs.Add("--disable-parallel");
- restoreArgs.Add($"/p:MNAVersion={_repoDirectoriesProvider.MicrosoftNETCoreAppVersion}");
- restoreArgs.Add($"/p:NETCoreAppFramework={_framework}");
+ restoreArgs.Add($"/p:MNAVersion={RepoDirProvider.MicrosoftNETCoreAppVersion}");
+ restoreArgs.Add($"/p:NETCoreAppFramework={Framework}");
if (extraMSBuildProperties != null)
{
restoreArgs.Add(extraMSBuildProperties);
}
- _sdkDotnet.Restore(restoreArgs.ToArray())
- .WorkingDirectory(_testProject.ProjectDirectory)
+ SdkDotnet.Restore(restoreArgs.ToArray())
+ .WorkingDirectory(TestProject.ProjectDirectory)
.CaptureStdErr()
.CaptureStdOut()
- .Environment("NUGET_PACKAGES", _repoDirectoriesProvider.NugetPackages)
+ .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages)
.Execute()
.EnsureSuccessful();
public TestProjectFixture EnsureRestored(params string[] fallbackSources)
{
- if ( ! _testProject.IsRestored())
+ if ( ! TestProject.IsRestored())
{
RestoreProject(fallbackSources);
}
public TestProjectFixture EnsureRestoredForRid(string rid, params string[] fallbackSources)
{
- if ( ! _testProject.IsRestored())
+ if ( ! TestProject.IsRestored())
{
string extraMSBuildProperties = $"/p:TestTargetRid={rid}";
RestoreProject(fallbackSources, extraMSBuildProperties);