Skip to content

Commit

Permalink
Normalize invalid regex patterns for --focus and --skip.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Dec 5, 2016
1 parent bbdcb16 commit ff8bd78
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
40 changes: 40 additions & 0 deletions specs/configuration.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,46 @@
});
});

describe('->setFocusPattern()', function() {
it('should normalize patterns that are invalid regular expressions', function() {
$this->configuration->setFocusPattern('certain ~( type of)? test');
$pattern = $this->configuration->getFocusPattern();

assert(preg_match($pattern, 'certain ~ test'), 'normalized pattern should still honor PCRE syntax');
assert(preg_match($pattern, 'certain ~ type of test'), 'normalized pattern should still honor PCRE syntax');
assert(preg_match($pattern, 'a certain ~ test with extras'), 'normalized pattern should match text with prefixes and suffixes');
assert(!preg_match($pattern, 'an uncertain ~ test'), 'normalized pattern should not match text without word boundaries');
});

it('should fall back to plaintext matching for regular expressions that cannot be salvaged', function() {
$this->configuration->setFocusPattern('lol(wat');
$pattern = $this->configuration->getFocusPattern();

assert(preg_match($pattern, 'lmao lol(wat huh'), 'normalized pattern should match text with prefixes and suffixes');
assert(!preg_match($pattern, 'lolol(wat'), 'normalized pattern should not match text without word boundaries');
});
});

describe('->setSkipPattern()', function() {
it('should normalize patterns that are invalid regular expressions', function() {
$this->configuration->setSkipPattern('certain ~( type of)? test');
$pattern = $this->configuration->getSkipPattern();

assert(preg_match($pattern, 'certain ~ test'), 'normalized pattern should still honor PCRE syntax');
assert(preg_match($pattern, 'certain ~ type of test'), 'normalized pattern should still honor PCRE syntax');
assert(preg_match($pattern, 'a certain ~ test with extras'), 'normalized pattern should match text with prefixes and suffixes');
assert(!preg_match($pattern, 'an uncertain ~ test'), 'normalized pattern should not match text without word boundaries');
});

it('should fall back to plaintext matching for regular expressions that cannot be salvaged', function() {
$this->configuration->setSkipPattern('lol(wat');
$pattern = $this->configuration->getSkipPattern();

assert(preg_match($pattern, 'lmao lol(wat huh'), 'normalized pattern should match text with prefixes and suffixes');
assert(!preg_match($pattern, 'lolol(wat'), 'normalized pattern should not match text without word boundaries');
});
});

describe('->enableColorsExplicit()', function() {
it('should enable colors when explicit is set', function() {
$this->configuration->enableColorsExplicit();
Expand Down
25 changes: 23 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getGrep()
*/
public function setFocusPattern($pattern)
{
return $this->write('focusPattern', $pattern);
return $this->write('focusPattern', $this->normalizeRegexPattern($pattern));
}

/**
Expand All @@ -116,7 +116,7 @@ public function getFocusPattern()
*/
public function setSkipPattern($pattern)
{
return $this->write('skipPattern', $pattern);
return $this->write('skipPattern', $this->normalizeRegexPattern($pattern));
}

/**
Expand Down Expand Up @@ -307,4 +307,25 @@ protected function write($varName, $value)
putenv($env . '=' . $value);
return $this;
}

/**
* Normalize the supplied regular expression pattern.
*
* @param string $pattern
* @return string
*/
protected function normalizeRegexPattern($pattern)
{
if (false !== @preg_match($pattern, null)) {
return $pattern;
}

$boundedPattern = '~\b' . str_replace('~', '\~', $pattern) . '\b~';

if (false !== @preg_match($boundedPattern, null)) {
return $boundedPattern;
}

return '~\b' . preg_quote($pattern, '~') . '\b~';
}
}

0 comments on commit ff8bd78

Please sign in to comment.