-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
168 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`pipeUtils escape in quoted regexp on *nix should escape [\\] 1`] = `"\\\\[kworker\\\\\\\\0:0\\\\]"`; | ||
|
||
exports[`pipeUtils escape in quoted regexp on *nix should escape ^*$ 1`] = `"\\\\^ma\\\\*tch\\\\$"`; | ||
|
||
exports[`pipeUtils escape in quoted regexp on *nix should escape dots 1`] = `"com\\\\.company\\\\.bundle"`; | ||
|
||
exports[`pipeUtils escape in quoted regexp on *nix should escape double quotes with slash 1`] = `"\\\\\\"a"`; | ||
|
||
exports[`pipeUtils escape in quoted regexp on *nix should not escape non-special characters 1`] = `"bundle_name"`; | ||
|
||
exports[`pipeUtils escape in quoted regexp on win32 should escape only double quotes 1`] = `"^Hello\\\\s \\"\\".*\\"\\"$"`; | ||
|
||
exports[`pipeUtils escape in quoted string on *nix should escape double quotes with slash 1`] = `"Says \\\\\\"Hello\\\\\\""`; | ||
|
||
exports[`pipeUtils escape in quoted string on win32 should escape double quotes with double quotes 1`] = `"Says \\"\\"Hello\\"\\""`; | ||
|
||
exports[`pipeUtils search by fragment pipe command on *nix should use grep -e "fragment" 1`] = `"grep -e \\"^Hello\\\\s \\\\\\".*\\\\\\"$\\""`; | ||
|
||
exports[`pipeUtils search by fragment pipe command on win32 should use findstr /C:"fragment" 1`] = `"findstr /C:\\"^Hello\\\\s \\"\\".*\\"\\"$\\""`; | ||
|
||
exports[`pipeUtils search by regexp pipe command on *nix should use grep "regexp" 1`] = `"grep \\"^Hello\\\\s \\\\\\".*\\\\\\"$\\""`; | ||
|
||
exports[`pipeUtils search by regexp pipe command on win32 should use findstr /R /C:"regexp" 1`] = `"findstr /R /C:\\"^Hello\\\\s \\"\\".*\\"\\"$\\""`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
function win32Implementation() { | ||
const escapeInQuotedStringWin32 = (fragment) => fragment.replace(/"/g, '""'); | ||
const escapeInQuotedRegexpWin32 = escapeInQuotedStringWin32; | ||
const searchRegexpWin32 = (pattern) => `findstr /R /C:"${escapeInQuotedStringWin32(pattern)}"`; | ||
const searchFragmentWin32 = (fragment) => `findstr /C:"${escapeInQuotedStringWin32(fragment)}"`; | ||
|
||
return { | ||
escape: { | ||
inQuotedString: escapeInQuotedStringWin32, | ||
inQuotedRegexp: escapeInQuotedRegexpWin32, | ||
}, | ||
search: { | ||
regexp: searchRegexpWin32, | ||
fragment: searchFragmentWin32, | ||
}, | ||
}; | ||
} | ||
|
||
function nixImplementation() { | ||
const SPECIAL_CHARS = /(["\^\$\[\]\*\.\\])/g; | ||
|
||
const escapeInQuotedStringNix = (fragment) => fragment.replace(/"/g, '\\"'); | ||
const escapeInQuotedRegexpNix = (fragment) => fragment.replace(SPECIAL_CHARS, "\\$1"); | ||
const searchRegexpNix = (pattern) => `grep "${escapeInQuotedStringNix(pattern)}"`; | ||
const searchFragmentNix = (fragment) => `grep -e "${escapeInQuotedStringNix(fragment)}"`; | ||
|
||
return { | ||
escape: { | ||
inQuotedString: escapeInQuotedStringNix, | ||
inQuotedRegexp: escapeInQuotedRegexpNix, | ||
}, | ||
search: { | ||
regexp: searchRegexpNix, | ||
fragment: searchFragmentNix, | ||
}, | ||
}; | ||
} | ||
|
||
// istanbul ignore next | ||
module.exports = { | ||
...(process.platform === 'win32' ? win32Implementation() : nixImplementation()), | ||
|
||
_win32: win32Implementation, | ||
_nix: nixImplementation, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const pipeCommands = require('./pipeCommands'); | ||
|
||
const _win32 = pipeCommands._win32(); | ||
const _nix = pipeCommands._nix(); | ||
|
||
describe('pipeUtils', () => { | ||
describe('escape in quoted string', () => { | ||
describe('on win32', () => { | ||
const escape = _win32.escape.inQuotedString; | ||
|
||
it('should escape double quotes with double quotes', () => | ||
expect(escape('Says "Hello"')).toMatchSnapshot()); | ||
}); | ||
|
||
describe('on *nix', () => { | ||
const escape = _nix.escape.inQuotedString; | ||
|
||
it('should escape double quotes with slash', () => | ||
expect(escape('Says "Hello"')).toMatchSnapshot()); | ||
}); | ||
}); | ||
|
||
describe('escape in quoted regexp', () => { | ||
describe('on win32', () => { | ||
const escape = _win32.escape.inQuotedRegexp; | ||
|
||
it('should escape only double quotes', () => | ||
expect(escape('^Hello\\s ".*"$')).toMatchSnapshot()); | ||
}); | ||
|
||
describe('on *nix', () => { | ||
const escape = _nix.escape.inQuotedRegexp; | ||
|
||
it('should not escape non-special characters', () => | ||
expect(escape('bundle_name')).toMatchSnapshot()); | ||
|
||
it('should escape double quotes with slash', () => | ||
expect(escape('"a')).toMatchSnapshot()); | ||
|
||
it('should escape [\\]', () => | ||
expect(escape('[kworker\\0:0]')).toMatchSnapshot()); | ||
|
||
it('should escape ^*$', () => | ||
expect(escape('^ma*tch$')).toMatchSnapshot()); | ||
|
||
it('should escape dots', () => | ||
expect(escape('com.company.bundle')).toMatchSnapshot()); | ||
}); | ||
}); | ||
|
||
describe('search by regexp pipe command', () => { | ||
const regexp = '^Hello\\s ".*"$'; | ||
|
||
describe('on win32', () => { | ||
const search = _win32.search.regexp; | ||
|
||
it('should use findstr /R /C:"regexp"', () => | ||
expect(search(regexp)).toMatchSnapshot()); | ||
}); | ||
|
||
describe('on *nix', () => { | ||
const search = _nix.search.regexp; | ||
|
||
it('should use grep "regexp"', () => | ||
expect(search(regexp)).toMatchSnapshot()); | ||
}); | ||
}); | ||
|
||
describe('search by fragment pipe command', () => { | ||
const fragment = '^Hello\\s ".*"$'; | ||
|
||
describe('on win32', () => { | ||
const search = _win32.search.fragment; | ||
|
||
it('should use findstr /C:"fragment"', () => | ||
expect(search(fragment)).toMatchSnapshot()); | ||
}); | ||
|
||
describe('on *nix', () => { | ||
const search = _nix.search.fragment; | ||
|
||
it('should use grep -e "fragment"', () => | ||
expect(search(fragment)).toMatchSnapshot()); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +0,0 @@ | ||
const SPECIAL_CHARS = /([\^\$\[\]\*\.\\])/g; | ||
|
||
function regexEscape(exactString) { | ||
return exactString.replace(SPECIAL_CHARS, "\\$1"); | ||
} | ||
|
||
module.exports = regexEscape; | ||
This file was deleted.
Oops, something went wrong.