shellwords_spec.coffee 1.8 KB

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