CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
quote.js129 linesDownload Raw Back to test
1'use strict';2 3var test = require('tape');4var quote = require('../').quote;5 6test('quote', function (t) {7	t.equal(quote(['a', 'b', 'c d']), 'a b \'c d\'');8	t.equal(9		quote(['a', 'b', "it's a \"neat thing\""]),10		'a b "it\'s a \\"neat thing\\""'11	);12	t.equal(13		quote(['$', '`', '\'']),14		'\\$ \\` "\'"'15	);16	t.equal(quote([]), '');17	t.equal(quote(['a\nb']), "'a\nb'");18	t.equal(quote([' #(){}*|][!']), "' #(){}*|][!'");19	t.equal(quote(["'#(){}*|][!"]), '"\'#(){}*|][\\!"');20	t.equal(quote(['X#(){}*|][!']), 'X\\#\\(\\)\\{\\}\\*\\|\\]\\[\\!');21	t.equal(quote(['a\n#\nb']), "'a\n#\nb'");22	t.equal(quote(['><;{}']), '\\>\\<\\;\\{\\}');23	t.equal(quote(['a', 1, true, false]), 'a 1 true false');24	t.equal(quote(['a', 1, null, undefined]), 'a 1 null undefined');25	t.equal(quote(['a\\x']), "'a\\x'");26	t.equal(quote(['a"b']), '\'a"b\'');27	t.equal(quote(['"a"b"']), '\'"a"b"\'');28	t.equal(quote(['a\\"b']), '\'a\\"b\'');29	t.equal(quote(['a\\b']), '\'a\\b\'');30	t.end();31});32 33test('quote tilde (escapes leading ~ to prevent shell tilde-expansion)', function (t) {34	t.equal(quote(['~']), '\\~');35	t.equal(quote(['~/foo']), '\\~/foo');36	t.equal(quote(['~root']), '\\~root');37	t.equal(quote(['~root/x']), '\\~root/x');38	t.equal(quote(['~+']), '\\~+');39	t.equal(quote(['~-']), '\\~-');40	t.equal(quote(['a~b']), 'a\\~b');41	t.equal(quote(['x~']), 'x\\~');42	t.end();43});44 45test('quote ops', function (t) {46	t.equal(quote(['a', { op: '|' }, 'b']), 'a \\| b');47	t.equal(48		quote(['a', { op: '&&' }, 'b', { op: ';' }, 'c']),49		'a \\&\\& b \\; c'50	);51	t.end();52});53 54test('quote windows paths', { skip: 'breaking change, disabled until 2.x' }, function (t) {55	var path = 'C:\\projects\\node-shell-quote\\index.js';56 57	t.equal(quote([path, 'b', 'c d']), 'C:\\projects\\node-shell-quote\\index.js b \'c d\'');58 59	t.end();60});61 62test("chars for windows paths don't break out", function (t) {63	var x = '`:\\a\\b';64	t.equal(quote([x]), "'`:\\a\\b'");65	t.end();66});67 68test('empty strings', function (t) {69	t.equal(quote(['-x', '', 'y']), '-x \'\' y');70 71	t.end();72});73 74test('quote ops: allowlist', function (t) {75	var ops = ['||', '&&', ';;', '|&', '<(', '<<<', '>>', '>&', '<&', '&', ';', '(', ')', '|', '<', '>'];76	for (var i = 0; i < ops.length; i++) {77		var op = ops[i];78		var expected = '';79		for (var j = 0; j < op.length; j++) { expected += '\\' + op.charAt(j); }80		t.equal(quote([{ op: op }]), expected, 'op ' + op);81	}82	t.end();83});84 85test('quote ops: rejects line terminators (GHSA-w7jw-789q-3m8p)', function (t) {86	t['throws'](function () { quote([{ op: ';\nid' }]); }, TypeError, 'newline in op');87	t['throws'](function () { quote([{ op: ';\rid' }]); }, TypeError, 'carriage return in op');88	t['throws'](function () { quote([{ op: ';\u2028id' }]); }, TypeError, 'U+2028 in op');89	t['throws'](function () { quote([{ op: ';\u2029id' }]); }, TypeError, 'U+2029 in op');90	t.end();91});92 93test('quote ops: rejects non-allowlisted values', function (t) {94	t['throws'](function () { quote([{ op: '' }]); }, TypeError, 'empty op');95	t['throws'](function () { quote([{ op: 'foo' }]); }, TypeError, 'arbitrary string');96	t['throws'](function () { quote([{ op: '|||' }]); }, TypeError, 'near-miss');97	t['throws'](function () { quote([{ op: 42 }]); }, TypeError, 'non-string op');98	t.end();99});100 101test('quote glob pattern', function (t) {102	t.equal(quote([{ op: 'glob', pattern: 'test/*.test.js' }]), 'test/*.test.js');103	t.equal(quote([{ op: 'glob', pattern: '?ab' }]), '?ab');104	t.equal(quote([{ op: 'glob', pattern: '[ab]c' }]), '[ab]c');105	t.equal(quote([{ op: 'glob', pattern: '{a,b}' }]), '{a,b}');106	t.equal(quote([{ op: 'glob', pattern: 'my dir/*.txt' }]), 'my\\ dir/*.txt');107	t.equal(quote([{ op: 'glob', pattern: 'a$b' }]), 'a\\$b');108	t['throws'](function () { quote([{ op: 'glob' }]); }, TypeError, 'missing pattern');109	t['throws'](function () { quote([{ op: 'glob', pattern: 'a\nb' }]); }, TypeError, 'newline in pattern');110	t['throws'](function () { quote([{ op: 'glob', pattern: 'a\u2028b' }]); }, TypeError, 'U+2028 in pattern');111	t.end();112});113 114test('quote comment', function (t) {115	t.equal(quote(['echo', 'hi', { comment: ' a comment' }]), 'echo hi # a comment');116	t.equal(quote([{ comment: '' }]), '#');117	t['throws'](function () { quote([{ comment: 'a\nb' }]); }, TypeError, 'newline in comment');118	t['throws'](function () { quote([{ comment: 'a\rb' }]); }, TypeError, 'CR in comment');119	t['throws'](function () { quote([{ comment: 'a\u2028b' }]); }, TypeError, 'U+2028 in comment');120	t.end();121});122 123test('quote rejects unrecognized object shapes', function (t) {124	t['throws'](function () { quote([{}]); }, TypeError, 'empty object');125	t['throws'](function () { quote([{ foo: 'bar' }]); }, TypeError, 'unknown key');126	t['throws'](function () { quote([{ op: null }]); }, TypeError, 'null op');127	t.end();128});129 
basant307/AI_Governance_Project · CoolFace