debian-mirror-gitlab/spec/scripts/failed_tests_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
3.5 KiB
Ruby
Raw Normal View History

2021-12-11 22:18:48 +05:30
# frozen_string_literal: true
2023-01-13 00:05:48 +05:30
require 'fast_spec_helper'
2021-12-11 22:18:48 +05:30
require_relative '../../scripts/failed_tests'
RSpec.describe FailedTests do
let(:report_file) { 'spec/fixtures/scripts/test_report.json' }
2023-04-23 21:23:45 +05:30
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 pg12 10/12',
'test_cases' => [
{
'status' => 'failed',
'file' => failure_path
}
]
},
{
'failed_count' => 1,
'name' => 'rspec-ee unit pg12',
'test_cases' => [
{
'status' => 'failed',
'file' => failure_path
}
]
},
{
'failed_count' => 1,
'name' => 'rspec unit pg13 10/12',
'test_cases' => [
{
'status' => 'failed',
'file' => other_failure_path
}
]
}
]
}
2021-12-11 22:18:48 +05:30
end
2023-04-23 21:23:45 +05:30
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
2021-12-11 22:18:48 +05:30
end
end
describe 'missing report file' do
2023-04-23 21:23:45 +05:30
subject { described_class.new(options.merge(previous_tests_report_path: 'unknownfile.json')) }
2021-12-11 22:18:48 +05:30
it 'does not fail for output files' do
2023-04-23 21:23:45 +05:30
subject.output_failed_tests
2021-12-11 22:18:48 +05:30
end
it 'returns empty results for suite failures' do
2023-04-23 21:23:45 +05:30
result = subject.failed_cases_for_suite_collection
2021-12-11 22:18:48 +05:30
expect(result.values.flatten).to be_empty
end
end
end