Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Fix #876, Correct handling of quotation marks in commands #1931

Merged
merged 1 commit into from
Jun 9, 2015

Conversation

dougbu
Copy link
Member

@dougbu dougbu commented May 22, 2015

  • don't strip quotes surrounding command-line arguments when used in scripts
    • prevented command paths and grouping arguments containing spaces
    • pass preserveSurroundingQuotes into CommandGrammar to control stripping
  • support expected shell capabilities such as quoting command and redirections
    • use cmd /s /c when executing scripts on Windows
    • use /bin/bash -c when executing scripts on Linux
  • add functional tests of commands and scripts (together with dnu restore)
    • no dnu restore for .NET Core due to extra time required

note:

  • CommandGrammar may need additional generalizations
    • e.g. unquoted term can't contain more than one escape sequence
    • but it's probably good enough for now
  • not adding quotes around variables replacements inserted into commands
    • no way of determining if replacement is already quoted
    • basically, leave it to project.json author to perform appropriate quoting for their platform

nits:

  • let VS do its thing with test services
  • make CommandGrammar constructor private; used only in Process() method
  • update a few ScriptExecutor comments, long lines, et cetera

@dougbu
Copy link
Member Author

dougbu commented May 22, 2015

/cc @davidfowl

Any suggestions on unit / integration / functional testing of scripts? I don't see anything in this repo at the moment.

@dougbu
Copy link
Member Author

dougbu commented May 23, 2015

@davidfowl since you like to look at individual commits, I rebased and added a couple more 😺

Latest version is more resilient and I've tested it numerous ways -- manually. (Don't seem to have test infrastructure in place to automated anything here.)

@dougbu
Copy link
Member Author

dougbu commented May 24, 2015

Rebased again and added tests.

@davidfowl this bug is for Beta 5.

@davidfowl
Copy link
Member

@dougbu This is broken on *nix. Look at travis


[ConditionalTheory]
[MemberData(nameof(RuntimeComponents))]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because i am positive my bash-writing skills are out-of-date and don't have a Linux system to play on. We can follow-up later with additional scripts.

@dougbu
Copy link
Member Author

dougbu commented May 24, 2015

@davidfowl Travis builds now successful because all new tests are disabled on Mac and Linux. AppVeyor build queued; no idea when it will start but local verify builds are fine.

@dougbu
Copy link
Member Author

dougbu commented May 24, 2015

Should have mentioned: No regressions on Mac or Linux i.e. I disabled only my new tests there.


Assert.Equal(0, exitCode);
Assert.Empty(error);
Assert.Equal(expectedOutput, output);
Copy link
Contributor

Choose a reason for hiding this comment

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

DNX_TRACE is turned on by default in our TeamCity CI and you'll get extra info in output.

Use Assert.Contains() or turn off DNX_TRACE in the subprocess with something like:
https://github.com/aspnet/dnx/blob/dev/test/Bootstrapper.FunctionalTests/BootstrapperTests.cs#L114

Copy link
Member Author

Choose a reason for hiding this comment

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

🆗 I turned off DNX_TRACE for other added tests in this PR. Will do the same here.

args[index] = arg.Substring(1, arg.Length - 2);
}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

As @davidfowl and I discussed, will undo this change.

@dougbu dougbu force-pushed the dougbu/script.spaces.876 branch 6 times, most recently from af4b0aa to 96f85ca Compare June 3, 2015 06:25
scriptArguments = new[] { "/bin/bash", "-c", "\"" }
.Concat(scriptArguments.Select(argument => argument.Replace("\"", "\\\"")))
.Concat(new[] { "\"" })
.ToArray();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

/cc @troydai I fixed the problems in this else block that we discussed today. E.g. code no longer checks for the script file in whatever random directory dnu was executing.

@dougbu
Copy link
Member Author

dougbu commented Jun 3, 2015

@davidfowl rebased, updated, and working on Linux. (Not sure why the Travix-ci/pr build hung but the push build succeeded.)

// Special-case a script name that, perhaps with added .sh, matches an existing file.
var surroundWithQuotes = false;
var scriptCandidate = scriptArguments[0];
if (scriptCandidate.StartsWith("\"", StringComparison.Ordinal) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you have spaces before the first quote or after the last one?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's possible in the project.json file. But CommandGrammar removes spaces between and before all arguments and ignores the (remote) possibility of an argument that contains more than a correctly-quoted string.

@victorhurdugaci
Copy link
Contributor

Looks good to me and I only had the two comments above

@victorhurdugaci
Copy link
Contributor

:shipit:

- don't strip quotes surrounding command-line arguments when used in `scripts`
 - prevented command paths and grouping arguments containing spaces
 - pass `preserveSurroundingQuotes` into `CommandGrammar` to control stripping
- support expected shell capabilities such as quoting command and redirections
 - use `cmd /s /c` when executing `scripts` on Windows
 - use `/bin/bash -c` when executing `scripts` on Linux
- add functional tests of commands and scripts (together with `dnu restore`)
 - no `dnu restore` for .NET Core due to extra time required

note:
- `CommandGrammar` may need additional generalizations
 - e.g. unquoted term can't contain more than one escape sequence
 - but it's probably good enough for now
- _not_ adding quotes around variables replacements inserted into commands
 - no way of determining if replacement is already quoted
 - basically, leave it to project.json author to perform appropriate quoting for their platform

nits:
- let VS do its thing with test services
- make `CommandGrammar` constructor private; used only in `Process()` method
- update a few `ScriptExecutor` comments, long lines, et cetera
@dougbu dougbu merged commit 90f1f03 into dev Jun 9, 2015
@dougbu
Copy link
Member Author

dougbu commented Jun 9, 2015

90f1f03

@dougbu dougbu deleted the dougbu/script.spaces.876 branch June 9, 2015 01:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants