2020-07-15 14:09:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-05-06 02:07:56 -04:00
|
|
|
require 'fast_spec_helper'
|
2020-07-15 14:09:09 -04:00
|
|
|
require 'haml_lint'
|
|
|
|
require 'haml_lint/spec'
|
2022-05-06 02:07:56 -04:00
|
|
|
|
|
|
|
require_relative '../../../haml_lint/linter/documentation_links'
|
2020-07-15 14:09:09 -04:00
|
|
|
|
|
|
|
RSpec.describe HamlLint::Linter::DocumentationLinks do
|
|
|
|
include_context 'linter'
|
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
shared_examples 'link validation rules' do |link_pattern|
|
|
|
|
context 'when link_to points to the existing file path' do
|
2021-06-28 08:38:12 -04:00
|
|
|
let(:haml) { "= link_to 'Description', #{link_pattern}('index.md')" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.not_to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'when link_to points to the existing file with valid anchor' do
|
2021-06-28 08:38:12 -04:00
|
|
|
let(:haml) { "= link_to 'Description', #{link_pattern}('index.md', anchor: 'overview'), target: '_blank'" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.not_to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'when link_to points to the existing file path without .md extension' do
|
2021-06-28 08:38:12 -04:00
|
|
|
let(:haml) { "= link_to 'Description', #{link_pattern}('index')" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.not_to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'when anchor is not correct' do
|
2021-06-28 08:38:12 -04:00
|
|
|
let(:haml) { "= link_to 'Description', #{link_pattern}('index.md', anchor: 'wrong')" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.to report_lint }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context "when #{link_pattern} has multiple options" do
|
2021-06-28 08:38:12 -04:00
|
|
|
let(:haml) { "= link_to 'Description', #{link_pattern}('index.md', key: :value, anchor: 'wrong')" }
|
2020-09-21 02:09:41 -04:00
|
|
|
|
|
|
|
it { is_expected.to report_lint }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file path is wrong' do
|
|
|
|
let(:haml) { "= link_to 'Description', #{link_pattern}('wrong.md'), target: '_blank'" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
|
|
|
it { is_expected.to report_lint }
|
2022-04-13 14:08:33 -04:00
|
|
|
|
|
|
|
context 'when haml ends with block definition' do
|
|
|
|
let(:haml) { "= link_to 'Description', #{link_pattern}('wrong.md') do" }
|
|
|
|
|
|
|
|
it { is_expected.to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
end
|
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'when link with wrong file path is assigned to a variable' do
|
|
|
|
let(:haml) { "- my_link = link_to 'Description', #{link_pattern}('wrong.md')" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'when it is a broken code' do
|
|
|
|
let(:haml) { "= I am broken! ]]]]" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.not_to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'when anchor belongs to a different element' do
|
2021-06-28 08:38:12 -04:00
|
|
|
let(:haml) { "= link_to 'Description', #{link_pattern}('index.md'), target: (anchor: 'blank')" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.not_to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context "when a simple #{link_pattern}" do
|
|
|
|
let(:haml) { "- url = #{link_pattern}('wrong.md')" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'when link is not a string' do
|
|
|
|
let(:haml) { "- url = #{link_pattern}(help_url)" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.not_to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'when link is a part of the tag' do
|
|
|
|
let(:haml) { ".data-form{ data: { url: #{link_pattern}('wrong.md') } }" }
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
it { is_expected.to report_lint }
|
|
|
|
end
|
2021-05-07 23:10:38 -04:00
|
|
|
|
|
|
|
context 'when the second link is invalid' do
|
2021-06-28 08:38:12 -04:00
|
|
|
let(:haml) { ".data-form{ data: { url: #{link_pattern}('index.md'), wrong_url: #{link_pattern}('wrong.md') } }" }
|
2021-05-07 23:10:38 -04:00
|
|
|
|
|
|
|
it { is_expected.to report_lint }
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
end
|
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'help_page_path' do
|
|
|
|
it_behaves_like 'link validation rules', 'help_page_path'
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
|
2020-09-21 02:09:41 -04:00
|
|
|
context 'help_page_url' do
|
|
|
|
it_behaves_like 'link validation rules', 'help_page_url'
|
2020-07-15 14:09:09 -04:00
|
|
|
end
|
|
|
|
end
|