File: only_failures_support_spec.rb

package info (click to toggle)
ruby-rspec 3.5.0c3e0m0s0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,312 kB
  • ctags: 4,788
  • sloc: ruby: 62,572; sh: 785; makefile: 100
file content (199 lines) | stat: -rw-r--r-- 7,232 bytes parent folder | download | duplicates (2)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
module RSpec::Core
  RSpec.describe Configuration, "--only-failures support" do
    let(:config) { Configuration.new }

    def simulate_persisted_examples(*examples)
      config.example_status_persistence_file_path = "examples.txt"
      persister = class_double(ExampleStatusPersister).as_stubbed_const

      allow(persister).to receive(:load_from).with("examples.txt").and_return(examples.flatten)
    end

    describe "#last_run_statuses" do
      def last_run_statuses
        config.last_run_statuses
      end

      context "when `example_status_persistence_file_path` is configured" do
        before do
          simulate_persisted_examples(
            { :example_id => "id_1", :status => "passed" },
            { :example_id => "id_2", :status => "failed" }
          )
        end

        it 'gets the last run statuses from the ExampleStatusPersister' do
          expect(last_run_statuses).to eq(
            'id_1' => 'passed', 'id_2' => 'failed'
          )
        end

        it 'returns a memoized value' do
          expect(last_run_statuses).to be(last_run_statuses)
        end

        specify 'the hash returns `unknown` for unknown example ids for consistency' do
          expect(last_run_statuses["foo"]).to eq(Configuration::UNKNOWN_STATUS)
          expect(last_run_statuses["bar"]).to eq(Configuration::UNKNOWN_STATUS)
        end
      end

      context "when `example_status_persistence_file_path` is not configured" do
        before do
          config.example_status_persistence_file_path = nil
        end

        it 'returns a memoized value' do
          expect(last_run_statuses).to be(last_run_statuses)
        end

        it 'returns a blank hash without attempting to load the persisted statuses' do
          persister = class_double(ExampleStatusPersister).as_stubbed_const
          expect(persister).not_to receive(:load_from)

          expect(last_run_statuses).to eq({})
        end

        specify 'the hash returns `unknown` for all ids for consistency' do
          expect(last_run_statuses["foo"]).to eq(Configuration::UNKNOWN_STATUS)
          expect(last_run_statuses["bar"]).to eq(Configuration::UNKNOWN_STATUS)
        end
      end

      def allows_value_to_change_when_updated
        simulate_persisted_examples(
          { :example_id => "id_1", :status => "passed" },
          { :example_id => "id_2", :status => "failed" }
        )

        config.example_status_persistence_file_path = nil

        expect {
          yield
        }.to change { last_run_statuses }.to('id_1' => 'passed', 'id_2' => 'failed')
      end

      it 'allows the value to be updated when `example_status_persistence_file_path` is set after first access' do
        allows_value_to_change_when_updated do
          config.example_status_persistence_file_path = "examples.txt"
        end
      end

      it 'allows the value to be updated when `example_status_persistence_file_path` is forced after first access' do
        allows_value_to_change_when_updated do
          config.force(:example_status_persistence_file_path => "examples.txt")
        end
      end
    end

    describe "#spec_files_with_failures" do
      def spec_files_with_failures
        config.spec_files_with_failures
      end

      context "when `example_status_persistence_file_path` is configured" do
        it 'returns a memoized array of unique spec files that contain failed exaples' do
          simulate_persisted_examples(
            { :example_id => "./spec_1.rb[1:1]", :status => "failed"  },
            { :example_id => "./spec_1.rb[1:2]", :status => "failed"  },
            { :example_id => "./spec_2.rb[1:2]", :status => "passed"  },
            { :example_id => "./spec_3.rb[1:2]", :status => "pending" },
            { :example_id => "./spec_4.rb[1:2]", :status => "unknown" },
            { :example_id => "./spec_5.rb[1:2]", :status => "failed"  }
          )

          expect(spec_files_with_failures).to(
            be_an(Array) &
            be(spec_files_with_failures) &
            contain_exactly("./spec_1.rb", "./spec_5.rb")
          )
        end
      end

      context "when `example_status_persistence_file_path` is not configured" do
        it "returns a memoized blank array" do
          config.example_status_persistence_file_path = nil

          expect(spec_files_with_failures).to(
            eq([]) & be(spec_files_with_failures)
          )
        end
      end

      def allows_value_to_change_when_updated
        simulate_persisted_examples({ :example_id => "./spec_1.rb[1:1]", :status => "failed" })

        config.example_status_persistence_file_path = nil

        expect {
          yield
        }.to change { spec_files_with_failures }.to(["./spec_1.rb"])
      end

      it 'allows the value to be updated when `example_status_persistence_file_path` is set after first access' do
        allows_value_to_change_when_updated do
          config.example_status_persistence_file_path = "examples.txt"
        end
      end

      it 'allows the value to be updated when `example_status_persistence_file_path` is forced after first access' do
        allows_value_to_change_when_updated do
          config.force(:example_status_persistence_file_path => "examples.txt")
        end
      end
    end

    describe "#files_to_run, when `only_failures` is set" do
      around do |ex|
        handle_current_dir_change do
          Dir.chdir("spec/rspec/core", &ex)
        end
      end

      let(:default_path) { "resources" }
      let(:files_with_failures) { ["./resources/a_spec.rb"] }
      let(:files_loaded_via_default_path) do
        configuration = Configuration.new
        configuration.default_path = default_path
        configuration.files_or_directories_to_run = []
        configuration.files_to_run
      end

      before do
        expect(files_loaded_via_default_path).not_to eq(files_with_failures)
        config.default_path = default_path

        simulate_persisted_examples(files_with_failures.map do |file|
          { :example_id => "#{file}[1:1]", :status => "failed" }
        end)

        config.force(:only_failures => true)
      end

      context "and no explicit paths have been set" do
        xit 'loads only the files that have failures' do
          config.files_or_directories_to_run = []
          expect(config.files_to_run).to eq(files_with_failures)
        end

        it 'loads the default path if there are no files with failures' do
          simulate_persisted_examples([])
          config.files_or_directories_to_run = []
          expect(config.files_to_run).to eq(files_loaded_via_default_path)
        end
      end

      context "and a path has been set" do
        it "loads the intersection of files matching the path and files with failures" do
          config.files_or_directories_to_run = ["resources"]
          expect(config.files_to_run).to eq(files_with_failures)
        end

        it "loads all files matching the path when there are no intersecting files" do
          config.files_or_directories_to_run = ["resources/acceptance"]
          expect(config.files_to_run).to contain_files("resources/acceptance/foo_spec.rb")
        end
      end
    end
  end
end