Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs11-async
grep
Commits
1cef1e77
Commit
1cef1e77
authored
4 years ago
by
Caleb C. Sander
Browse files
Options
Download
Email Patches
Plain Diff
Fix #4
parent
eba3bbb5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
grep.js
+5
-1
grep.js
package.json
+1
-0
package.json
tests/test.js
+15
-5
tests/test.js
with
21 additions
and
6 deletions
+21
-6
grep.js
View file @
1cef1e77
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: n
ode
grep
.js
[-i] [-r] [-v] [-z] pattern ...files
'
)
if
(
!
pattern
)
throw
new
Error
(
'
Syntax: n
pm run
grep [-i] [-r] [-v] [-z] pattern ...files
'
)
// Prevent warnings when piping many streams to `process.stdout`
process
.
stdout
.
setMaxListeners
(
Infinity
)
...
...
This diff is collapsed.
Click to expand it.
package.json
View file @
1cef1e77
...
...
@@ -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"
:
{
...
...
This diff is collapsed.
Click to expand it.
tests/test.js
View file @
1cef1e77
...
...
@@ -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
'
)
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help