Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It Possible To Pass A regex to the 'rspec` Command line command?

When calling rspec from the command line, I know that you can use the -e or -example flag to pass in a regex which it matched against the content of any it blocks, but is there a way to pass in a regex and have RSpec run files whose names match that regex?

like image 567
Undistraction Avatar asked Jul 09 '14 12:07

Undistraction


2 Answers

You can use for examples :-

It will load for you all files, whose names start with test

rspec -P spec/**/test*_spec.rb
rspec -P spec/**/test*.rb

It will load for you all files, which has a word test in middle, but not start with test

rspec -P spec/**/*?test*_spec.rb

It will run all files whose name start with test. Like test_1_spec.rb, test_2_spec.rb etc. But you have to run this command from your project root directory.

Docs for -P / --pattern flag

Information can also be obtained from --help

arup$ rspec -help | grep PATTERN
 -P, --pattern PATTERN  Load files matching pattern (default: "spec/**/*_spec.rb").
like image 133
Arup Rakshit Avatar answered Oct 26 '22 05:10

Arup Rakshit


You can use bash expansion. It isn't as powerful as a full regex but works most of the time. So you can do things like

rspec spec/models/user_*.spec

Docs here: https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html#Pattern-Matching

like image 36
bundacia Avatar answered Oct 26 '22 05:10

bundacia