Commit 78c7a7b5 authored by Maxime Orefice's avatar Maxime Orefice Committed by Peter Leitzen

Add AccessibilityReportsComparer class

parent 3bcb4928
......@@ -36,6 +36,10 @@ module Gitlab
def set_error_message(error)
@error_message = error
end
def all_errors
@urls.values.flatten
end
end
end
end
......
# frozen_string_literal: true
module Gitlab
module Ci
module Reports
class AccessibilityReportsComparer
include Gitlab::Utils::StrongMemoize
STATUS_SUCCESS = 'success'
STATUS_FAILED = 'failed'
attr_reader :base_reports, :head_reports
def initialize(base_reports, head_reports)
@base_reports = base_reports || AccessibilityReports.new
@head_reports = head_reports
end
def status
head_reports.errors_count.positive? ? STATUS_FAILED : STATUS_SUCCESS
end
def existing_errors
strong_memoize(:existing_errors) do
base_reports.all_errors
end
end
def new_errors
strong_memoize(:new_errors) do
head_reports.all_errors - base_reports.all_errors
end
end
def resolved_errors
strong_memoize(:resolved_errors) do
base_reports.all_errors - head_reports.all_errors
end
end
def errors_count
head_reports.errors_count
end
def resolved_count
resolved_errors.size
end
def total_count
existing_errors.size + new_errors.size
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
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(:url) { "https://gitlab.com" }
let(:single_error) do
[
{
"code" => "WCAG2AA.Principle4.Guideline4_1.4_1_2.H91.A.NoContent",
"type" => "error",
"typeCode" => 1,
"message" => "Anchor element found with a valid href attribute, but no link content has been supplied.",
"context" => %{<a href="/" class="navbar-brand animated"><svg height="36" viewBox="0 0 1...</a>},
"selector" => "#main-nav > div:nth-child(1) > a",
"runner" => "htmlcs",
"runnerExtras" => {}
}
]
end
let(:different_error) do
[
{
"code" => "WCAG2AA.Principle1.Guideline1_4.1_4_3.G18.Fail",
"type" => "error",
"typeCode" => 1,
"message" => "This element has insufficient contrast at this conformance level.",
"context" => %{<a href="/stages-devops-lifecycle/" class="main-nav-link">Product</a>},
"selector" => "#main-nav > div:nth-child(2) > ul > li:nth-child(1) > a",
"runner" => "htmlcs",
"runnerExtras" => {}
}
]
end
describe '#status' do
subject { comparer.status }
context 'when head report has an error' do
before do
head_reports.add_url(url, single_error)
end
it 'returns status failed' do
expect(subject).to eq(described_class::STATUS_FAILED)
end
end
context 'when head reports does not have errors' do
before do
head_reports.add_url(url, [])
end
it 'returns status success' do
expect(subject).to eq(described_class::STATUS_SUCCESS)
end
end
end
describe '#errors_count' do
subject { comparer.errors_count }
context 'when head report has an error' do
before do
head_reports.add_url(url, single_error)
end
it 'returns the number of new errors' do
expect(subject).to eq(1)
end
end
context 'when head reports does not have an error' do
before do
head_reports.add_url(url, [])
end
it 'returns the number new errors' do
expect(subject).to eq(0)
end
end
end
describe '#resolved_count' do
subject { 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)
end
it 'returns the resolved count' do
expect(subject).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, [])
end
it 'returns the resolved count' do
expect(subject).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)
end
it 'returns zero' do
expect(subject).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)
end
it 'returns the number of resolved errors' do
expect(subject).to eq(0)
end
end
end
describe '#total_count' do
subject { comparer.total_count }
context 'when base reports has an error' do
before do
base_reports.add_url(url, single_error)
end
it 'returns the error count' do
expect(subject).to eq(1)
end
end
context 'when head report has an error' do
before do
head_reports.add_url(url, single_error)
end
it 'returns the error count' do
expect(subject).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)
end
it 'returns the error count' do
expect(subject).to eq(2)
end
end
end
describe '#existing_errors' do
subject { 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)
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")
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)
end
it 'returns an empty array' do
expect(subject).to be_empty
end
end
end
describe '#new_errors' do
subject { 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)
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")
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, [])
end
it 'returns an empty array' do
expect(subject).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)
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")
end
end
end
describe '#resolved_errors' do
subject { 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)
end
it 'returns an empty array' do
expect(subject).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)
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")
end
end
context 'when base reports does not have errors and head has errors' do
before do
head_reports.add_url(url, single_error)
end
it 'returns an empty array' do
expect(subject).to be_empty
end
end
end
end
......@@ -12,7 +12,7 @@ describe Gitlab::Ci::Reports::AccessibilityReports do
"type": "error",
"typeCode": 1,
"message": "Anchor element found with a valid href attribute, but no link content has been supplied.",
"context": "<a href=\"/customers/worldline\">\n<svg viewBox=\"0 0 509 89\" xmln...</a>",
"context": %{<a href="/customers/worldline"><svg viewBox="0 0 509 89" xmln...</a>},
"selector": "html > body > div:nth-child(9) > div:nth-child(2) > a:nth-child(17)",
"runner": "htmlcs",
"runnerExtras": {}
......@@ -22,7 +22,7 @@ describe Gitlab::Ci::Reports::AccessibilityReports do
"type": "error",
"typeCode": 1,
"message": "Anchor element found with a valid href attribute, but no link content has been supplied.",
"context": "<a href=\"/customers/equinix\">\n<svg xmlns=\"http://www.w3.org/...</a>",
"context": %{<a href="/customers/equinix"><svg xmlns="http://www.w3.org/...</a>},
"selector": "html > body > div:nth-child(9) > div:nth-child(2) > a:nth-child(18)",
"runner": "htmlcs",
"runnerExtras": {}
......@@ -199,4 +199,34 @@ describe Gitlab::Ci::Reports::AccessibilityReports do
end
end
end
describe '#all_errors' do
subject { accessibility_report.all_errors }
context 'when data has errors' do
before do
accessibility_report.add_url(url, data)
end
it 'returns all errors' do
expect(subject.size).to eq(2)
end
end
context 'when data has no errors' do
before do
accessibility_report.add_url(url, [])
end
it 'returns an empty array' do
expect(subject).to eq([])
end
end
context 'when accessibility report has no data' do
it 'returns an empty array' do
expect(subject).to eq([])
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment