Commit 445f82d9 authored by Caleb C. Sander's avatar Caleb C. Sander
Browse files

Fix path separators passed to grep

parent 1cef1e77
No related merge requests found
Showing with 12 additions and 5 deletions
+12 -5
const fs = require('fs')
const test = require('ava')
const {execFilePromise, matchOutput, reorderings, updateExpectedPaths} = require('./util')
const {
execFilePromise, matchOutput, reorderings, updateExpectedPaths, updatePath
} = require('./util')
// We are going to run the server directly, not through npm
const {INIT_CWD, ...GREP_ENV} = process.env
......@@ -557,6 +559,8 @@ const TESTS = [
for (const {args, expected} of TESTS) {
const flags = args.filter(arg => arg.startsWith('-'))
const parameters = args.filter(arg => !arg.startsWith('-'))
// Update all paths in the parameters to have correct separators
for (let i = 1; i < parameters.length; i++) parameters[i] = updatePath(parameters[i])
for (const args of reorderings(parameters, flags)) {
test(`grep ${args.join(' ')}`, async t => {
const {stdout} = await execFilePromise(
......@@ -573,7 +577,7 @@ test('grep no matches', async t => {
let stdout
try {
({stdout} = await execFilePromise(
'node', ['../grep.js', 'no-such-string', 'files/independence.txt'],
'node', ['../grep.js', 'no-such-string', updatePath('files/independence.txt')],
{env: GREP_ENV}
))
}
......@@ -606,7 +610,7 @@ 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, updatePath('files/long.txt')],
{env: GREP_ENV}
)
const expectedLines = lines.filter(line => line.includes(search))
......@@ -620,7 +624,7 @@ 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}`, updatePath('files/long-lines.txt')],
{env: GREP_ENV}
)
t.deepEqual(stdout, lines[i] + '\n')
......
......@@ -17,12 +17,15 @@ function* reorderings(fixed, reorderable) {
}
exports.reorderings = reorderings
const updatePath = filename => path.join(...filename.split('/'))
exports.updatePath = updatePath
/** Updates the path separators in an expected output to match the OS's separator */
exports.updateExpectedPaths = expected =>
expected.map(lineGroup => lineGroup.map(line => {
const pathEnd = line.indexOf(':')
if (pathEnd < 0) throw new Error('Line does not have a path')
return path.join(...line.slice(0, pathEnd).split('/')) + line.slice(pathEnd)
return updatePath(line.slice(0, pathEnd)) + line.slice(pathEnd)
}))
function toLines(text) {
......
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