File: allocator_builder_spec.rb

package info (click to toggle)
ruby-knapsack 1.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,084 kB
  • sloc: ruby: 2,832; makefile: 4; sh: 3
file content (124 lines) | stat: -rw-r--r-- 4,023 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
describe Knapsack::AllocatorBuilder do
  let(:allocator_builder) { described_class.new(adapter_class) }
  let(:allocator) { double }

  let(:report) { double }
  let(:knapsack_report) { instance_double(Knapsack::Report) }

  let(:adapter_report_path) { adapter_class::REPORT_PATH }
  let(:adapter_test_file_pattern) { adapter_class::TEST_DIR_PATTERN }

  let(:env_ci_node_total) { double }
  let(:env_ci_node_index) { double }
  let(:env_report_path) { nil }
  let(:env_test_file_pattern) { nil }

  describe '#allocator' do
    subject { allocator_builder.allocator }

    before do
      expect(Knapsack::Config::Env).to receive(:report_path).and_return(env_report_path)
      expect(Knapsack::Config::Env).to receive(:test_file_pattern).and_return(env_test_file_pattern)
      expect(Knapsack::Config::Env).to receive(:ci_node_total).and_return(env_ci_node_total)
      expect(Knapsack::Config::Env).to receive(:ci_node_index).and_return(env_ci_node_index)

      expect(Knapsack).to receive(:report).twice.and_return(knapsack_report)
      expect(knapsack_report).to receive(:open).and_return(report)

      expect(knapsack_report).to receive(:config).with(report_config)
      expect(Knapsack::Allocator).to receive(:new).with(allocator_args).and_return(allocator)
    end

    shared_examples 'allocator builder' do
      context 'when ENVs are nil' do
        let(:report_config) { { report_path: adapter_report_path } }
        let(:allocator_args) do
          {
            report: report,
            test_file_pattern: adapter_test_file_pattern,
            ci_node_total: env_ci_node_total,
            ci_node_index: env_ci_node_index
          }
        end

        it { should eql allocator }
      end

      context 'when ENV report_path has value' do
        let(:env_report_path) { 'knapsack_custom_report.json' }
        let(:report_config) { { report_path: env_report_path } }
        let(:allocator_args) do
          {
            report: report,
            test_file_pattern: adapter_test_file_pattern,
            ci_node_total: env_ci_node_total,
            ci_node_index: env_ci_node_index
          }
        end

        it { should eql allocator }
      end

      context 'when ENV test_file_pattern has value' do
        let(:env_test_file_pattern) { 'custom_spec/**{,/*/**}/*_spec.rb' }
        let(:report_config) { { report_path: adapter_report_path } }
        let(:allocator_args) do
          {
            report: report,
            test_file_pattern: env_test_file_pattern,
            ci_node_total: env_ci_node_total,
            ci_node_index: env_ci_node_index
          }
        end

        it { should eql allocator }
      end
    end

    context 'when RSpecAdapter' do
      let(:adapter_class) { Knapsack::Adapters::RSpecAdapter }
      it_behaves_like 'allocator builder'
    end

    # To make sure we do not break backwards compatibility
    context 'when RspecAdapter' do
      let(:adapter_class) { Knapsack::Adapters::RspecAdapter }
      it_behaves_like 'allocator builder'
    end

    context 'when CucumberAdapter' do
      let(:adapter_class) { Knapsack::Adapters::CucumberAdapter }
      it_behaves_like 'allocator builder'
    end
  end

  describe '#test_dir' do
    let(:adapter_class) { Knapsack::Adapters::RSpecAdapter }

    subject { allocator_builder.test_dir }

    context 'when ENV test_dir has value' do
      before do
        expect(Knapsack::Config::Env).to receive(:test_dir).and_return("custom_spec")
      end

      it { should eq 'custom_spec' }
    end

    context 'when ENV test_dir has no value' do
      before do
        expect(Knapsack::Config::Env).to receive(:test_file_pattern).and_return(env_test_file_pattern)
      end

      context 'when ENV test_file_pattern has value' do
        let(:env_test_file_pattern) { 'custom_spec/**{,/*/**}/*_spec.rb' }

        it { should eq 'custom_spec' }
      end

      context 'when ENV test_file_pattern has no value' do
        it { should eq 'spec' }
      end
    end
  end
end