diff options
Diffstat (limited to 'modules/test/test-shell-script.js')
| -rw-r--r-- | modules/test/test-shell-script.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/modules/test/test-shell-script.js b/modules/test/test-shell-script.js index 578b186..7b19fa6 100644 --- a/modules/test/test-shell-script.js +++ b/modules/test/test-shell-script.js @@ -145,6 +145,32 @@ async function runScript(seed, lines) { ok('D10 sequential statements all run', r.printed.join('|') === 'a|b', r.printed.join('|')); } - if (fails === 0) console.log('test-shell-script: ' + passes + ' passed, 0 failed'); + // D. if-conditions: the `test` keyword and the bracket form (both were false before v4.6.119) +{ + const files = { '/work/alpha.txt': 'one\ntwo' }; + shell.terminal = { + fs: { + readFile: (p) => (Object.prototype.hasOwnProperty.call(files, p) ? files[p] : null), + readDir: (p) => (p === '/work' ? [{ name: 'alpha.txt', type: 'file' }, { name: 'beta.txt', type: 'file' }] : null), + currentPath: ['work'], + }, + }; + ok('D1 "test -f" is true for an existing file', shell._evalTest('test -f /work/alpha.txt', {}) === true); + ok('D2 "test -f" is false for a missing file', shell._evalTest('test -f /work/nope.txt', {}) === false); + ok('D3 "[ -f … ]" is true for an existing file', shell._evalTest('[ -f /work/alpha.txt ]', {}) === true); + ok('D4 "[ -f … ]" is false for a missing file', shell._evalTest('[ -f /work/nope.txt ]', {}) === false); + ok('D5 "test -d" is true for a directory', shell._evalTest('test -d /work', {}) === true); + ok('D6 "! test -f" inverts', shell._evalTest('! test -f /work/nope.txt', {}) === true); +} + +// E. for-items go through glob expansion (measured live: the loop ran once with $f = the pattern) +{ + const src = fsMod.readFileSync(path.join(ROOT, 'ext/shell.js'), 'utf8'); + ok('E1 the for branch expands globs per item', /_expandGlobs\(\[expanded\], cwd\)/.test(src), 'for-branch expansion missing'); + ok('E2 _expandGlobs turns *.txt into the real files', shell._expandGlobs(['*.txt'], ['work']).join(',') === '/work/alpha.txt,/work/beta.txt', + JSON.stringify(shell._expandGlobs(['*.txt'], ['work']))); +} + +if (fails === 0) console.log('test-shell-script: ' + passes + ' passed, 0 failed'); else { console.log('test-shell-script: ' + passes + ' passed, ' + fails + ' FAILED'); process.exit(1); } })(); |