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