Home > Blockchain >  With RSpec, how do I write an expression that captures _spec files in nested directories?
With RSpec, how do I write an expression that captures _spec files in nested directories?

Time:04-22

I’m using Rails 6 with RSpec 3.10. I have the following directory structure

spec/models/my_object_spec.rb
spec/models/module_name/product_spec.rb

I run the tests with a command similar to the below

bundle exec rspec --color --require spec_helper --format RspecJunitFormatter --out $CIRCLE_TEST_REPORTS/rspec/rspec.xml --format progress $(circleci tests glob spec/**/*_spec.rb | circleci tests split --split-by=timings)

The issue is that the regular expression “spec/**/*_spec.rb” only seems to run the file “spec/models/my_object_spec.rb”, however, the other file in the child directory of “models” doesn’t get run. Is there a way to write the regular expression so that it would capture all files ending in “_spec” regardless of which child directory they are in?

CodePudding user response:

glob is not a regex, so you need to enable the shell-option globstar in order to recurse subdirectories.

Adding the line - run: shopt -s globstar before your test call probably won't work because each step in CircleCI runs it in its own shell instance. You need to set the shell-opt in the same step that you run your tests. Something like this:

- run:
  command: |
    shopt -s globstar
    bundle exec rspec --color --require spec_helper --format RspecJunitFormatter --out $CIRCLE_TEST_REPORTS/rspec/rspec.xml --format progress $(circleci tests glob spec/**/*_spec.rb | circleci tests split --split-by=timings)
  • Related