10

I have a test suite for a very large project (~ 3800 individual examples) that I'm updating from RSpec 2.14 to 3.6.

I have just run a replace-all for s/be_true/be true/, but some of them should be_truthy instead, and these specs are failing.

I can extract the changed lines from git diff - with a little work - but then I need to feed these into RSpec's config.filter_run, and it doesn't seem to like me doing that for the locations filter (the one activated when you specify a rspec path/to/file:line on the command line); it says "All examples were filtered out; ignoring {:locations=>...}".

I can also example.run unless example.location.in? changed_specs in an around filter, but this skips all the others and marks them as pending, rather than ignoring them completely; and it doesn't work in concert with the other filters to run_all_when_everything_filtered. (Probably because example.location refers to the top line of the test, but the changes are a few lines below that.)

And I can't seem to use it to add a tag to the example.metadata and then filter it out, either.

Edit: I also don't want to specify them on the command line, because my tests run under rerun, which keeps running the same command.

Any more ideas?

PJSCopeland
  • 2,818
  • 1
  • 26
  • 40

3 Answers3

14

You can run changed specs with:

rspec $(git ls-files --modified spec)

if you want include new unversioned specs as well use:

rspec $(git ls-files --modified --others spec)

You can make this handier with a shell alias:

alias rch='rspec $(git ls-files --modified --others spec)'
weston
  • 1,972
  • 17
  • 17
5

git status might be a better bet? Why doesn't something like this work for you:

$ bundle exec rspec $(git status | grep spec | grep "modified:" | cut -b 15-)

source: https://coderwall.com/p/usrhyq/running-modified-specs-with-git

PressingOnAlways
  • 11,948
  • 6
  • 32
  • 59
  • 1
    Hmm. Not bad, but (1) I'd rather specify line numbers as well, and (2) I want my `spec_helper` to pick up the relevant specs, since - I originally neglected to mention - I have `rspec` running under `rerun`. – PJSCopeland Aug 04 '17 at 00:30
  • `cut -b 15-` didn't quite work for me (it cut the string in the wrong place), but `awk '{ print $2 }'` did. I think using `awk` is likely to be more reliable, since it splits the string based on whitespace rather than position. – Nick F Jul 17 '18 at 11:48
0

I couldn't find a straight forward way of doing it, but you can do it combining the ruby gem 'guard' and the 'watch' command like mentioned in the below link.

Automatically run RSPec when plain-old Ruby (not Rails) files change

It worked for me but you may need to find out further tweaks to enhance what you want.

Additional suggestion, https://github.com/guard/guard-rspec is having good support if you want to try out the above with 'guard-rspec' gem.

SajithP
  • 592
  • 8
  • 19