Dir.glob pattern examples for KNAPSACK_PRO_TEST_FILE_PATTERN and KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN
Dir.glob accepts a single pattern or multiple test file patterns. When you provide multiple patterns, they must be wrapped by curly braces ({}
) and separated by a comma with no space between the patterns, e.g.: {pattern1,pattern2,pattern3}
. In the case of a single pattern, you should not wrap it in curly braces ({}
).
When can this be useful? For instance, you could use complex patterns to find all model specs and all controller specs and run only them by defining KNAPSACK_PRO_TEST_FILE_PATTERN
:
KNAPSACK_PRO_TEST_FILE_PATTERN="{spec/models/**{,/*/**}/*_spec.rb,spec/controllers/**{,/*/**}/*_spec.rb}"
You can verify if the pattern works in Rails console:
$ bin/rails console
3.0.1 :001 > Dir.glob("{spec/models/**{,/*/**}/*_spec.rb,spec/controllers/**{,/*/**}/*_spec.rb}")
=>
["spec/models/user_spec.rb",
"spec/models/book_spec.rb",
"spec/controllers/api/home_controller_spec.rb",
"spec/controllers/api/v1/users_controller_spec.rb",
"spec/controllers/books_controller_spec.rb"]
If you like, you can exclude specific tests with a KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN
environment variable. For instance, you can exclude all API controller specs (note that we use a single pattern here, so no curly braces ({}
) wrapping the pattern here):
KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN="spec/controllers/api/**{,/*/**}/*_spec.rb"
Please use Rails console to verify that your Dir.glob
pattern is valid for your test suite.