shellwords_spec.coffee
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
shellwords = require "../src/shellwords"
describe "Shellwords", ->
describe "#split", ->
it "splits normal words", ->
results = shellwords.split "foo bar baz"
(expect results).toEqual ["foo", "bar", "baz"]
it "splits single quoted phrases", ->
results = shellwords.split "foo 'bar baz'"
(expect results).toEqual ["foo", "bar baz"]
it "splits double quoted phrases", ->
results = shellwords.split '"foo bar" baz'
(expect results).toEqual ["foo bar", "baz"]
it "respects escaped characters", ->
results = shellwords.split "foo\\ bar baz"
(expect results).toEqual ["foo bar", "baz"]
it "respects escaped characters within single quotes", ->
results = shellwords.split "foo 'bar\\ baz'"
(expect results).toEqual ["foo", "bar baz"]
it "respects escaped characters within double quotes", ->
results = shellwords.split 'foo "bar\\ baz"'
(expect results).toEqual ["foo", "bar baz"]
it "respects escaped quotes within quotes", ->
results = shellwords.split 'foo "bar\\" baz"'
(expect results).toEqual ['foo', 'bar" baz']
results = shellwords.split "foo 'bar\\' baz'"
(expect results).toEqual ["foo", "bar' baz"]
it "throws on unmatched single quotes", ->
fn = ->
shellwords.split "foo 'bar baz"
(expect fn).toThrow()
it "throws on unmatched double quotes", ->
fn = ->
shellwords.split 'foo "bar baz'
(expect fn).toThrow()
describe "#escape", ->
it "escapes a string to be safe for shell command line", ->
results = shellwords.escape "foo '\"' bar"
(expect results).toEqual "foo\\ \\'\\\"\\'\\ bar"
it "dummy escapes any multibyte chars", ->
results = shellwords.escape "あい"
(expect results).toEqual "\\あ\\い"