Add ability to set test_dir using an environment variable#35
Add ability to set test_dir using an environment variable#35ArturT merged 4 commits intoKnapsackPro:masterfrom
Conversation
Prior to this commit, `test_dir` was extracted from the test file pattern using a regex. For example, using the default RSpec test file pattern of `spec/**{,/*/**}/*_spec.rb`, `test_dir` would be extracted in one of two ways:
```ruby
"spec/**{,/*/**}/*_spec.rb".split('/').first # => "spec"
"spec/**{,/*/**}/*_spec.rb".gsub(/^(.*?)\//).first # => "spec/"
```
The end result is effectively the same and works great for this case. However, when using a more complex test file pattern this method of extraction fails to provide the desired result. For example, say we had multiple spec directories under different trees:
```ruby
"{spec,engines/**/spec}/**{,/*/**}/*_spec.rb".split('/').first # => "{spec,engines"
"{spec,engines/**/spec}/**{,/*/**}/*_spec.rb".gsub(/^(.*?)\//).first # => "{spec,engines/"
```
In this case, the extracted `test_dir` is not usuable.
This commit modifies the code to use the value specified in `ENV["KNAPSACK_DEFAULT_TEST_DIR"]`, otherwise to extract the `test_dir` in the normal way.
_Note: We could attempt to make extraction more intelligent (and thereby more complex) but allowing one to specify it directly may be the simplest working solution for everyone._
|
Could you add example in readme in FAQ section. https://github.com/ArturT/knapsack#faq When you provide KNAPSACK_DEFAULT_TEST_DIR then you will run tests only for this directory or you will have to specify twice How are you using this in your case? Good example in read me would be great. |
|
Certainly! We're trying to setup Knapsack for our app on BuildKite. We use RSpec and in addition to the normal $ bundle exec rspec spec engines/**/specThis takes advantage of the default spec directory for RSpec— Initially, I attempted to pass these same arguments to the Knapsack rake task, but the resulting command gave unexpected results (with good reason): $ bundle exec rake "knapsack:rspec[spec engines/**/spec]"
# `Knapsack::Runner::RSpecRunner` executes:
$ bundle exec rspec spec engines/**/spec --default-path spec -- spec/models/account_spec.rb spec/models/user_spec.rb engines/my_engine/spec/models/invoice_spec.rb engines/my_other_engine/spec/models/document_spec.rbThis stops Knapsack from being able to control what specs are run in each node because we've told RSpec to run the directories directly. Specifying a more complex test file pattern and omitting the arguments half-fixes the issue, giving control of which specs are run back to Knapsack, but results in an unusable test directory: $ bundle exec rake "knapsack:rspec[spec engines/**/spec]"
# `Knapsack::Runner::RSpecRunner` executes:
$ bundle exec rspec --tag js --default-path {spec,engines/ -- spec/models/account_spec.rb spec/models/user_spec.rb engines/my_engine/spec/models/invoice_spec.rb engines/my_other_engine/spec/models/document_spec.rbThis is what led me to believe the ability to specify a test directory would be useful. Let me know if you have any other questions about this. I'll add an example to the FAQ. |
|
Thinking about the environment variable further, I wonder if a better name is simply |
|
@jnraine Thanks for explanation. Could you add example case with using |
|
@ArturT done! |
|
@jnraine Thanks for great work! I've just released knapsack v1.7.0. BTW If your test suite is big then you may like to try http://knapsackpro.com and get free API key. |
|
Nice. Thanks for the fast merge and release @ArturT! |
Prior to this commit,
test_dirwas extracted from the test file pattern using a regex. For example, using the default RSpec test file pattern ofspec/**{,/*/**}/*_spec.rb,test_dirwould be extracted in one of two ways:The end result is effectively the same and works great for this case. However, when using a more complex test file pattern this method of extraction fails to provide the desired result. For example, say we had multiple spec directories under different trees:
In this case, the extracted
test_diris not usuable.This commit modifies the code to use the value specified in
ENV["KNAPSACK_DEFAULT_TEST_DIR"], otherwise to extract thetest_dirin the normal way.Note: We could attempt to make extraction more intelligent (and thereby more complex) but allowing one to specify it directly may be the simplest working solution for everyone.