"git@gitlab.caltech.edu:cs24-19fa/git_rec_nano.git" did not exist on "b8d32d8b7b3312db868f36c2d8e4e9855bd7cef4"
Commit 1cef1e77 authored by Caleb C. Sander's avatar Caleb C. Sander
Browse files

Fix #4

parent eba3bbb5
No related merge requests found
Showing with 21 additions and 6 deletions
+21 -6
const fs = require('fs') const fs = require('fs')
const path = require('path') 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. * Calls a callback function on every file inside a directory.
* This is like fs.readdir(), but recurses on subdirectories. * This is like fs.readdir(), but recurses on subdirectories.
...@@ -46,7 +50,7 @@ for (const arg of process.argv.slice(2)) { ...@@ -46,7 +50,7 @@ for (const arg of process.argv.slice(2)) {
else pattern = arg 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` // Prevent warnings when piping many streams to `process.stdout`
process.stdout.setMaxListeners(Infinity) process.stdout.setMaxListeners(Infinity)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
"version": "1.0.0", "version": "1.0.0",
"description": "CS 11 Asynchronous Programming - Grep", "description": "CS 11 Asynchronous Programming - Grep",
"scripts": { "scripts": {
"grep": "node grep.js",
"test": "ava --verbose tests/test.js" "test": "ava --verbose tests/test.js"
}, },
"repository": { "repository": {
......
...@@ -2,6 +2,8 @@ const fs = require('fs') ...@@ -2,6 +2,8 @@ const fs = require('fs')
const test = require('ava') const test = require('ava')
const {execFilePromise, matchOutput, reorderings, updateExpectedPaths} = require('./util') 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) process.chdir(__dirname)
const RUST_MUTABLE_EXPECTED = updateExpectedPaths([ const RUST_MUTABLE_EXPECTED = updateExpectedPaths([
...@@ -557,7 +559,10 @@ for (const {args, expected} of TESTS) { ...@@ -557,7 +559,10 @@ for (const {args, expected} of TESTS) {
const parameters = args.filter(arg => !arg.startsWith('-')) const parameters = args.filter(arg => !arg.startsWith('-'))
for (const args of reorderings(parameters, flags)) { for (const args of reorderings(parameters, flags)) {
test(`grep ${args.join(' ')}`, async t => { 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) matchOutput(t, stdout, expected)
}) })
} }
...@@ -567,7 +572,10 @@ for (const {args, expected} of TESTS) { ...@@ -567,7 +572,10 @@ for (const {args, expected} of TESTS) {
test('grep no matches', async t => { test('grep no matches', async t => {
let stdout let stdout
try { 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) } catch (e) { ({stdout} = e) }
matchOutput(t, stdout, []) matchOutput(t, stdout, [])
...@@ -575,7 +583,7 @@ test('grep no matches', async t => { ...@@ -575,7 +583,7 @@ test('grep no matches', async t => {
// Test grepping the standard input // Test grepping the standard input
test('grep stdin', async t => { 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') fs.createReadStream('files/rust-book/SUMMARY.md')
.on('error', _ => t.fail('Failed to open file')) .on('error', _ => t.fail('Failed to open file'))
.pipe(grepPromise.child.stdin) .pipe(grepPromise.child.stdin)
...@@ -598,7 +606,8 @@ test('multiple-chunk input file', async t => { ...@@ -598,7 +606,8 @@ test('multiple-chunk input file', async t => {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
const search = `${i}` const search = `${i}`
const {stdout} = await execFilePromise( 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)) const expectedLines = lines.filter(line => line.includes(search))
t.deepEqual(stdout, expectedLines.map(line => `${line}\n`).join('')) t.deepEqual(stdout, expectedLines.map(line => `${line}\n`).join(''))
...@@ -611,7 +620,8 @@ test('multiple-chunk lines', async t => { ...@@ -611,7 +620,8 @@ test('multiple-chunk lines', async t => {
const lines = contents.trim().split('\n') const lines = contents.trim().split('\n')
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
const {stdout} = await execFilePromise( 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') t.deepEqual(stdout, lines[i] + '\n')
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment