diff options
Diffstat (limited to 'spec/lib/gitlab/ci/reports')
4 files changed, 628 insertions, 66 deletions
diff --git a/spec/lib/gitlab/ci/reports/accessibility_reports_comparer_spec.rb b/spec/lib/gitlab/ci/reports/accessibility_reports_comparer_spec.rb index 650ae41320b..ade0e36cf1e 100644 --- a/spec/lib/gitlab/ci/reports/accessibility_reports_comparer_spec.rb +++ b/spec/lib/gitlab/ci/reports/accessibility_reports_comparer_spec.rb @@ -3,9 +3,9 @@ require 'spec_helper' RSpec.describe Gitlab::Ci::Reports::AccessibilityReportsComparer do - let(:comparer) { described_class.new(base_reports, head_reports) } - let(:base_reports) { Gitlab::Ci::Reports::AccessibilityReports.new } - let(:head_reports) { Gitlab::Ci::Reports::AccessibilityReports.new } + let(:comparer) { described_class.new(base_report, head_report) } + let(:base_report) { Gitlab::Ci::Reports::AccessibilityReports.new } + let(:head_report) { Gitlab::Ci::Reports::AccessibilityReports.new } let(:url) { "https://gitlab.com" } let(:single_error) do [ @@ -38,233 +38,254 @@ RSpec.describe Gitlab::Ci::Reports::AccessibilityReportsComparer do end describe '#status' do - subject { comparer.status } + subject(:status) { comparer.status } context 'when head report has an error' do before do - head_reports.add_url(url, single_error) + head_report.add_url(url, single_error) end it 'returns status failed' do - expect(subject).to eq(described_class::STATUS_FAILED) + expect(status).to eq(described_class::STATUS_FAILED) end end context 'when head reports does not have errors' do before do - head_reports.add_url(url, []) + head_report.add_url(url, []) end it 'returns status success' do - expect(subject).to eq(described_class::STATUS_SUCCESS) + expect(status).to eq(described_class::STATUS_SUCCESS) end end end describe '#errors_count' do - subject { comparer.errors_count } + subject(:errors_count) { comparer.errors_count } context 'when head report has an error' do before do - head_reports.add_url(url, single_error) + head_report.add_url(url, single_error) end it 'returns the number of new errors' do - expect(subject).to eq(1) + expect(errors_count).to eq(1) end end context 'when head reports does not have an error' do before do - head_reports.add_url(url, []) + head_report.add_url(url, []) end it 'returns the number new errors' do - expect(subject).to eq(0) + expect(errors_count).to eq(0) end end end describe '#resolved_count' do - subject { comparer.resolved_count } + subject(:resolved_count) { comparer.resolved_count } context 'when base reports has an error and head has a different error' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, different_error) + base_report.add_url(url, single_error) + head_report.add_url(url, different_error) end it 'returns the resolved count' do - expect(subject).to eq(1) + expect(resolved_count).to eq(1) end end context 'when base reports has errors head has no errors' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, []) + base_report.add_url(url, single_error) + head_report.add_url(url, []) end it 'returns the resolved count' do - expect(subject).to eq(1) + expect(resolved_count).to eq(1) end end context 'when base reports has errors and head has the same error' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, single_error) + base_report.add_url(url, single_error) + head_report.add_url(url, single_error) end it 'returns zero' do - expect(subject).to eq(0) + expect(resolved_count).to eq(0) end end context 'when base reports does not have errors and head has errors' do before do - head_reports.add_url(url, single_error) + head_report.add_url(url, single_error) end it 'returns the number of resolved errors' do - expect(subject).to eq(0) + expect(resolved_count).to eq(0) end end end describe '#total_count' do - subject { comparer.total_count } + subject(:total_count) { comparer.total_count } context 'when base reports has an error' do before do - base_reports.add_url(url, single_error) + base_report.add_url(url, single_error) end - it 'returns the error count' do - expect(subject).to eq(1) + it 'returns zero' do + expect(total_count).to be_zero end end context 'when head report has an error' do before do - head_reports.add_url(url, single_error) + head_report.add_url(url, single_error) end - it 'returns the error count' do - expect(subject).to eq(1) + it 'returns the total count' do + expect(total_count).to eq(1) end end context 'when base report has errors and head report has errors' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, different_error) + base_report.add_url(url, single_error) + head_report.add_url(url, different_error) + end + + it 'returns the total count' do + expect(total_count).to eq(1) + end + end + + context 'when base report has errors and head report has the same error' do + before do + base_report.add_url(url, single_error) + head_report.add_url(url, single_error + different_error) end - it 'returns the error count' do - expect(subject).to eq(2) + it 'returns the total count' do + expect(total_count).to eq(2) end end end describe '#existing_errors' do - subject { comparer.existing_errors } + subject(:existing_errors) { comparer.existing_errors } context 'when base report has errors and head has a different error' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, different_error) + base_report.add_url(url, single_error) + head_report.add_url(url, different_error) end - it 'returns the existing errors' do - expect(subject.size).to eq(1) - expect(subject.first["code"]).to eq("WCAG2AA.Principle4.Guideline4_1.4_1_2.H91.A.NoContent") + it 'returns an empty array' do + expect(existing_errors).to be_empty end end context 'when base report does not have errors and head has errors' do before do - base_reports.add_url(url, []) - head_reports.add_url(url, single_error) + base_report.add_url(url, []) + head_report.add_url(url, single_error) end it 'returns an empty array' do - expect(subject).to be_empty + expect(existing_errors).to be_empty + end + end + + context 'when base report has errors and head report has the same error' do + before do + base_report.add_url(url, single_error) + head_report.add_url(url, single_error + different_error) + end + + it 'returns the existing error' do + expect(existing_errors).to eq(single_error) end end end describe '#new_errors' do - subject { comparer.new_errors } + subject(:new_errors) { comparer.new_errors } context 'when base reports has errors and head has more errors' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, single_error + different_error) + base_report.add_url(url, single_error) + head_report.add_url(url, single_error + different_error) end it 'returns new errors between base and head reports' do - expect(subject.size).to eq(1) - expect(subject.first["code"]).to eq("WCAG2AA.Principle1.Guideline1_4.1_4_3.G18.Fail") + expect(new_errors.size).to eq(1) + expect(new_errors.first["code"]).to eq("WCAG2AA.Principle1.Guideline1_4.1_4_3.G18.Fail") end end context 'when base reports has an error and head has no errors' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, []) + base_report.add_url(url, single_error) + head_report.add_url(url, []) end it 'returns an empty array' do - expect(subject).to be_empty + expect(new_errors).to be_empty end end context 'when base reports does not have errors and head has errors' do before do - head_reports.add_url(url, single_error) + head_report.add_url(url, single_error) end it 'returns the new error' do - expect(subject.size).to eq(1) - expect(subject.first["code"]).to eq("WCAG2AA.Principle4.Guideline4_1.4_1_2.H91.A.NoContent") + expect(new_errors.size).to eq(1) + expect(new_errors.first["code"]).to eq("WCAG2AA.Principle4.Guideline4_1.4_1_2.H91.A.NoContent") end end end describe '#resolved_errors' do - subject { comparer.resolved_errors } + subject(:resolved_errors) { comparer.resolved_errors } context 'when base report has errors and head has more errors' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, single_error + different_error) + base_report.add_url(url, single_error) + head_report.add_url(url, single_error + different_error) end it 'returns an empty array' do - expect(subject).to be_empty + expect(resolved_errors).to be_empty end end context 'when base reports has errors and head has a different error' do before do - base_reports.add_url(url, single_error) - head_reports.add_url(url, different_error) + base_report.add_url(url, single_error) + head_report.add_url(url, different_error) end it 'returns the resolved errors' do - expect(subject.size).to eq(1) - expect(subject.first["code"]).to eq("WCAG2AA.Principle4.Guideline4_1.4_1_2.H91.A.NoContent") + expect(resolved_errors.size).to eq(1) + expect(resolved_errors.first["code"]).to eq("WCAG2AA.Principle4.Guideline4_1.4_1_2.H91.A.NoContent") end end context 'when base reports does not have errors and head has errors' do before do - head_reports.add_url(url, single_error) + head_report.add_url(url, single_error) end it 'returns an empty array' do - expect(subject).to be_empty + expect(resolved_errors).to be_empty end end end diff --git a/spec/lib/gitlab/ci/reports/codequality_reports_comparer_spec.rb b/spec/lib/gitlab/ci/reports/codequality_reports_comparer_spec.rb new file mode 100644 index 00000000000..7053d54381b --- /dev/null +++ b/spec/lib/gitlab/ci/reports/codequality_reports_comparer_spec.rb @@ -0,0 +1,308 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do + let(:comparer) { described_class.new(base_report, head_report) } + let(:base_report) { Gitlab::Ci::Reports::CodequalityReports.new } + let(:head_report) { Gitlab::Ci::Reports::CodequalityReports.new } + let(:degradation_1) do + { + "categories": [ + "Complexity" + ], + "check_name": "argument_count", + "content": { + "body": "" + }, + "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.", + "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547", + "location": { + "path": "foo.rb", + "lines": { + "begin": 10, + "end": 10 + } + }, + "other_locations": [], + "remediation_points": 900000, + "severity": "major", + "type": "issue", + "engine_name": "structure" + }.with_indifferent_access + end + + let(:degradation_2) do + { + "type": "Issue", + "check_name": "Rubocop/Metrics/ParameterLists", + "description": "Avoid parameter lists longer than 5 parameters. [12/5]", + "categories": [ + "Complexity" + ], + "remediation_points": 550000, + "location": { + "path": "foo.rb", + "positions": { + "begin": { + "column": 14, + "line": 10 + }, + "end": { + "column": 39, + "line": 10 + } + } + }, + "content": { + "body": "This cop checks for methods with too many parameters.\nThe maximum number of parameters is configurable.\nKeyword arguments can optionally be excluded from the total count." + }, + "engine_name": "rubocop", + "fingerprint": "ab5f8b935886b942d621399f5a2ca16e", + "severity": "minor" + }.with_indifferent_access + end + + describe '#status' do + subject(:report_status) { comparer.status } + + context 'when head report has an error' do + before do + head_report.add_degradation(degradation_1) + end + + it 'returns status failed' do + expect(report_status).to eq(described_class::STATUS_FAILED) + end + end + + context 'when head report does not have errors' do + it 'returns status success' do + expect(report_status).to eq(described_class::STATUS_SUCCESS) + end + end + end + + describe '#errors_count' do + subject(:errors_count) { comparer.errors_count } + + context 'when head report has an error' do + before do + head_report.add_degradation(degradation_1) + end + + it 'returns the number of new errors' do + expect(errors_count).to eq(1) + end + end + + context 'when head report does not have an error' do + it 'returns zero' do + expect(errors_count).to be_zero + end + end + end + + describe '#resolved_count' do + subject(:resolved_count) { comparer.resolved_count } + + context 'when base report has an error and head has a different error' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'counts the base report error as resolved' do + expect(resolved_count).to eq(1) + end + end + + context 'when base report has errors head has no errors' do + before do + base_report.add_degradation(degradation_1) + end + + it 'counts the base report errors as resolved' do + expect(resolved_count).to eq(1) + end + end + + context 'when base report has errors and head has the same error' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_1) + end + + it 'returns zero' do + expect(resolved_count).to eq(0) + end + end + + context 'when base report does not have errors and head has errors' do + before do + head_report.add_degradation(degradation_1) + end + + it 'returns zero' do + expect(resolved_count).to be_zero + end + end + end + + describe '#total_count' do + subject(:total_count) { comparer.total_count } + + context 'when base report has an error' do + before do + base_report.add_degradation(degradation_1) + end + + it 'returns zero' do + expect(total_count).to be_zero + end + end + + context 'when head report has an error' do + before do + head_report.add_degradation(degradation_1) + end + + it 'includes the head report error in the count' do + expect(total_count).to eq(1) + end + end + + context 'when base report has errors and head report has errors' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'includes errors in the count' do + expect(total_count).to eq(1) + end + end + + context 'when base report has errors and head report has the same error' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'includes errors in the count' do + expect(total_count).to eq(2) + end + end + end + + describe '#existing_errors' do + subject(:existing_errors) { comparer.existing_errors } + + context 'when base report has errors and head has the same error' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'includes the base report errors' do + expect(existing_errors).to contain_exactly(degradation_1) + end + end + + context 'when base report has errors and head has a different error' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'returns an empty array' do + expect(existing_errors).to be_empty + end + end + + context 'when base report does not have errors and head has errors' do + before do + head_report.add_degradation(degradation_1) + end + + it 'returns an empty array' do + expect(existing_errors).to be_empty + end + end + end + + describe '#new_errors' do + subject(:new_errors) { comparer.new_errors } + + context 'when base report has errors and head has more errors' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'includes errors not found in the base report' do + expect(new_errors).to eq([degradation_2]) + end + end + + context 'when base report has an error and head has no errors' do + before do + base_report.add_degradation(degradation_1) + end + + it 'returns an empty array' do + expect(new_errors).to be_empty + end + end + + context 'when base report does not have errors and head has errors' do + before do + head_report.add_degradation(degradation_1) + end + + it 'returns the head report error' do + expect(new_errors).to eq([degradation_1]) + end + end + end + + describe '#resolved_errors' do + subject(:resolved_errors) { comparer.resolved_errors } + + context 'when base report errors are still found in the head report' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'returns an empty array' do + expect(resolved_errors).to be_empty + end + end + + context 'when base report has errors and head has a different error' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'returns the base report error' do + expect(resolved_errors).to eq([degradation_1]) + end + end + + context 'when base report does not have errors and head has errors' do + before do + head_report.add_degradation(degradation_1) + end + + it 'returns an empty array' do + expect(resolved_errors).to be_empty + end + end + end +end diff --git a/spec/lib/gitlab/ci/reports/codequality_reports_spec.rb b/spec/lib/gitlab/ci/reports/codequality_reports_spec.rb new file mode 100644 index 00000000000..44e67259369 --- /dev/null +++ b/spec/lib/gitlab/ci/reports/codequality_reports_spec.rb @@ -0,0 +1,136 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Reports::CodequalityReports do + let(:codequality_report) { described_class.new } + let(:degradation_1) do + { + "categories": [ + "Complexity" + ], + "check_name": "argument_count", + "content": { + "body": "" + }, + "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.", + "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547", + "location": { + "path": "foo.rb", + "lines": { + "begin": 10, + "end": 10 + } + }, + "other_locations": [], + "remediation_points": 900000, + "severity": "major", + "type": "issue", + "engine_name": "structure" + }.with_indifferent_access + end + + let(:degradation_2) do + { + "type": "Issue", + "check_name": "Rubocop/Metrics/ParameterLists", + "description": "Avoid parameter lists longer than 5 parameters. [12/5]", + "categories": [ + "Complexity" + ], + "remediation_points": 550000, + "location": { + "path": "foo.rb", + "positions": { + "begin": { + "column": 14, + "line": 10 + }, + "end": { + "column": 39, + "line": 10 + } + } + }, + "content": { + "body": "This cop checks for methods with too many parameters.\nThe maximum number of parameters is configurable.\nKeyword arguments can optionally be excluded from the total count." + }, + "engine_name": "rubocop", + "fingerprint": "ab5f8b935886b942d621399f5a2ca16e", + "severity": "minor" + }.with_indifferent_access + end + + it { expect(codequality_report.degradations).to eq({}) } + + describe '#add_degradation' do + context 'when there is a degradation' do + before do + codequality_report.add_degradation(degradation_1) + end + + it 'adds degradation to codequality report' do + expect(codequality_report.degradations.keys).to eq([degradation_1[:fingerprint]]) + expect(codequality_report.degradations.values.size).to eq(1) + end + end + + context 'when a required property is missing in the degradation' do + let(:invalid_degradation) do + { + "type": "Issue", + "check_name": "Rubocop/Metrics/ParameterLists", + "description": "Avoid parameter lists longer than 5 parameters. [12/5]", + "fingerprint": "ab5f8b935886b942d621399aefkaehfiaehf", + "severity": "minor" + }.with_indifferent_access + end + + it 'sets location as an error' do + codequality_report.add_degradation(invalid_degradation) + + expect(codequality_report.error_message).to eq("Invalid degradation format: The property '#/' did not contain a required property of 'location'") + end + end + end + + describe '#set_error_message' do + context 'when there is an error' do + it 'sets errors' do + codequality_report.set_error_message("error") + + expect(codequality_report.error_message).to eq("error") + end + end + end + + describe '#degradations_count' do + subject(:degradations_count) { codequality_report.degradations_count } + + context 'when there are many degradations' do + before do + codequality_report.add_degradation(degradation_1) + codequality_report.add_degradation(degradation_2) + end + + it 'returns the number of degradations' do + expect(degradations_count).to eq(2) + end + end + end + + describe '#all_degradations' do + subject(:all_degradations) { codequality_report.all_degradations } + + context 'when there are many degradations' do + before do + codequality_report.add_degradation(degradation_1) + codequality_report.add_degradation(degradation_2) + end + + it 'returns all degradations' do + expect(all_degradations).to contain_exactly(degradation_1, degradation_2) + end + end + end +end diff --git a/spec/lib/gitlab/ci/reports/reports_comparer_spec.rb b/spec/lib/gitlab/ci/reports/reports_comparer_spec.rb new file mode 100644 index 00000000000..1e5e4766583 --- /dev/null +++ b/spec/lib/gitlab/ci/reports/reports_comparer_spec.rb @@ -0,0 +1,97 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Reports::ReportsComparer do + let(:comparer) { described_class.new(base_report, head_report) } + let(:base_report) { Gitlab::Ci::Reports::CodequalityReports.new } + let(:head_report) { Gitlab::Ci::Reports::CodequalityReports.new } + + describe '#initialize' do + context 'sets getter for the report comparer' do + it 'return base report' do + expect(comparer.base_report).to be_an_instance_of(Gitlab::Ci::Reports::CodequalityReports) + end + + it 'return head report' do + expect(comparer.head_report).to be_an_instance_of(Gitlab::Ci::Reports::CodequalityReports) + end + end + end + + describe '#status' do + subject(:status) { comparer.status } + + it 'returns not implemented error' do + expect { status }.to raise_error(NotImplementedError) + end + + context 'when success? is true' do + before do + allow(comparer).to receive(:success?).and_return(true) + end + + it 'returns status success' do + expect(status).to eq('success') + end + end + + context 'when success? is false' do + before do + allow(comparer).to receive(:success?).and_return(false) + end + + it 'returns status failed' do + expect(status).to eq('failed') + end + end + end + + describe '#success?' do + subject(:success?) { comparer.success? } + + it 'returns not implemented error' do + expect { success? }.to raise_error(NotImplementedError) + end + end + + describe '#existing_errors' do + subject(:existing_errors) { comparer.existing_errors } + + it 'returns not implemented error' do + expect { existing_errors }.to raise_error(NotImplementedError) + end + end + + describe '#resolved_errors' do + subject(:resolved_errors) { comparer.resolved_errors } + + it 'returns not implemented error' do + expect { resolved_errors }.to raise_error(NotImplementedError) + end + end + + describe '#errors_count' do + subject(:errors_count) { comparer.errors_count } + + it 'returns not implemented error' do + expect { errors_count }.to raise_error(NotImplementedError) + end + end + + describe '#resolved_count' do + subject(:resolved_count) { comparer.resolved_count } + + it 'returns not implemented error' do + expect { resolved_count }.to raise_error(NotImplementedError) + end + end + + describe '#total_count' do + subject(:total_count) { comparer.total_count } + + it 'returns not implemented error' do + expect { total_count }.to raise_error(NotImplementedError) + end + end +end |