-
Notifications
You must be signed in to change notification settings - Fork 104
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
Improve command line argument quoting logic #335
Open
nahk-ivanov
wants to merge
2
commits into
tunnelvisionlabs:master
Choose a base branch
from
nahk-ivanov:fix-cmd-args-quoting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
runtime/CSharp/Antlr4BuildTasks.Test/Antlr4BuildTasks.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net40</TargetFrameworks> | ||
<EnableDefaultNoneItems>False</EnableDefaultNoneItems> | ||
|
||
<Description>Unit tests for ANTLR 4 build tasks.</Description> | ||
<Copyright>Copyright © Sam Harwell 2013</Copyright> | ||
<Company>Tunnel Vision Laboratories, LLC</Company> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<DebugType>full</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | ||
<DebugType>pdbonly</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
</PropertyGroup> | ||
|
||
<Choose> | ||
<When Condition="'$(TargetFramework)' == 'net40'"> | ||
<PropertyGroup> | ||
<DefineConstants>$(DefineConstants);NET40;NET40PLUS;NET35PLUS;NET30PLUS;NET20PLUS</DefineConstants> | ||
<MSTest2>False</MSTest2> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Antlr4BuildTasks\Antlr4BuildTasks.csproj"> | ||
<SetTargetFramework>TargetFramework=net40</SetTargetFramework> | ||
</ProjectReference> | ||
</ItemGroup> | ||
</When> | ||
</Choose> | ||
|
||
<PropertyGroup> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\..\..\build\keys\TestingKey.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<Choose> | ||
<When Condition="'$(MSTest2)' == 'false'"> | ||
<!-- Workaround for https://github.com/dotnet/roslyn-project-system/issues/1739 --> | ||
<PropertyGroup> | ||
<AssemblySearchPaths>$(AssemblySearchPaths);{GAC}</AssemblySearchPaths> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" /> | ||
</ItemGroup> | ||
</When> | ||
<Otherwise> | ||
<ItemGroup> | ||
<PackageReference Include="MSTest.TestFramework" Version="1.1.18" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="1.1.18" /> | ||
</ItemGroup> | ||
</Otherwise> | ||
</Choose> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\..\build\keys\TestingKey.snk"> | ||
<Link>TestingKey.snk</Link> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
|
||
</Project> |
22 changes: 22 additions & 0 deletions
22
runtime/CSharp/Antlr4BuildTasks.Test/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Terence Parr, Sam Harwell. All Rights Reserved. | ||
// Licensed under the BSD License. See LICENSE.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
[assembly: CLSCompliant(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("4AA8C780-9E51-4F31-AC04-A80D144A7D48")] |
69 changes: 69 additions & 0 deletions
69
runtime/CSharp/Antlr4BuildTasks.Test/TestCommandLineHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Terence Parr, Sam Harwell. All Rights Reserved. | ||
// Licensed under the BSD License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Antlr4BuildTasks.Test | ||
{ | ||
using Antlr4.Build.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
/// <summary> | ||
/// Defines test cases for <see cref="CommandLineHelper"/> | ||
/// </summary> | ||
[TestClass] | ||
public class TestCommandLineHelper | ||
{ | ||
[TestMethod] | ||
[Description("Verifies that there are no unnecessary double quotes added.")] | ||
public void JoinArgumentsDoesNotAddUnnecessaryQuotes() | ||
{ | ||
Assert.AreEqual( | ||
@"-o C:\somedir\ grammar.g4", | ||
CommandLineHelper.JoinArguments(new[] { "-o", @"C:\somedir\", "grammar.g4"})); | ||
} | ||
|
||
[TestMethod] | ||
[Description("Verifies that arguments with spaces in them are being enclosed in double quotes.")] | ||
public void JoinArgumentsQuotesArgsWithSpaces() | ||
{ | ||
Assert.AreEqual( | ||
@"-o ""C:\some dir"" grammar.g4", | ||
CommandLineHelper.JoinArguments(new[] { "-o", @"C:\some dir", "grammar.g4" })); | ||
} | ||
|
||
[TestMethod] | ||
[Description("Verifies that arguments with double quotes in them are being enclosed in double quotes and double quotes within the argument are properly escaped.")] | ||
public void JoinArgumentsAddsQuotesToArgsWithQuotes() | ||
{ | ||
Assert.AreEqual( | ||
@"-o ""C:\some\""dir"" grammar.g4", | ||
CommandLineHelper.JoinArguments(new[] { "-o", @"C:\some""dir", "grammar.g4" })); | ||
} | ||
|
||
[TestMethod] | ||
[Description("Verifies that arguments with backslash followed by double quote are being enclosed in double quotes and all special characters within the argument are properly escaped.")] | ||
public void JoinArgumentsEscapesBackSlashBeforeDoubleQuote() | ||
{ | ||
Assert.AreEqual( | ||
@"-o ""C:\some\\\""dir"" grammar.g4", | ||
CommandLineHelper.JoinArguments(new[] { "-o", @"C:\some\""dir", "grammar.g4" })); | ||
} | ||
|
||
[TestMethod] | ||
[Description("Verifies that arguments with multiple backslashes followed by double quote are being enclosed in double quotes and all special characters within the argument are properly escaped.")] | ||
public void JoinArgumentsEscapesMultipleBackSlashesBeforeDoubleQuote() | ||
{ | ||
Assert.AreEqual( | ||
@"-o ""C:\some\\\\\""dir"" grammar.g4", | ||
CommandLineHelper.JoinArguments(new[] { "-o", @"C:\some\\""dir", "grammar.g4" })); | ||
} | ||
|
||
[TestMethod] | ||
[Description("Verifies that arguments with spaces and trailing backslash are being enclosed in double quotes and backslash within the argument is properly escaped.")] | ||
public void JoinArgumentsEscapesTrailingBackSlashesInQuotedArgs() | ||
{ | ||
Assert.AreEqual( | ||
@"-o ""C:\some dir\\"" grammar.g4", | ||
CommandLineHelper.JoinArguments(new[] { "-o", @"C:\some dir\", "grammar.g4" })); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Terence Parr, Sam Harwell. All Rights Reserved. | ||
// Licensed under the BSD License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Antlr4.Build.Tasks | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
/// <summary> | ||
/// Defines helper methods to work with command line and arguments. | ||
/// </summary> | ||
public static class CommandLineHelper | ||
{ | ||
/// <summary> | ||
/// Joins multiple command line arguments together into one string using space as a separator, | ||
/// applying all necessary quoting and escaping. | ||
/// </summary> | ||
/// <param name="arguments">Command line argument to join</param> | ||
/// <returns>String that contains all command line arguments joined together.</returns> | ||
public static string JoinArguments(IEnumerable<string> arguments) | ||
{ | ||
if (arguments == null) | ||
throw new ArgumentNullException("arguments"); | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
foreach (string argument in arguments) | ||
{ | ||
if (builder.Length > 0) | ||
builder.Append(' '); | ||
|
||
if (argument.IndexOfAny(new[] { '"', ' ' }) < 0) | ||
{ | ||
builder.Append(argument); | ||
continue; | ||
} | ||
|
||
// escape backslashes appearing before a double quote or end of line by doubling them | ||
string arg = Regex.Replace(argument, @"(\\+)(""|$)", "$1$1$2"); | ||
// escape double quotes | ||
arg = arg.Replace("\"", "\\\""); | ||
|
||
// wrap the argument in outer quotes | ||
builder.Append('"').Append(arg).Append('"'); | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't want tests/new helper class, then all it takes to fix it is to replace this line in the original method.