-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6d71b2
commit 0d361d9
Showing
3 changed files
with
62 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {OSType, getOSType} from './utils'; | ||
|
||
export function shellEscape(args: Array<string>): string { | ||
var output: Array<string> = []; | ||
|
||
if (getOSType() === OSType.windows) { | ||
args.forEach(function(arg) { | ||
if (/[^A-Za-z0-9_\/:=-]/.test(arg)) { | ||
arg = '"' + arg.replace(/"/g, '\\"') + '"'; | ||
arg = arg.replace(/^(?:"")+/g, '') // unduplicate double-quote at the beginning | ||
.replace(/\\"""/g, '\\"'); // remove non-escaped double-quote if there are enclosed between 2 escaped | ||
} | ||
output.push(arg); | ||
}); | ||
return output.join(' '); | ||
} else { | ||
args.forEach(function(arg) { | ||
if (/[^A-Za-z0-9_\/:=-]/.test(arg)) { | ||
arg = "'" + arg.replace(/'/g,"'\\''") + "'"; | ||
arg = arg.replace(/^(?:'')+/g, '') // unduplicate single-quote at the beginning | ||
.replace(/\\'''/g, "\\'"); // remove non-escaped single-quote if there are enclosed between 2 escaped | ||
}; | ||
output.push(arg); | ||
}); | ||
return output.join(' '); | ||
}; | ||
}; |
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,30 @@ | ||
import * as assert from 'assert'; | ||
import * as shellEscape from '../../src/shellEscape'; | ||
import * as sinon from 'sinon'; | ||
import * as utils from '../../src/utils'; | ||
|
||
|
||
suite('shellEscape', () => { | ||
let sandbox: sinon.SinonSandbox; | ||
|
||
setup(() => { | ||
sandbox = sinon.createSandbox(); | ||
}); | ||
|
||
teardown(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
suite('shellEscape', () => { | ||
test('non windows case: flag with spaces', () => { | ||
sandbox.stub(utils, 'getOSType').returns(utils.OSType.macOSarm); | ||
const output = shellEscape.shellEscape(['--project-name', 'test | whoami']); | ||
assert.strictEqual(output, '--project-name \'test | whoami\''); | ||
}); | ||
test.only('windows case: flag with space', () => { | ||
sandbox.stub(utils, 'getOSType').returns(utils.OSType.windows); | ||
const output = shellEscape.shellEscape(['--project-name', 'test | whoami']); | ||
assert.strictEqual(output, '--project-name \"test | whoami\"'); | ||
}) | ||
Check warning on line 28 in test/suite/shellEscape.test.ts GitHub Actions / build (ubuntu-latest)
|
||
}); | ||
}); |