Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.0.2xx Servicing] Fix Property Parsing in publish and pack #30826

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/MSBuildPropertyParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

namespace Microsoft.DotNet.Cli.Utils;

/// <summary>
/// Parses property key value pairs that have already been forwarded through the PropertiesOption class.
/// Does not parse -p and etc. formats, (this is done by PropertiesOption) but does parse property values separated by =, ;, and using quotes.
/// </summary>
public static class MSBuildPropertyParser {
public static IEnumerable<(string key, string value)> ParseProperties(string input) {
var currentPos = 0;
Expand Down
10 changes: 6 additions & 4 deletions src/Cli/dotnet/ReleasePropertyProjectLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ private bool IsValidProjectFilePath(string path)
private Dictionary<string, string> GetGlobalPropertiesFromUserArgs(ParseResult parseResult)
{
Dictionary<string, string> globalProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

string[] globalPropEnumerable = parseResult.GetValueForOption(CommonOptions.PropertiesOption);

foreach (var keyEqVal in globalPropEnumerable)
foreach (var keyEqValString in globalPropEnumerable)
{
string[] keyValuePair = keyEqVal.Split("=", 2);
globalProperties[keyValuePair[0]] = keyValuePair[1];
var propertyPairs = MSBuildPropertyParser.ParseProperties(keyEqValString);
foreach (var propertyKeyValue in propertyPairs)
{
globalProperties[propertyKeyValue.key] = propertyKeyValue.value;
}
}
return globalProperties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
Expand Down Expand Up @@ -688,11 +689,13 @@ public void PublishRelease_does_not_override_custom_Configuration_on_proj_and_lo
[InlineData("-property:Configuration=Debug")]
[InlineData("--property:Configuration=Debug")]
[InlineData("/p:Configuration=Debug")]
[InlineData("-p:_IsPublishing=true;Configuration=Debug")]
[InlineData("-p:_IsPublishing=true;Configuration=Debug;")]
[InlineData("/property:Configuration=Debug")]
public void PublishRelease_does_not_override_Configuration_property_across_formats(string configOpt)
{
var helloWorldAsset = _testAssetsManager
.CopyTestAsset("HelloWorld", $"PublishReleaseHelloWorldCsProjConfigPropOverride{configOpt}")
.CopyTestAsset("HelloWorld", identifier: configOpt)
.WithSource()
.WithTargetFramework(ToolsetInfo.CurrentTargetFramework)
.WithProjectChanges(project =>
Expand All @@ -702,24 +705,45 @@ public void PublishRelease_does_not_override_Configuration_property_across_forma
propertyGroup.Add(new XElement(ns + "PublishRelease", "true"));
});

new BuildCommand(helloWorldAsset)
.Execute(configOpt)
.Should()
.Pass();

var publishCommand = new DotnetPublishCommand(Log, helloWorldAsset.TestRoot);

publishCommand
.Execute(configOpt)
.Should()
.Pass().And.NotHaveStdErr();
new DotnetPublishCommand(Log, helloWorldAsset.TestRoot)
.Execute(configOpt)
.Should()
.Pass().And.NotHaveStdErr();

var expectedAssetPath = System.IO.Path.Combine(helloWorldAsset.Path, "bin", "Debug", ToolsetInfo.CurrentTargetFramework, "HelloWorld.dll");
var expectedAssetPath = Path.Combine(helloWorldAsset.Path, "bin", "Debug", ToolsetInfo.CurrentTargetFramework, "HelloWorld.dll");
Assert.True(File.Exists(expectedAssetPath));
var releaseAssetPath = System.IO.Path.Combine(helloWorldAsset.Path, "bin", "Release", ToolsetInfo.CurrentTargetFramework, "HelloWorld.dll");
var releaseAssetPath = Path.Combine(helloWorldAsset.Path, "bin", "Release", ToolsetInfo.CurrentTargetFramework, "HelloWorld.dll");
Assert.False(File.Exists(releaseAssetPath)); // build will produce a debug asset, need to make sure this doesn't exist either.
}

[Theory]
[InlineData("")]
[InlineData("=")]
public void PublishRelease_does_recognize_undefined_property(string propertySuffix)
{
string tfm = ToolsetInfo.CurrentTargetFramework;
var testProject = new TestProject()
{
IsExe = true,
TargetFrameworks = tfm
};

testProject.RecordProperties("SelfContained");
testProject.RecordProperties("PublishAot");

var testAsset = _testAssetsManager.CreateTestProject(testProject);
new DotnetPublishCommand(Log)
.WithWorkingDirectory(Path.Combine(testAsset.TestRoot, MethodBase.GetCurrentMethod().Name))
.Execute(("-p:SelfContained" + propertySuffix))
.Should()
.Pass();

var properties = testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: tfm);

Assert.Equal("", properties["SelfContained"]);
Assert.Equal("", properties["PublishAot"]);
}

[Fact]
public void PublishRelease_does_not_override_Configuration_option()
{
Expand Down