UNPKG

1.84 kBJavaScriptView Raw
1var assert = require('assert');
2var shellescape = require('./shellEscape');
3
4//simple
5var args = ['curl', '-v', '-H', 'Location;', '-H', 'User-Agent: dave#10', 'http://www.daveeddy.com/?name=dave&age=24'];
6
7var escaped = shellescape(args);
8
9assert.strictEqual(escaped, "curl -v -H 'Location;' -H 'User-Agent: dave#10' 'http://www.daveeddy.com/?name=dave&age=24'");
10console.log(escaped);
11
12
13//more
14var d = {
15 "echo 'hello\\nworld'": ['echo', 'hello\\nworld'],
16 "echo 'hello\\tworld'": ['echo', 'hello\\tworld'],
17 "echo '\thello\nworld'\\'": ['echo', '\thello\nworld\''],
18 "echo 'hello world'": ['echo', 'hello world'],
19 "echo hello world": ['echo', 'hello', 'world'],
20 "echo 'hello\\\\'\\' \\''\\\\'\\''world'": ["echo", "hello\\\\'", "'\\\\'world"],
21 "echo hello 'world\\'": ["echo", "hello", "world\\"]
22};
23
24Object.keys(d).forEach(function(s) {
25 var escaped = shellescape(d[s]);
26 assert.strictEqual(escaped, s);
27 console.log(s);
28});
29
30
31//advanced
32var args = ['echo', 'hello!', 'how are you doing $USER', '"double"', "'single'"];
33
34var escaped = shellescape(args);
35assert.strictEqual(escaped, "echo 'hello!' 'how are you doing $USER' '\"double\"' \\''single'\\'");
36console.log(escaped);
37
38//my tests
39var escaped = shellescape(['cat', 'file.csv', '>', 'otherthing.csv']);
40assert.strictEqual(escaped, "cat 'file.csv' > 'otherthing.csv'");
41console.log(escaped);
42
43var escaped = shellescape(['cat', 'file.csv', '|', 'mappy', '>', 'otherthing.csv']);
44assert.strictEqual(escaped, "cat 'file.csv' | mappy > 'otherthing.csv'");
45console.log(escaped);
46
47var escaped = shellescape(['cat', 'file.csv?&', '|', 'mappy', '>', 'otherthing.csv']);
48assert.strictEqual(escaped, "cat 'file.csv?&' | mappy > 'otherthing.csv'");
49console.log(escaped);
50
51var escaped = shellescape(['hi\'?']);
52console.log(escaped);
53assert.strictEqual(escaped, "'hi'\\''?'");
\No newline at end of file