Skip to content

Commit 684f75d

Browse files
committed
Fix environment variable for dotnet build in test
1 parent 1fa323c commit 684f75d

4 files changed

Lines changed: 204 additions & 126 deletions

File tree

src/NuGet.Core/NuGet.Build.Tasks.Pack/GetPackOutputItemsTask.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,42 @@ public override bool Execute()
5454

5555
if (!string.IsNullOrWhiteSpace(NuspecInputFilePath))
5656
{
57+
bool hasVersionInNuspecProperties = false;
58+
if (NuspecProperties != null && NuspecProperties.Length > 0)
59+
{
60+
PackArgs packArgs = new PackArgs();
61+
PackTaskLogic.SetPackArgsPropertiesFromNuspecProperties(packArgs, MSBuildStringUtility.TrimAndExcludeNullOrEmpty(NuspecProperties));
62+
if (packArgs.Properties.ContainsKey("version"))
63+
{
64+
packageVersion = packArgs.Version;
65+
hasVersionInNuspecProperties = true;
66+
}
67+
if (packArgs.Properties.TryGetValue("id", out var idTemp))
68+
{
69+
packageId = idTemp;
70+
}
71+
}
72+
5773
var nuspecReader = new NuGet.Packaging.NuspecReader(NuspecInputFilePath);
5874
packageId = nuspecReader.GetId();
59-
packageVersion = nuspecReader.GetVersion().ToNormalizedString();
60-
61-
PackArgs packArgs = new PackArgs() { Version = packageVersion };
62-
PackTaskLogic.SetPackArgsPropertiesFromNuspecProperties(packArgs, MSBuildStringUtility.TrimAndExcludeNullOrEmpty(NuspecProperties), NuspecInputFilePath);
63-
packageVersion = packArgs.Version;
75+
if (!hasVersionInNuspecProperties)
76+
{
77+
packageVersion = nuspecReader.GetVersion().ToNormalizedString();
78+
}
6479
}
6580

66-
NuGetVersion version;
67-
if (!NuGetVersion.TryParse(packageVersion, out version))
81+
if (!NuGetVersion.TryParse(packageVersion, out var versionTemp))
6882
{
6983
throw new ArgumentException(string.Format(
7084
CultureInfo.CurrentCulture,
7185
Strings.InvalidPackageVersion,
7286
packageVersion));
7387
}
88+
NuGetVersion version = versionTemp!;
7489

7590
var symbolPackageFormat = PackArgs.GetSymbolPackageFormat(MSBuildStringUtility.TrimAndGetNullForEmpty(SymbolPackageFormat));
76-
var nupkgFileName = PackCommandRunner.GetOutputFileName(packageId, version, isNupkg: true, symbols: false, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
77-
var nuspecFileName = PackCommandRunner.GetOutputFileName(packageId, version, isNupkg: false, symbols: false, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
91+
var nupkgFileName = PackCommandRunner.GetOutputFileName(packageId, version!, isNupkg: true, symbols: false, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
92+
var nuspecFileName = PackCommandRunner.GetOutputFileName(packageId, version!, isNupkg: false, symbols: false, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
7893

7994
var outputs = new List<ITaskItem>();
8095
outputs.Add(new TaskItem(Path.Combine(PackageOutputPath, nupkgFileName)));

src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public PackArgs GetPackArgs(IPackTaskRequest<IMSBuildItem> request)
7575

7676
if (!string.IsNullOrEmpty(request.NuspecFile))
7777
{
78-
SetPackArgsPropertiesFromNuspecProperties(packArgs, request.NuspecProperties, request.NuspecFile);
78+
SetPackArgsPropertiesFromNuspecProperties(packArgs, request.NuspecProperties);
7979
}
8080
else
8181
{
@@ -1084,9 +1084,9 @@ private static IDictionary<string, string> ParsePropertiesAsDictionary(string[]
10841084
return dictionary;
10851085
}
10861086

1087-
internal static void SetPackArgsPropertiesFromNuspecProperties(PackArgs packArgs, string[] nuspecProperties, string nuspecInputFilePath)
1087+
internal static void SetPackArgsPropertiesFromNuspecProperties(PackArgs packArgs, string[] nuspecProperties)
10881088
{
1089-
if (string.IsNullOrWhiteSpace(nuspecInputFilePath) || nuspecProperties == null || !nuspecProperties.Any())
1089+
if (nuspecProperties == null || !nuspecProperties.Any())
10901090
{
10911091
return;
10921092
}
@@ -1103,8 +1103,6 @@ internal static void SetPackArgsPropertiesFromNuspecProperties(PackArgs packArgs
11031103
}
11041104
packArgs.Version = version.ToNormalizedString();
11051105
}
1106-
1107-
11081106
}
11091107

11101108
private HashSet<string> InitOutputExtensions(IEnumerable<string> outputExtensions)
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)