Welcome to mirror list, hosted at ThFree Co, Russian Federation.

listener_spec.rb « rspec_flaky « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e193bf408b5efb83c0b21fb4bdaa605d7b04443 (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
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
require 'spec_helper'

describe RspecFlaky::Listener do
  let(:flaky_example_report) do
    {
      'abc123' => {
        example_id: 'spec/foo/bar_spec.rb:2',
        file: 'spec/foo/bar_spec.rb',
        line: 2,
        description: 'hello world',
        first_flaky_at: 1234,
        last_flaky_at: instance_of(Time),
        last_attempts_count: 2,
        flaky_reports: 1,
        last_flaky_job: nil
      }
    }
  end
  let(:example_attrs) do
    {
      id: 'spec/foo/baz_spec.rb:3',
      metadata: {
        file_path: 'spec/foo/baz_spec.rb',
        line_number: 3,
        full_description: 'hello GitLab'
      },
      execution_result: double(status: 'passed', exception: nil)
    }
  end

  before do
    # Stub these env variables otherwise specs don't behave the same on the CI
    stub_env('CI_PROJECT_URL', nil)
    stub_env('CI_JOB_ID', nil)
  end

  describe '#initialize' do
    shared_examples 'a valid Listener instance' do
      let(:expected_all_flaky_examples) { {} }

      it 'returns a valid Listener instance' do
        listener = described_class.new

        expect(listener.to_report(listener.all_flaky_examples))
          .to match(hash_including(expected_all_flaky_examples))
        expect(listener.new_flaky_examples).to eq({})
      end
    end

    context 'when no report file exists' do
      it_behaves_like 'a valid Listener instance'
    end

    context 'when a report file exists and set by ALL_FLAKY_RSPEC_REPORT_PATH' do
      let(:report_file) do
        Tempfile.new(%w[rspec_flaky_report .json]).tap do |f|
          f.write(JSON.pretty_generate(flaky_example_report))
          f.rewind
        end
      end

      before do
        stub_env('ALL_FLAKY_RSPEC_REPORT_PATH', report_file.path)
      end

      after do
        report_file.close
        report_file.unlink
      end

      it_behaves_like 'a valid Listener instance' do
        let(:expected_all_flaky_examples) { flaky_example_report }
      end
    end
  end

  describe '#example_passed' do
    let(:rspec_example) { double(example_attrs) }
    let(:notification) { double(example: rspec_example) }

    shared_examples 'a non-flaky example' do
      it 'does not change the flaky examples hash' do
        expect { subject.example_passed(notification) }
          .not_to change { subject.all_flaky_examples }
      end
    end

    describe 'when the RSpec example does not respond to attempts' do
      it_behaves_like 'a non-flaky example'
    end

    describe 'when the RSpec example has 1 attempt' do
      let(:rspec_example) { double(example_attrs.merge(attempts: 1)) }

      it_behaves_like 'a non-flaky example'
    end

    describe 'when the RSpec example has 2 attempts' do
      let(:rspec_example) { double(example_attrs.merge(attempts: 2)) }
      let(:expected_new_flaky_example) do
        {
          example_id: 'spec/foo/baz_spec.rb:3',
          file: 'spec/foo/baz_spec.rb',
          line: 3,
          description: 'hello GitLab',
          first_flaky_at: instance_of(Time),
          last_flaky_at: instance_of(Time),
          last_attempts_count: 2,
          flaky_reports: 1,
          last_flaky_job: nil
        }
      end

      it 'does not change the flaky examples hash' do
        expect { subject.example_passed(notification) }
          .to change { subject.all_flaky_examples }

        new_example = RspecFlaky::Example.new(rspec_example)

        expect(subject.all_flaky_examples[new_example.uid].to_h)
          .to match(hash_including(expected_new_flaky_example))
      end
    end
  end

  describe '#dump_summary' do
    let(:rspec_example) { double(example_attrs) }
    let(:notification) { double(example: rspec_example) }

    context 'when a report file path is set by ALL_FLAKY_RSPEC_REPORT_PATH' do
      let(:report_file_path) { Rails.root.join('tmp', 'rspec_flaky_report.json') }

      before do
        stub_env('ALL_FLAKY_RSPEC_REPORT_PATH', report_file_path)
        FileUtils.rm(report_file_path) if File.exist?(report_file_path)
      end

      after do
        FileUtils.rm(report_file_path) if File.exist?(report_file_path)
      end

      context 'when FLAKY_RSPEC_GENERATE_REPORT == "false"' do
        before do
          stub_env('FLAKY_RSPEC_GENERATE_REPORT', 'false')
        end

        it 'does not write the report file' do
          subject.example_passed(notification)

          subject.dump_summary(nil)

          expect(File.exist?(report_file_path)).to be(false)
        end
      end

      context 'when FLAKY_RSPEC_GENERATE_REPORT == "true"' do
        before do
          stub_env('FLAKY_RSPEC_GENERATE_REPORT', 'true')
        end

        it 'writes the report file' do
          subject.example_passed(notification)

          subject.dump_summary(nil)

          expect(File.exist?(report_file_path)).to be(true)
        end
      end
    end
  end

  describe '#to_report' do
    it 'transforms the internal hash to a JSON-ready hash' do
      expect(subject.to_report('abc123' => RspecFlaky::FlakyExample.new(flaky_example_report['abc123'])))
        .to match(hash_including(flaky_example_report))
    end
  end
end