# frozen_string_literal: true require 'fast_spec_helper' RSpec.describe Gitlab::Ci::Parsers::Test::Junit do describe '#parse!' do subject { described_class.new.parse!(junit, test_suite, **args) } let(:test_suite) { Gitlab::Ci::Reports::TestSuite.new('rspec') } let(:test_cases) { flattened_test_cases(test_suite) } let(:args) { { job: { id: 1, project: "project" } } } context 'when data is JUnit style XML' do context 'when there are no in ' do let(:junit) do <<-EOF.strip_heredoc EOF end it 'ignores the case' do expect { subject }.not_to raise_error expect(test_cases.count).to eq(0) end end context 'when there are no in ' do let(:junit) do <<-EOF.strip_heredoc EOF end it 'ignores the case' do expect { subject }.not_to raise_error expect(test_cases.count).to eq(0) end end context 'when there is only one in ' do let(:junit) do <<-EOF.strip_heredoc EOF end it 'parses XML and adds a test case to a suite' do expect { subject }.not_to raise_error expect(test_cases[0].classname).to eq('Calculator') expect(test_cases[0].name).to eq('sumTest1') expect(test_cases[0].execution_time).to eq(0.01) end end context 'when there is ' do let(:junit) do <<-EOF.strip_heredoc #{testcase_content} EOF end let(:test_case) { test_cases[0] } before do expect { subject }.not_to raise_error end shared_examples_for ' XML parser' do |status, output| it 'parses XML and adds a test case to the suite' do aggregate_failures do expect(test_case.classname).to eq('Calculator') expect(test_case.name).to eq('sumTest1') expect(test_case.execution_time).to eq(0.01) expect(test_case.status).to eq(status) expect(test_case.system_output).to eq(output) end end end context 'and has failure' do let(:testcase_content) { 'Some failure' } it_behaves_like ' XML parser', ::Gitlab::Ci::Reports::TestCase::STATUS_FAILED, 'Some failure' end context 'and has error' do let(:testcase_content) { 'Some error' } it_behaves_like ' XML parser', ::Gitlab::Ci::Reports::TestCase::STATUS_ERROR, 'Some error' end context 'and has skipped' do let(:testcase_content) { '' } it_behaves_like ' XML parser', ::Gitlab::Ci::Reports::TestCase::STATUS_SKIPPED, nil context 'with an empty double-tag' do let(:testcase_content) { '' } it_behaves_like ' XML parser', ::Gitlab::Ci::Reports::TestCase::STATUS_SKIPPED, nil end end context 'and has an unknown type' do let(:testcase_content) { 'Some foo' } it_behaves_like ' XML parser', ::Gitlab::Ci::Reports::TestCase::STATUS_SUCCESS, nil end context 'and has no content' do let(:testcase_content) { '' } it_behaves_like ' XML parser', ::Gitlab::Ci::Reports::TestCase::STATUS_SUCCESS, nil end end context 'PHPUnit' do let(:junit) do <<-EOF.strip_heredoc EOF end it 'parses XML and adds a test case to a suite' do expect { subject }.not_to raise_error expect(test_cases.count).to eq(1) end end context 'when there are two test cases' do let(:junit) do <<-EOF.strip_heredoc EOF end it 'parses XML and adds test cases to a suite' do expect { subject }.not_to raise_error expect(test_cases[0].classname).to eq('Calculator') expect(test_cases[0].name).to eq('sumTest1') expect(test_cases[0].execution_time).to eq(0.01) expect(test_cases[1].classname).to eq('Calculator') expect(test_cases[1].name).to eq('sumTest2') expect(test_cases[1].execution_time).to eq(0.02) end end context 'when there are two test suites' do let(:junit) do <<-EOF.strip_heredoc EOF end it 'parses XML and adds test cases to a suite' do expect { subject }.not_to raise_error expect(test_cases[0].classname).to eq('Calculator') expect(test_cases[0].name).to eq('sumTest1') expect(test_cases[0].execution_time).to eq(0.01) expect(test_cases[1].classname).to eq('Calculator') expect(test_cases[1].name).to eq('sumTest2') expect(test_cases[1].execution_time).to eq(0.02) expect(test_cases[2].classname).to eq('Statemachine') expect(test_cases[2].name).to eq('happy path') expect(test_cases[2].execution_time).to eq(100) expect(test_cases[3].classname).to eq('Statemachine') expect(test_cases[3].name).to eq('unhappy path') expect(test_cases[3].execution_time).to eq(200) end end end context 'when data is not JUnit style XML' do let(:junit) { { testsuite: 'abc' }.to_json } it 'attaches an error to the TestSuite object' do expect { subject }.not_to raise_error expect(test_cases).to be_empty end end context 'when data is malformed JUnit XML' do let(:junit) do <<-EOF.strip_heredoc EOF end it 'attaches an error to the TestSuite object' do expect { subject }.not_to raise_error expect(test_suite.suite_error).to eq("JUnit XML parsing failed: 4:1: FATAL: expected '>'") end it 'returns 0 tests cases' do subject expect(test_cases).to be_empty expect(test_suite.total_count).to eq(0) expect(test_suite.success_count).to eq(0) expect(test_suite.error_count).to eq(0) end it 'returns a failure status' do subject expect(test_suite.total_status).to eq(Gitlab::Ci::Reports::TestCase::STATUS_ERROR) end end context 'when data is not XML' do let(:junit) { double(:random_trash) } it 'attaches an error to the TestSuite object' do expect { subject }.not_to raise_error expect(test_suite.suite_error).to eq('JUnit data parsing failed: no implicit conversion of RSpec::Mocks::Double into String') end it 'returns 0 tests cases' do subject expect(test_cases).to be_empty expect(test_suite.total_count).to eq(0) expect(test_suite.success_count).to eq(0) expect(test_suite.error_count).to eq(0) end it 'returns a failure status' do subject expect(test_suite.total_status).to eq(Gitlab::Ci::Reports::TestCase::STATUS_ERROR) end end context 'when attachment is specified in failed test case' do let(:junit) do <<~EOF Some failure [[ATTACHMENT|some/path.png]] EOF end it 'assigns correct attributes to the test case' do expect { subject }.not_to raise_error expect(test_cases[0].has_attachment?).to be_truthy expect(test_cases[0].attachment).to eq("some/path.png") expect(test_cases[0].job).to be_present expect(test_cases[0].job[:id]).to eq(1) expect(test_cases[0].job[:project]).to eq("project") end end context 'when data contains multiple attachments tag' do let(:junit) do <<~EOF Some failure [[ATTACHMENT|some/path.png]] [[ATTACHMENT|some/path.html]] EOF end it 'adds the first match attachment to a test case' do expect { subject }.not_to raise_error expect(test_cases[0].has_attachment?).to be_truthy expect(test_cases[0].attachment).to eq("some/path.png") end end context 'when data does not match attachment tag regex' do let(:junit) do <<~EOF Some failure [[attachment]some/path.png]] EOF end it 'does not add attachment to a test case' do expect { subject }.not_to raise_error expect(test_cases[0].has_attachment?).to be_falsy expect(test_cases[0].attachment).to be_nil end end private def flattened_test_cases(test_suite) test_suite.test_cases.map do |status, value| value.map do |key, test_case| test_case end end.flatten end end end