Posted March 28, 2018 in Code 2 minutes

Use The Silver Searcher with RSpec

Working within an app so large that its test suite clocks in at just under 3 hours for a full run, even small local changes can result in unexpected spec failures. To combat failed CI builds, I often find myself searching around /spec for anything I might have missed.

It was during one of these recent “search and destroy” sessions when a thought came to me, “Searching with ag gives me a list of files. RSpec can accept a list of spec files to run. Why on earth am I not combining these two tools?”

After a little messing about, I arrived at:

ag -l "some_search_term" ./spec --ignore "spec/fixtures" --ignore "spec/whatever_to_exclude" --ignore "spec/support" | xargs rspec

SHAZZAM!

The above command generates a list of files containing the search term and pipes the list to RSpec. Furthermore, it allows search by regex to provide even greater discoverability (Want to look for the SomeModel constant as well as a :some_model factory reference? No problem!).

It also allows for flexible exclusion of directories, files, extensions, etc.

The command has been a huge time saver for me, and while I’m sure I am not the first to pair these tools, I have not happened upon any articles outlining their combined use.

So, whether you’re after improved discovery within a large test suite containing hundreds of spec files or just want an easy way to run all specs mentioning a specific term, hopefully this knowledge will come in handy!

Cheers!