basant307/AI_Governance_Project
048
1'use strict';2 3var test = require('tape');4var parse = require('../').parse;5 6test('parse shell commands', function (t) {7 t.same(parse(''), [], 'parses an empty string');8 9 t['throws'](10 function () { parse('${}'); },11 Error,12 'empty substitution throws'13 );14 t['throws'](15 function () { parse('${'); },16 Error,17 'incomplete substitution throws'18 );19 20 t.same(parse('a \'b\' "c"'), ['a', 'b', 'c']);21 t.same(22 parse('beep "boop" \'foo bar baz\' "it\'s \\"so\\" groovy"'),23 ['beep', 'boop', 'foo bar baz', 'it\'s "so" groovy']24 );25 t.same(parse('a b\\ c d'), ['a', 'b c', 'd']);26 t.same(parse('\\$beep bo\\`op'), ['$beep', 'bo`op']);27 t.same(parse('echo "foo = \\"foo\\""'), ['echo', 'foo = "foo"']);28 t.same(parse(''), []);29 t.same(parse(' '), []);30 t.same(parse('\t'), []);31 t.same(parse('a"b c d"e'), ['ab c de']);32 t.same(parse('a\\ b"c d"\\ e f'), ['a bc d e', 'f']);33 t.same(parse('a\\ b"c d"\\ e\'f g\' h'), ['a bc d ef g', 'h']);34 t.same(parse("x \"bl'a\"'h'"), ['x', "bl'ah"]);35 t.same(parse("x bl^'a^'h'", {}, { escape: '^' }), ['x', "bl'a'h"]);36 t.same(parse('abcH def', {}, { escape: 'H' }), ['abc def']);37 38 t.deepEqual(parse('# abc def ghi'), [{ comment: ' abc def ghi' }], 'start-of-line comment content is unparsed');39 t.deepEqual(parse('xyz # abc def ghi'), ['xyz', { comment: ' abc def ghi' }], 'comment content is unparsed');40 41 t.deepEqual(parse('-x "" -y'), ['-x', '', '-y'], 'empty string is preserved');42 43 t.same(44 parse('2;b', {}, { escape: 'd' }),45 [{ op: '2;b' }],46 'control char in unquoted context mid-token with regex-special escape returns op'47 );48 49 t.end();50});51 52test('parse stays linear in token count (GHSA-395f-4hp3-45gv)', function (t) {53 // the old concat-in-reduce finalizer was O(n^2): this many tokens took54 // ~minutes, so under the unfixed code this test hangs rather than passes55 var n = 2e5;56 var input = new Array(n + 1).join('x '); // avoid String#repeat for old engines57 58 var words = parse(input);59 t.equal(words.length, n, 'every token is returned');60 t.equal(words[0], 'x', 'first token is correct');61 t.equal(words[n - 1], 'x', 'last token is correct');62 63 var withEnv = parse(input, function () { return 'v'; });64 t.equal(withEnv.length, n, 'env-function path returns every token');65 66 t.end();67});68 