Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
1 change: 1 addition & 0 deletions 1 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project.lock.json
/debug/
/staging/
/Packages/
*.nuget.props

# dotnet cli install/uninstall scripts
dotnet-install.ps1
Expand Down
2 changes: 2 additions & 0 deletions 2 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
Unreleased
----------

- Fix passing escaped double quoted spaces to native executables

v6.0.0-alpha.9 - 2016-08-15
---------------------------

Expand Down
16 changes: 16 additions & 0 deletions 16 build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,21 @@ function Get-PesterTag {
$o
}

function Publish-PSTestTools {
[CmdletBinding()]
param()

Find-Dotnet

# Publish EchoArgs so it can be run by tests
Push-Location "$PSScriptRoot/test/tools/EchoArgs"
try {
dotnet publish --output bin
} finally {
Pop-Location
}
}

function Start-PSPester {
[CmdletBinding()]
param(
Expand All @@ -563,6 +578,7 @@ function Start-PSPester {
)

Write-Verbose "Running pester tests at '$path' with tag '$($Tag -join ''', ''')' and ExcludeTag '$($ExcludeTag -join ''', ''')'" -Verbose
Publish-PSTestTools
# All concatenated commands/arguments are suffixed with the delimiter (space)
$Command = ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,20 @@ private void appendOneNativeArgument(ExecutionContext context, object obj, char
// just the normal double quotes, no other special quotes. Also note that mismatched
// quotes are supported.

bool needQuotes = false;
bool needQuotes = false, followingBackslash = false;
int quoteCount = 0;
for (int i = 0; i < arg.Length; i++)
{
if (arg[i] == '"')
if (arg[i] == '"' && !followingBackslash)
{
quoteCount += 1;
}
else if (char.IsWhiteSpace(arg[i]) && (quoteCount % 2 == 0))
{
needQuotes = true;
}

followingBackslash = arg[i] == '\\';
}

if (needQuotes)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Describe "Native Command Arguments" -tags "CI" {
# Find where test/powershell is so we can find the echoargs command relative to it
$powershellTestDir = $PSScriptRoot
while ($powershellTestDir -notmatch 'test[\\/]powershell$') {
$powershellTestDir = Split-Path $powershellTestDir
}
$echoArgs = Join-Path (Split-Path $powershellTestDir) tools/EchoArgs/bin/echoargs

# When passing arguments to native commands, quoted segments that contain
# spaces need to be quoted with '"' characters when they are passed to the
# native command (or to bash or sh on Linux).
#
# This test checks that the proper quoting is occuring by passing arguments
# to the echoargs native command and looking at how it got the arguments.
It "Should handle quoted spaces correctly" {
$a = 'a"b c"d'
$lines = & $echoArgs $a 'a"b c"d' a"b c"d
($lines | measure).Count | Should Be 3
$lines[0] | Should Be 'Arg 0 is <ab cd>'
$lines[1] | Should Be 'Arg 1 is <ab cd>'
$lines[2] | Should Be 'Arg 2 is <ab cd>'
}

# In order to pass '"' characters so they are actually part of command line
# arguments for native commands, they need to be escaped with a '\' (this
# is in addition to the '`' escaping needed inside '"' quoted strings in
# PowerShell).
#
# This functionality was broken in PowerShell 5.0 and 5.1, so this test
# will fail on those versions unless the fix is backported to them.
#
# This test checks that the proper quoting and escaping is occurring by
# passing arguments with escaped quotes to the echoargs native command and
# looking at how it got the arguments.
It "Should handle spaces between escaped quotes" {
$lines = & $echoArgs 'a\"b c\"d' "a\`"b c\`"d"
($lines | measure).Count | Should Be 2
$lines[0] | Should Be 'Arg 0 is <a"b c"d>'
$lines[1] | Should Be 'Arg 1 is <a"b c"d>'
}
}
25 changes: 25 additions & 0 deletions 25 test/tools/EchoArgs/EchoArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//---------------------------------------------------------------------

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you, please include url from which is was originally obtained to the header?

// Author: Keith Hill
// Source: https://github.com/Pscx/Pscx/blob/master/Src/EchoArgs/EchoArgs.cs
//
// Description: Very simple little console class that you can use to see
// how PowerShell is passing parameters to legacy console
// apps.
//
// Creation Date: March 06, 2006
//---------------------------------------------------------------------
using System;

namespace Pscx.Applications
{
class EchoArgs
{
static void Main(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg {0} is <{1}>", i, args[i]);
}
}
}
}
28 changes: 28 additions & 0 deletions 28 test/tools/EchoArgs/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "echoargs",
"version": "1.0.0-*",
"description": "Very simple little console class that you can use to see how PowerShell is passing parameters to legacy console apps.",

"buildOptions": {
"emitEntryPoint": true
},

"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": "1.0.0"
}
}
},

"runtimes": {
"ubuntu.16.04-x64": { },
"ubuntu.14.04-x64": { },
"debian.8-x64": { },
"centos.7-x64": { },
"win7-x64": { },
"win81-x64": { },
"win10-x64": { },
"osx.10.11-x64": { }
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.