Skip to content

Commit

Permalink
Added logic to handle extraneous double-quotes around value for --msb…
Browse files Browse the repository at this point in the history
…uild-parameters that could be passed in .NET argument for certain execution environments.
  • Loading branch information
ashishdhingra committed Jan 9, 2025
1 parent fa1385e commit 3ce8925
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .autover/changes/fd1edd27-5324-4679-b0af-e2530bcc918f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.Tools",
"Type": "Patch",
"ChangelogMessages": [
"Added logic to handle extraneous double-quotes around value for --msbuild-parameters that could be passed in .NET argument for certain execution environments."
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public static CommandOptions ParseArguments(
value.BoolValue = bv;
break;
}

// --msbuild-parameters is a special case where multiple parameters separated by space character are enclosed in double quotes. In certain environments (like JavaScript action runner), the leading and trailing double quotes characters are also passed to .NET command arguments.
if (option == CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS && !string.IsNullOrEmpty(value.StringValue)
&& (value.StringValue.Trim().StartsWith('\"') && value.StringValue.Trim().EndsWith('\"')))
{
value.StringValue = value.StringValue.Trim().Trim('\"');
}

i++;
}

Expand Down
13 changes: 13 additions & 0 deletions test/Amazon.Lambda.Tools.Test/CommandLineParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,18 @@ public void BuildLambdaDeployCommandWithAllArguments()
Assert.Equal(55, command.Timeout);
Assert.Equal("netcore9.9", command.Runtime);
}

[Fact]
public void BuildLambdaDeployCommandWithMSBuildParamAndSwitchDoubleQuotedValue()
{
var arguments = new List<string>();
arguments.AddRange(new string[] { "--region", "us-west-2" });
arguments.AddRange(new string[] { "--msbuild-parameters", "\"--no-restore --no-build\"" });
arguments.Add("/p:Foo=bar;Version=1.2.3");

var command = new DeployFunctionCommand(new ConsoleToolLogger(), ".", arguments.ToArray());
Assert.Equal("us-west-2", command.Region);
Assert.Equal("--no-restore --no-build /p:Foo=bar;Version=1.2.3", command.MSBuildParameters);
}
}
}
34 changes: 34 additions & 0 deletions test/Amazon.Lambda.Tools.Test/OptionParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,39 @@ public void ParseMSBuildParameters()
Assert.NotNull(param);
Assert.Equal("us-west-2", param.Item2.StringValue);
}

[Fact]
public void ParseMSBuildSwitchDoubleQuotedValue()
{
var values = CommandLineParser.ParseArguments(DeployFunctionCommand.DeployCommandOptions,
new[] { "myfunc", "--region", "us-west-2", "--msbuild-parameters", "\"--no-restore --no-build\"" });

Assert.Equal("myfunc", values.Arguments[0]);

var msbuildparametersParam = values.FindCommandOption("--msbuild-parameters");
Assert.NotNull(msbuildparametersParam);
Assert.Equal("--no-restore --no-build", msbuildparametersParam.Item2.StringValue);

var param = values.FindCommandOption("--region");
Assert.NotNull(param);
Assert.Equal("us-west-2", param.Item2.StringValue);
}

[Fact]
public void ParseMSBuildSwitchNotDoubleQuotedValue()
{
var values = CommandLineParser.ParseArguments(DeployFunctionCommand.DeployCommandOptions,
new[] { "myfunc", "--region", "us-west-2", "--msbuild-parameters", "--no-restore --no-build" });

Assert.Equal("myfunc", values.Arguments[0]);

var msbuildparametersParam = values.FindCommandOption("--msbuild-parameters");
Assert.NotNull(msbuildparametersParam);
Assert.Equal("--no-restore --no-build", msbuildparametersParam.Item2.StringValue);

var param = values.FindCommandOption("--region");
Assert.NotNull(param);
Assert.Equal("us-west-2", param.Item2.StringValue);
}
}
}

0 comments on commit 3ce8925

Please sign in to comment.