Skip to content

Add MsBuildArgument constructor supporting multiple values #2732

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions src/BenchmarkDotNet/Jobs/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,34 @@ public MonoArgument(string value) : base(value)
/// example: new MsBuildArgument("/p:MyCustomSetting=123")
/// </summary>
[PublicAPI]

/// <summary>
/// A specialized MSBuild argument that sets a property using a quoted, semicolon-separated list of values.
/// </summary>
public class MsBuildProperty : MsBuildArgument
{
public MsBuildProperty(string key, params string[] values)
: base($"/p:{key}=\"{string.Join(";", values)}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that quotes need to be escaped to work cross-platform.

Suggested change
: base($"/p:{key}=\"{string.Join(";", values)}")
: base($"/p:{key}=\\\"{string.Join(";", values)}\\\"")

{
}


}
public class MsBuildArgument : Argument
{
public MsBuildArgument(string value) : base(value) { }

public MsBuildArgument(string key, params string[] values)
: base($"/p:{key}={EscapeAndJoin(values)}") { }

private static string EscapeAndJoin(string[] values)
{
return string.Join("%3b", values.Select(EscapeSpecialChars));
}

private static string EscapeSpecialChars(string input)
{
return input.Replace(";", "%3B");
}
Comment on lines +64 to +76
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public MsBuildArgument(string key, params string[] values)
: base($"/p:{key}={EscapeAndJoin(values)}") { }
private static string EscapeAndJoin(string[] values)
{
return string.Join("%3b", values.Select(EscapeSpecialChars));
}
private static string EscapeSpecialChars(string input)
{
return input.Replace(";", "%3B");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public void ProcessIsBuiltWithCustomProperty(bool setCustomProperty)
CanExecute<PropertyDefine>(config);
}

[Fact]

public void MsBuildProperty_ShouldWrapMultipleValuesInQuotes()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this test to BenchmarkDotNet.Tests project, and add a real integration test in BenchmarkDotNet.IntegrationTests project.

{
var arg = new MsBuildProperty("DefineConstants", "FOO", "BAR");
Assert.Equal("/p:DefineConstants=\"FOO;BAR\"", arg.TextRepresentation);
}
[Fact]
public void MultipleProcessesAreBuiltWithCorrectProperties()
{
Expand Down
Loading