-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathBuildFixture.cs
More file actions
146 lines (123 loc) · 6.48 KB
/
BuildFixture.cs
File metadata and controls
146 lines (123 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Internal.NuGet.Testing.SignedPackages.ChildProcess;
using NuGet.Frameworks;
using NuGet.Test.Utility;
using Xunit;
namespace NuGet.Build.Tasks.Pack.Test
{
[CollectionDefinition(Name)]
public class FixtureCollection
: ICollectionFixture<BuildFixture>
{
internal const string Name = "Build Tests";
}
public class BuildFixture : IDisposable
{
#if DEBUG
const string CONFIGURATION = "Debug";
#else
const string CONFIGURATION = "Release";
#endif
const string FILENAME_DLL = "NuGet.Build.Tasks.Pack.dll";
const string FILENAME_TARGETS = "NuGet.Build.Tasks.Pack.targets";
internal readonly bool _isDotNetFramework = false;
internal readonly string _testFrameworkMoniker = "netstandard2.0";
#if IS_DESKTOP
private const string SdkVersion = "10";
private const string SdkTfm = "net10.0";
#endif
internal readonly string _pathDotnetExe;
internal readonly string _pathMSBuildExe;
internal readonly string _pathDllFile;
internal readonly string _pathTargetsFile;
internal readonly IReadOnlyDictionary<string, string> _dotnetEnvironments;
public BuildFixture()
{
_pathDotnetExe = NuGet.Test.Utility.TestFileSystemUtility.GetDotnetCli();
#if IS_DESKTOP
var _cliDirectory = TestDotnetCLiUtility.CopyAndPatchLatestDotnetCli(SdkVersion, SdkTfm);
#else
string testAssemblyPath = Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().Location);
var _cliDirectory = TestDotnetCLiUtility.CopyAndPatchLatestDotnetCli(testAssemblyPath);
#endif
var dotnetExecutableName = NuGet.Common.RuntimeEnvironmentHelper.IsWindows ? "dotnet.exe" : "dotnet";
_pathDotnetExe = Path.Combine(_cliDirectory, dotnetExecutableName);
var sdkPath = Directory.EnumerateDirectories(Path.Combine(_cliDirectory, "sdk"))
.Single(d => !string.Equals(Path.GetFileName(d), "NuGetFallbackFolder", StringComparison.OrdinalIgnoreCase));
_pathMSBuildExe = GetMsBuildExePath();
_testFrameworkMoniker = GetFrameworkMoniker(typeof(NuGet.Build.Tasks.Pack.GetPackOutputItemsTask), out var isDotNetFramework);
_isDotNetFramework = isDotNetFramework;
var artifactsDirectory = NuGet.Test.Utility.TestFileSystemUtility.GetArtifactsDirectoryInRepo();
var dllLocation = typeof(NuGet.Build.Tasks.Pack.GetPackOutputItemsTask).Assembly.Location;
var dllDirectory = Path.Combine(artifactsDirectory, "NuGet.Build.Tasks.Pack", "bin", CONFIGURATION, _testFrameworkMoniker);
if (!System.IO.Directory.Exists(dllDirectory))
{
dllDirectory = Path.Combine(artifactsDirectory, "NuGet.Build.Tasks.Pack", "bin", CONFIGURATION, _testFrameworkMoniker);
}
_pathDllFile = Path.Combine(dllDirectory, FILENAME_DLL);
// https://github.com/NuGet/NuGet.Client/pull/6712
// NuGet.Build.Tasks.Pack.targets has been moved to in NuGet.Build.Tasks project.
// Therefore, NuGet.Build.Tasks project must be built before running this test.
var tfmTargets = GetFrameworkMoniker(typeof(PackageFileNameTests), out var _);
_pathTargetsFile = Path.Combine(artifactsDirectory, "NuGet.Build.Tasks", "bin", CONFIGURATION, tfmTargets, FILENAME_TARGETS);
if (!System.IO.File.Exists(_pathTargetsFile))
{
_pathTargetsFile = Path.Combine(artifactsDirectory, "NuGet.Build.Tasks", "bin", CONFIGURATION, _testFrameworkMoniker, FILENAME_TARGETS);
if (!System.IO.File.Exists(_pathTargetsFile))
{
_pathTargetsFile = Path.Combine(dllDirectory, FILENAME_TARGETS);
}
}
_dotnetEnvironments = new Dictionary<string, string>()
{
["MSBuildSDKsPath"] = Path.Combine(sdkPath, "Sdks"),
["DOTNET_MULTILEVEL_LOOKUP"] = "0",
["DOTNET_ROOT"] = _cliDirectory,
["MSBuildExtensionsPath"] = new DirectoryInfo(sdkPath).FullName,
["PATH"] = $"{_cliDirectory}{Path.PathSeparator}{Environment.GetEnvironmentVariable("PATH")}"
};
Assert.True(System.IO.File.Exists(_pathDllFile), $"{FILENAME_DLL} missing");
Assert.True(System.IO.File.Exists(_pathTargetsFile), $"{FILENAME_TARGETS} missing");
}
private static string GetFrameworkMoniker(Type typeInAssembly, out bool isDotNetFramework)
{
var assembly = typeInAssembly.Assembly;
var targetFrameworkAttribute
= assembly.GetCustomAttributes(typeof(System.Runtime.Versioning.TargetFrameworkAttribute), false)
.OfType<System.Runtime.Versioning.TargetFrameworkAttribute>().FirstOrDefault();
Assert.True(targetFrameworkAttribute != null, "Can't get targetFramework version");
isDotNetFramework = targetFrameworkAttribute.FrameworkName.Contains(".NETFramework");
return NuGetFramework.Parse(targetFrameworkAttribute.FrameworkName).GetShortFolderName();
}
private static string GetMsBuildExePath()
{
if (System.Environment.OSVersion.Platform == System.PlatformID.Win32NT)
{
var msbuildexe = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Windows), "Microsoft.NET", "Framework", "v4.0.30319", "msbuild.exe");
var vswhereexe = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86), "Microsoft Visual Studio", "Installer", "vswhere.exe");
var runresult = CommandRunner.Run(
vswhereexe,
System.Environment.CurrentDirectory,
@" -latest -find MSBuild\**\Bin\MSBuild.exe");
if (runresult.Success)
{
msbuildexe = new System.IO.StringReader(runresult.Output).ReadLine() ?? "";
}
return msbuildexe;
}
else
{
return "";
}
}
public void Dispose()
{
}
}
}