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

failed_tests_spec.rb « scripts « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c379d3448a6cd8cb47c2cacc4ebd89c0a19a2ccb (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
# frozen_string_literal: true

require 'fast_spec_helper'
require_relative '../../scripts/failed_tests'

RSpec.describe FailedTests do
  let(:report_file) { 'spec/fixtures/scripts/test_report.json' }
  let(:options) { described_class::DEFAULT_OPTIONS.merge(previous_tests_report_path: report_file) }
  let(:failure_path) { 'path/to/fail_file_spec.rb' }
  let(:other_failure_path) { 'path/to/fail_file_spec_2.rb' }
  let(:file_contents_as_json) do
    {
      'suites' => [
        {
          'failed_count' => 1,
          'name' => 'rspec unit pg14 10/12',
          'test_cases' => [
            {
              'status' => 'failed',
              'file' => failure_path
            }
          ]
        },
        {
          'failed_count' => 1,
          'name' => 'rspec-ee unit pg14',
          'test_cases' => [
            {
              'status' => 'failed',
              'file' => failure_path
            }
          ]
        },
        {
          'failed_count' => 1,
          'name' => 'rspec unit pg15 10/12',
          'test_cases' => [
            {
              'status' => 'failed',
              'file' => other_failure_path
            }
          ]
        }
      ]
    }
  end

  subject { described_class.new(options) }

  describe '#output_failed_tests' do
    context 'with a valid report file' do
      before do
        allow(subject).to receive(:file_contents_as_json).and_return(file_contents_as_json)
      end

      it 'writes the file for the suite' do
        expect(File).to receive(:open)
          .with(File.join(described_class::DEFAULT_OPTIONS[:output_directory], "rspec_failed_tests.txt"), 'w').once
        expect(File).to receive(:open)
        .with(File.join(described_class::DEFAULT_OPTIONS[:output_directory], "rspec_ee_failed_tests.txt"), 'w').once

        subject.output_failed_tests
      end

      context 'when given a valid format' do
        subject { described_class.new(options.merge(format: :json)) }

        it 'writes the file for the suite' do
          expect(File).to receive(:open)
            .with(File.join(described_class::DEFAULT_OPTIONS[:output_directory], "rspec_failed_tests.json"), 'w').once
          expect(File).to receive(:open)
            .with(File.join(described_class::DEFAULT_OPTIONS[:output_directory], "rspec_ee_failed_tests.json"), 'w')
            .once

          subject.output_failed_tests
        end
      end

      context 'when given an invalid format' do
        subject { described_class.new(options.merge(format: :foo)) }

        it 'raises an exception' do
          expect { subject.output_failed_tests }
            .to raise_error '[FailedTests] Unsupported format `foo` (allowed formats: `oneline` and `json`)!'
        end
      end

      describe 'empty report' do
        let(:file_contents_as_json) do
          {}
        end

        it 'does not fail for output files' do
          subject.output_failed_tests
        end

        it 'returns empty results for suite failures' do
          result = subject.failed_cases_for_suite_collection

          expect(result.values.flatten).to be_empty
        end
      end
    end
  end

  describe 'missing report file' do
    subject { described_class.new(options.merge(previous_tests_report_path: 'unknownfile.json')) }

    it 'does not fail for output files' do
      subject.output_failed_tests
    end

    it 'returns empty results for suite failures' do
      result = subject.failed_cases_for_suite_collection

      expect(result.values.flatten).to be_empty
    end
  end
end