Skip to content
Merged
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

### 2.7.0

* Add support for env var `KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE` to allow accepting file containing test files to run.

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/129

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v2.6.0...v2.7.0

### 2.6.0

* Improve logger to show failed requests URL and when retry will happen
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3082,15 +3082,34 @@ The test file pattern and exclude pattern support any glob pattern handled by [`

#### How to run a specific list of test files or only some tests from test file?

:information_source: If you don't want to use the pattern [`KNAPSACK_PRO_TEST_FILE_PATTERN`](#how-can-i-run-tests-from-multiple-directories) to define a list of tests to run then read below.
:information_source: If you don't want to use the pattern [`KNAPSACK_PRO_TEST_FILE_PATTERN`](#how-can-i-run-tests-from-multiple-directories) to define a list of tests to run then read below two options.

**Option 1:**

If you want to run a specific list of test files that are explicitly defined by you or auto-generated by some kind of script you created then please use:

`KNAPSACK_PRO_TEST_FILE_LIST=spec/features/dashboard_spec.rb,spec/models/user.rb:10,spec/models/user.rb:29`

Note `KNAPSACK_PRO_TEST_FILE_LIST` must be a list of test files comma separated. You can provide line number for tests inside of spec file in case of RSpec (this way you can run only one test or a group of tests from RSpec spec file). You can provide the same file a few times with different test line number.

Note when you set `KNAPSACK_PRO_TEST_FILE_LIST` then below environment variables are ignored:
**Option 2:**

Similarly, you can also provide a source file containing the test files that you would like to run. For example:
`KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE=spec/fixtures/test_file_list_source_file.txt`
And the content of the source file can be any of the format below:

```
./spec/test1_spec.rb
spec/test2_spec.rb[1]
./spec/test3_spec.rb[1:2:3:4]
./spec/test4_spec.rb:4
./spec/test4_spec.rb:5
```

> Note that each of the line must be ending with `\n` the new line.

Note when you set `KNAPSACK_PRO_TEST_FILE_LIST` or `KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE` then below environment variables are ignored:

* `KNAPSACK_PRO_TEST_FILE_PATTERN`
* `KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN`

Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def test_file_list
ENV['KNAPSACK_PRO_TEST_FILE_LIST']
end

def test_file_list_source_file
ENV['KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE']
end

def test_dir
ENV['KNAPSACK_PRO_TEST_DIR']
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/test_file_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def test_files
return KnapsackPro::Config::Env.test_file_list.split(',').map(&:strip)
end

if test_file_list_enabled && KnapsackPro::Config::Env.test_file_list_source_file
return File.read(KnapsackPro::Config::Env.test_file_list_source_file).split(/\n/)
end

test_file_paths = Dir.glob(test_file_pattern).uniq

excluded_test_file_paths =
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/test_file_list_source_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
./spec/test1_spec.rb
spec/test2_spec.rb[1]
./spec/test3_spec.rb[1:2:3:4]
./spec/test4_spec.rb:4
./spec/test4_spec.rb:5
14 changes: 14 additions & 0 deletions spec/knapsack_pro/config/env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@
end
end

describe '.test_file_list_source_file' do
subject { described_class.test_file_list_source_file }

context 'when ENV exists' do
let(:test_file_list_source_file) { 'spec/fixtures/test_file_list_source_file.txt' }
before { stub_const("ENV", { 'KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE' => test_file_list_source_file }) }
it { should eq test_file_list_source_file }
end

context "when ENV doesn't exist" do
it { should be_nil }
end
end

describe '.test_dir' do
subject { described_class.test_dir }

Expand Down
18 changes: 18 additions & 0 deletions spec/knapsack_pro/test_file_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,23 @@
end
end
end

context 'when KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE is defined' do
let(:test_file_list_source_file) { 'spec/fixtures/test_file_list_source_file.txt' }

before do
stub_const("ENV", { 'KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE' => test_file_list_source_file })
end

it do
expect(subject).to eq([
{ 'path' => 'spec/test1_spec.rb' },
{ 'path' => 'spec/test2_spec.rb[1]' },
{ 'path' => 'spec/test3_spec.rb[1:2:3:4]' },
{ 'path' => 'spec/test4_spec.rb:4' },
{ 'path' => 'spec/test4_spec.rb:5' },
])
end
end
end
end