Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sed): better support for converting sed patterns #137

Merged
merged 2 commits into from Jun 26, 2018

Conversation

nfischer
Copy link
Member

This PR does:

  1. Refactors sed-specific pattern-conversion logic. This logic converts
    a unix-style pattern (e.g., "s/foo/bar/") into the separate
    parameters expected by shell.sed() (e.g., new RegExp('foo'), 'bar').
    This builds the RegExp more carefully to avoid the issue with
    escaped slashes (as reported in Regex support for forward slashes in sed #136).
  2. Explicitly checks for empty patterns and throws an error
    (previously, these would just silently fail conversion).
  3. Adds tests for the previous two cases.
  4. Refactors the sed tests to be more maintainable by saving file
    contents in test variables. No change in the logic of
    already-existing tests.

Fixes #136
Test: 'works with backslashes and forward slashes in pattern'
Test: 'does not work with empty regex strings'

This PR does:

 1. Refactors sed-specific pattern-conversion logic. This logic converts
    a unix-style pattern (e.g., "s/foo/bar/") into the separate
    parameters expected by shell.sed() (e.g., new RegExp('foo'), 'bar').
    This builds the RegExp more carefully to avoid the issue with
    escaped slashes (as reported in #136).
 2. Explicitly checks for empty patterns and throws an error
    (previously, these would just silently fail conversion).
 3. Adds tests for the previous two cases.
 4. Refactors the sed tests to be more maintainable by saving file
    contents in test variables. No change in the logic of
    already-existing tests.

Fixes #136
Test: 'works with backslashes and forward slashes in pattern'
Test: 'does not work with empty regex strings'
@codecov-io
Copy link

codecov-io commented Jun 17, 2018

Codecov Report

Merging #137 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #137   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           3      3           
  Lines          22     40   +18     
=====================================
+ Hits           22     40   +18
Impacted Files Coverage Δ
src/shx.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 63bdaa4...e7fb628. Read the comment docs.

src/shx.js Outdated
const regexString = match[1].replace(/\\\//g, '/');
const replacement = match[2].replace(/\\\//g, '/').replace(/\\./g, '.');
const regexFlags = match[3];
if (!regexString) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps regexString === '' would be clearer here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

it('does not work with empty regex strings', () => {
(() => {
cli('sed', 's//foo/g', testFileName1);
}).should.throw(Error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check for the exact error message here, i.e. .should.throw('Bad sed pattern (empty regex)').

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@nfischer
Copy link
Member Author

@freitagbr ptal

@freitagbr
Copy link
Contributor

LGTM

@nfischer nfischer merged commit 57b58af into master Jun 26, 2018
@nfischer nfischer deleted the sed-slash-regex branch June 26, 2018 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants