summaryrefslogtreecommitdiff
path: root/spec/tooling/lib/tooling/parallel_rspec_runner_spec.rb
blob: 4b44d991d89b96ddc158aae77284e5feb166ff2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true

require_relative '../../../../tooling/lib/tooling/parallel_rspec_runner'

RSpec.describe Tooling::ParallelRSpecRunner do # rubocop:disable RSpec/FilePath
  describe '#run' do
    let(:allocator) { instance_double(Knapsack::Allocator) }
    let(:rspec_args) { '--seed 123' }
    let(:filter_tests_file) { 'tests.txt' }
    let(:node_tests) { %w[01_spec.rb 03_spec.rb 05_spec.rb] }
    let(:filter_tests) { '01_spec.rb 02_spec.rb 03_spec.rb' }
    let(:test_dir) { 'spec' }

    before do
      allow(Knapsack.logger).to receive(:info)
      allow(allocator).to receive(:node_tests).and_return(node_tests)
      allow(allocator).to receive(:test_dir).and_return(test_dir)
      allow(File).to receive(:exist?).with(filter_tests_file).and_return(true)
      allow(File).to receive(:read).and_call_original
      allow(File).to receive(:read).with(filter_tests_file).and_return(filter_tests)
      allow(subject).to receive(:exec)
    end

    subject { described_class.new(allocator: allocator, filter_tests_file: filter_tests_file, rspec_args: rspec_args) }

    shared_examples 'runs node tests' do
      it 'runs rspec with tests allocated for this node' do
        expect_command(%w[bundle exec rspec --seed 123 --default-path spec -- 01_spec.rb 03_spec.rb 05_spec.rb])

        subject.run
      end
    end

    context 'given filter tests' do
      it 'reads filter tests file for list of tests' do
        expect(File).to receive(:read).with(filter_tests_file)

        subject.run
      end

      it 'runs rspec filter tests that are allocated for this node' do
        expect_command(%w[bundle exec rspec --seed 123 --default-path spec -- 01_spec.rb 03_spec.rb])

        subject.run
      end

      context 'when there is no intersect between allocated tests and filtered tests' do
        let(:filter_tests) { '99_spec.rb' }

        it 'does not run rspec' do
          expect(subject).not_to receive(:exec)

          subject.run
        end
      end
    end

    context 'with empty filter tests file' do
      let(:filter_tests) { '' }

      it_behaves_like 'runs node tests'
    end

    context 'without filter_tests_file option' do
      let(:filter_tests_file) { nil }

      it_behaves_like 'runs node tests'
    end

    context 'if filter_tests_file does not exist' do
      before do
        allow(File).to receive(:exist?).with(filter_tests_file).and_return(false)
      end

      it_behaves_like 'runs node tests'
    end

    context 'without rspec args' do
      let(:rspec_args) { nil }

      it 'runs rspec with without extra arguments' do
        expect_command(%w[bundle exec rspec --default-path spec -- 01_spec.rb 03_spec.rb])

        subject.run
      end
    end

    def expect_command(cmd)
      expect(subject).to receive(:exec).with(*cmd)
    end
  end
end