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

[WIP] Implement --focus and --skip CLI options #197

Merged
merged 3 commits into from
Dec 10, 2016

Conversation

ezzatron
Copy link
Contributor

@ezzatron ezzatron commented Dec 1, 2016

@brianium Here's a working prototype of the --focus and --skip CLI options.

In its current form, it works like Ginkgo, in that if you specify either option on the command line, it completely overrides any tests that are focused "programatically" with an f prefix. Seems like the approach that will cause the least confusion.

Definitely not perfect, and no tests yet, but hopefully it's a starting point for discussion :)

@ezzatron ezzatron force-pushed the feature/focused-specs branch from 6969f45 to 795a77e Compare December 1, 2016 02:12
@codecov-io
Copy link

codecov-io commented Dec 1, 2016

Current coverage is 96.97% (diff: 100%)

Merging #197 into feature/focused-specs will increase coverage by 0.16%

@@           feature/focused-specs       #197   diff @@
=======================================================
  Files                         20         20          
  Lines                        658        695    +37   
  Methods                      165        173     +8   
  Messages                       0          0          
  Branches                       0          0          
=======================================================
+ Hits                         637        674    +37   
  Misses                        21         21          
  Partials                       0          0          

Powered by Codecov. Last update 2973fa2...ff8bd78

Copy link
Member

@brianium brianium left a comment

Choose a reason for hiding this comment

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

@ezzatron Looks like a pretty simple implementation that offers a lot of functionality. I just had one question to confirm my understanding, but I think this looks good so far

* @param string|null $skipPattern
*/
public function applyFocusPatterns($focusPattern, $skipPattern = null)
{
Copy link
Member

Choose a reason for hiding this comment

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

Just to double check, we are saying $skipPattern and $focusPattern are not mutually exclusive right?

You could run a test that has a combination of a focus pattern and a skip pattern? Like "run integration tests, but skip the selenium ones?"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's the intent, at least. Ginkgo has a similar feature:

When using the command line flags you can specify one or both of --focus and --skip. If both are specified the constraints will be ANDed together.

It's not really clear what they mean by "ANDed together", but I assume they meant "matched by --focus AND NOT matched by --skip". It seems like the most useful and intuitive option.

@ezzatron ezzatron force-pushed the feature/focused-specs branch from 795a77e to 2973fa2 Compare December 4, 2016 23:35
@ezzatron
Copy link
Contributor Author

ezzatron commented Dec 5, 2016

@brianium I've pushed tests for this PR, so I think it's probably ready to pull, unless you have more questions or feedback.

Copy link
Member

@brianium brianium left a comment

Choose a reason for hiding this comment

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

Tests look good, just added one more question about validating patterns.

{
$title = $this->getTitle();

if ($skipPattern !== null && preg_match($skipPattern, $title)) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we validate patterns somehow? If a non delimited regex is given, PHP will throw warnings:

$ php -a
php > preg_match('lol', 'rofl');
PHP Warning:  preg_match(): Delimiter must not be alphanumeric or backslash in php shell code on line 1

Warning: preg_match(): Delimiter must not be alphanumeric or backslash in php shell code on line 1

Copy link
Member

Choose a reason for hiding this comment

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

We could argue that an invalid regex is the tester's problem, but a more helpful error message might be nice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, we could be more helpful / handle the warnings better. Based off of this StackOverflow answer, we can validate the patterns by running them with @ suppression against a null value.

I had given some thought to supporting different behaviour when you don't provide a full regex. For example, if someone types --focus foo.*, we could interpret that in a number of useful ways, rather than just rejecting it.

My personal preference would probably be to surround the pattern in /\b and \b/, turning the above example into /\bfoo.*\b. The \b matches word boundaries, which means that foo.* would match foo and foobar, but not barfoo. It would also still work with things like ^foo$, because \b also matches the start and end of the line. See here for an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've pushed a proof of concept for the above. It "normalizes" the focus patterns in three steps:

  • If the pattern is valid, it is used as-is.
  • Otherwise, a new pattern is created by surrounding the original in ~\b and \b~, with instances of ~ replaced with \~.
  • If this new pattern is also invalid, then a 'fall-back" pattern is created using preg_quote().

So basically, if the pattern is valid regex, but missing delimiters, then it will "just work" like a normal regex. If the pattern has really messed up stuff like unbalanced parentheses, then it will use plaintext matching instead.

Also, I figured ~ is a better delimiter here because / is more likely to be used in test descriptions.

The use of \b is probably debatable here, but I know that when I use PHPUnit's --filter option, I am constantly using them to avoid matching unwanted substrings.

Copy link
Member

Choose a reason for hiding this comment

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

I think this looks good man. \b seems like a sane default if an explicit pattern is not given. Anything else in the pipeline for this? If not, I'll pull it down and give the CLI a go. If that’s all good I am good to merge this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@brianium I don't think there's any more code to write, it's only really the docs. I've already document fit(), fcontext(), and fdescribe(), but not these new CLI options. Shouldn't take too long though.

I opened up another PR on the docs with the current progress: peridot-php/peridot-php.github.io#3

@brianium brianium merged commit 6eb5318 into feature/focused-specs Dec 10, 2016
@brianium brianium deleted the focused-specs-cli branch December 10, 2016 13:56
@ezzatron
Copy link
Contributor Author

@brianium Shall I squash this down, and then we can organize a time to make a new release together?

@brianium
Copy link
Member

@ezzatron Sounds good man!

@ezzatron ezzatron modified the milestone: Next release Jan 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants