parse.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. var test = require('tape');
  3. var parse = require('../').parse;
  4. var quote = require('../').quote;
  5. test('parse shell commands', function (t) {
  6. t.same(parse(''), [], 'parses an empty string');
  7. t['throws'](
  8. function () { parse('${}'); },
  9. Error,
  10. 'empty substitution throws'
  11. );
  12. t['throws'](
  13. function () { parse('${'); },
  14. Error,
  15. 'incomplete substitution throws'
  16. );
  17. t.same(parse('a \'b\' "c"'), ['a', 'b', 'c']);
  18. t.same(
  19. parse('beep "boop" \'foo bar baz\' "it\'s \\"so\\" groovy"'),
  20. ['beep', 'boop', 'foo bar baz', 'it\'s "so" groovy']
  21. );
  22. t.same(parse('a b\\ c d'), ['a', 'b c', 'd']);
  23. t.same(parse('\\$beep bo\\`op'), ['$beep', 'bo`op']);
  24. t.same(parse('echo "foo = \\"foo\\""'), ['echo', 'foo = "foo"']);
  25. t.same(parse(''), []);
  26. t.same(parse(' '), []);
  27. t.same(parse('\t'), []);
  28. t.same(parse('a"b c d"e'), ['ab c de']);
  29. t.same(parse('a\\ b"c d"\\ e f'), ['a bc d e', 'f']);
  30. t.same(parse('a\\ b"c d"\\ e\'f g\' h'), ['a bc d ef g', 'h']);
  31. t.same(parse("x \"bl'a\"'h'"), ['x', "bl'ah"]);
  32. // inside single quotes everything is literal, so a backslash does not escape
  33. // the closing quote and a quoted token must end at its first closing quote
  34. t.same(parse("'\\' '\\'"), ['\\', '\\'], 'single-quoted backslashes parse as two separate tokens');
  35. t.same(parse("'\\'\\''"), ["\\'"], 'single-quoted backslash joined with an escaped quote');
  36. t.same(parse(quote(['\\', '\\'])), ['\\', '\\'], 'quote/parse round-trips a pair of backslashes');
  37. t.same(parse("x bl^'a^'h'", {}, { escape: '^' }), ['x', "bl'a'h"]);
  38. t.same(parse('abcH def', {}, { escape: 'H' }), ['abc def']);
  39. t.deepEqual(parse('# abc def ghi'), [{ comment: ' abc def ghi' }], 'start-of-line comment content is unparsed');
  40. t.deepEqual(parse('xyz # abc def ghi'), ['xyz', { comment: ' abc def ghi' }], 'comment content is unparsed');
  41. t.deepEqual(parse('-x "" -y'), ['-x', '', '-y'], 'empty string is preserved');
  42. t.same(
  43. parse('2;b', {}, { escape: 'd' }),
  44. [{ op: '2;b' }],
  45. 'control char in unquoted context mid-token with regex-special escape returns op'
  46. );
  47. t.end();
  48. });
  49. test('single quotes are literal', function (t) {
  50. t.same(parse("'\\'\\'"), ["\\'"], 'close-escape-reopen produces a quote after a quoted backslash');
  51. t.same(parse("'a'\\''b'"), ["a'b"], 'close-escape-reopen embeds a quote mid-word');
  52. t.same(parse("'\\'x"), ['\\x'], 'bareword joins a preceding quoted backslash');
  53. t.same(parse("a'\\'b"), ['a\\b'], 'quoted backslash joins surrounding barewords');
  54. t.same(parse("''"), [''], 'empty single quotes produce an empty token');
  55. t.same(parse("''a''"), ['a'], 'empty single quotes join adjacent content');
  56. t.same(parse("'*'"), ['*'], 'quoted glob char is a plain string, not a glob');
  57. t.end();
  58. });
  59. test('unmatched single quotes', function (t) {
  60. // real shells reject unterminated quotes; parse is lenient, and these pin the shape of that leniency
  61. t.same(parse("'"), [], 'a lone quote is dropped');
  62. t.same(parse("'a"), ['a'], 'an unterminated quote keeps its content');
  63. t.same(parse("a'b"), ['a', 'b'], 'an unmatched quote mid-word splits the token');
  64. t.end();
  65. });
  66. test('nested parameter expansion', function (t) {
  67. t.same(parse('${a${b}c}'), [''], 'a nested ${} is consumed as one substitution, not split at the first }');
  68. t.same(parse('${a${b}}'), [''], 'a nested ${} at the end is consumed as one substitution');
  69. t.same(parse('${foo{bar}'), [''], 'a lone { that is not part of a nested ${} does not change brace depth');
  70. t.same(
  71. parse('level=${levels[$RANDOM%${#levels[@]}]}'),
  72. ['level='],
  73. 'a nested array-index expansion is consumed whole, without leaking a partial token'
  74. );
  75. t.end();
  76. });
  77. test('splitUnquoted: field-splits unquoted variable expansion (#1)', function (t) {
  78. var env = { T: 'c d', E: '', S: ' c d ', W: ' ' };
  79. var opts = { splitUnquoted: true };
  80. t.same(parse('test a b $T', env, opts), ['test', 'a', 'b', 'c', 'd'], 'unquoted expansion splits into separate tokens');
  81. t.same(parse('a$T', env, opts), ['ac', 'd'], 'the first field joins the preceding text');
  82. t.same(parse('$T x', env, opts), ['c', 'd', 'x'], 'the last field is a token of its own');
  83. t.same(parse('x${T}y', env, opts), ['xc', 'dy'], 'fields join text on both sides');
  84. t.same(parse('$S', env, opts), ['c', 'd'], 'leading, trailing, and repeated whitespace collapses');
  85. t.same(parse('a$S', env, opts), ['a', 'c', 'd'], 'leading whitespace closes the preceding field');
  86. t.same(parse('$W', env, opts), [], 'an all-whitespace expansion produces no tokens');
  87. t.same(parse('a$W b', env, opts), ['a', 'b'], 'an all-whitespace expansion just separates fields');
  88. t.same(parse('$E', env, opts), [], 'an empty unquoted expansion produces no token');
  89. t.same(parse('"$T"', env, opts), ['c d'], 'a quoted expansion is never split');
  90. t.same(parse('-x "" -y', env, opts), ['-x', '', '-y'], 'a quoted empty string is still preserved');
  91. t.same(parse('a $F b', function () { return 'c d'; }, opts), ['a', 'c', 'd', 'b'], 'the env-function path splits too');
  92. t.same(parse('test a b $T', env), ['test', 'a', 'b', 'c d'], 'without the option, unquoted expansion is not split');
  93. t.end();
  94. });
  95. test('splitUnquoted: a string value is a custom IFS (#1)', function (t) {
  96. function o(ifs) { return { splitUnquoted: ifs }; }
  97. t.same(parse('${V}', { V: 'a:b' }, o(':')), ['a', 'b'], 'a non-whitespace IFS char splits fields');
  98. t.same(parse('${V}', { V: 'a::b' }, o(':')), ['a', '', 'b'], 'adjacent non-whitespace delimiters yield an empty field');
  99. t.same(parse('${V}', { V: ':a:' }, o(':')), ['', 'a'], 'a leading delimiter yields a leading empty; a trailing one does not');
  100. t.same(parse('${V}', { V: 'a::' }, o(':')), ['a', ''], 'a trailing double delimiter yields one empty field');
  101. t.same(parse('${V}', { V: ':' }, o(':')), [''], 'a lone delimiter yields a single empty field');
  102. t.same(parse('${V}', { V: '::' }, o(':')), ['', ''], 'two delimiters yield two empty fields');
  103. t.same(parse('${V}${W}', { V: 'a:', W: ':b' }, o(':')), ['a', '', 'b'], 'delimiters spanning an expansion boundary merge into one run');
  104. t.same(parse('a${V}${W}z', { V: ':x:', W: ':y:' }, o(':')), ['a', 'x', '', 'y', 'z'], 'fields join literal text on both sides across expansions');
  105. t.same(parse('${V}', { V: 'a : b' }, o(' :')), ['a', 'b'], 'whitespace around a non-whitespace delimiter is absorbed');
  106. t.same(parse('${V}', { V: 'a b:c' }, o(' :')), ['a', 'b', 'c'], 'mixed IFS: whitespace and non-whitespace each delimit');
  107. t.same(parse('${V}', { V: 'a,b' }, o(',')), ['a', 'b'], 'any character can be the IFS');
  108. t.same(parse('${V}', { V: 'a b' }, o('')), ['a b'], 'an empty IFS string disables splitting');
  109. t.end();
  110. });
  111. test('parse stays linear in token count (GHSA-395f-4hp3-45gv)', function (t) {
  112. // the old concat-in-reduce finalizer was O(n^2): this many tokens took
  113. // ~minutes, so under the unfixed code this test hangs rather than passes
  114. var n = 2e5;
  115. var input = new Array(n + 1).join('x '); // avoid String#repeat for old engines
  116. var words = parse(input);
  117. t.equal(words.length, n, 'every token is returned');
  118. t.equal(words[0], 'x', 'first token is correct');
  119. t.equal(words[n - 1], 'x', 'last token is correct');
  120. var withEnv = parse(input, function () { return 'v'; });
  121. t.equal(withEnv.length, n, 'env-function path returns every token');
  122. t.end();
  123. });