From 1cef1e773157746b948905947712b01fcc1bcaab Mon Sep 17 00:00:00 2001 From: Caleb Sander <caleb.sander@gmail.com> Date: Fri, 29 Jan 2021 23:17:02 -0500 Subject: [PATCH] Fix #4 --- grep.js | 6 +++++- package.json | 1 + tests/test.js | 20 +++++++++++++++----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/grep.js b/grep.js index d67f2ab..ca4ed0f 100644 --- a/grep.js +++ b/grep.js @@ -1,6 +1,10 @@ const fs = require('fs') const path = require('path') +// If running as `npm run grep`, use the current directory the command was run in +const {INIT_CWD} = process.env +if (INIT_CWD) process.chdir(INIT_CWD) + /** * Calls a callback function on every file inside a directory. * This is like fs.readdir(), but recurses on subdirectories. @@ -46,7 +50,7 @@ for (const arg of process.argv.slice(2)) { else pattern = arg } } -if (!pattern) throw new Error('Syntax: node grep.js [-i] [-r] [-v] [-z] pattern ...files') +if (!pattern) throw new Error('Syntax: npm run grep [-i] [-r] [-v] [-z] pattern ...files') // Prevent warnings when piping many streams to `process.stdout` process.stdout.setMaxListeners(Infinity) diff --git a/package.json b/package.json index a3cf7ad..554d115 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "CS 11 Asynchronous Programming - Grep", "scripts": { + "grep": "node grep.js", "test": "ava --verbose tests/test.js" }, "repository": { diff --git a/tests/test.js b/tests/test.js index 78ec8db..f76e9b8 100644 --- a/tests/test.js +++ b/tests/test.js @@ -2,6 +2,8 @@ const fs = require('fs') const test = require('ava') const {execFilePromise, matchOutput, reorderings, updateExpectedPaths} = require('./util') +// We are going to run the server directly, not through npm +const {INIT_CWD, ...GREP_ENV} = process.env process.chdir(__dirname) const RUST_MUTABLE_EXPECTED = updateExpectedPaths([ @@ -557,7 +559,10 @@ for (const {args, expected} of TESTS) { const parameters = args.filter(arg => !arg.startsWith('-')) for (const args of reorderings(parameters, flags)) { test(`grep ${args.join(' ')}`, async t => { - const {stdout} = await execFilePromise('node', ['../grep.js', ...args]) + const {stdout} = await execFilePromise( + 'node', ['../grep.js', ...args], + {env: GREP_ENV} + ) matchOutput(t, stdout, expected) }) } @@ -567,7 +572,10 @@ for (const {args, expected} of TESTS) { test('grep no matches', async t => { let stdout try { - ({stdout} = await execFilePromise('node', ['../grep.js', 'no-such-string', 'files/independence.txt'])) + ({stdout} = await execFilePromise( + 'node', ['../grep.js', 'no-such-string', 'files/independence.txt'], + {env: GREP_ENV} + )) } catch (e) { ({stdout} = e) } matchOutput(t, stdout, []) @@ -575,7 +583,7 @@ test('grep no matches', async t => { // Test grepping the standard input test('grep stdin', async t => { - const grepPromise = execFilePromise('node', ['../grep.js', 'Rust']) + const grepPromise = execFilePromise('node', ['../grep.js', 'Rust'], {env: GREP_ENV}) fs.createReadStream('files/rust-book/SUMMARY.md') .on('error', _ => t.fail('Failed to open file')) .pipe(grepPromise.child.stdin) @@ -598,7 +606,8 @@ test('multiple-chunk input file', async t => { for (let i = 0; i < 10; i++) { const search = `${i}` const {stdout} = await execFilePromise( - 'node', ['../grep.js', search, 'files/long.txt'] + 'node', ['../grep.js', search, 'files/long.txt'], + {env: GREP_ENV} ) const expectedLines = lines.filter(line => line.includes(search)) t.deepEqual(stdout, expectedLines.map(line => `${line}\n`).join('')) @@ -611,7 +620,8 @@ test('multiple-chunk lines', async t => { const lines = contents.trim().split('\n') for (let i = 0; i < 10; i++) { const {stdout} = await execFilePromise( - 'node', ['../grep.js', `${i}`, 'files/long-lines.txt'] + 'node', ['../grep.js', `${i}`, 'files/long-lines.txt'], + {env: GREP_ENV} ) t.deepEqual(stdout, lines[i] + '\n') } -- GitLab