Commit 1cef1e77 authored by Caleb C. Sander's avatar Caleb C. Sander
Browse files

Fix #4

parent eba3bbb5
Showing with 21 additions and 6 deletions
+21 -6
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)
......
......@@ -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": {
......
......@@ -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')
}
......
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