Skip to content
代码片段 群组 项目
提交 f5eb9709 编辑于 作者: Vasilii Iakliushin's avatar Vasilii Iakliushin
浏览文件

Check `help_page_url` by documentation link linter

Related issue: https://gitlab.com/gitlab-org/gitlab/-/issues/208354
Previou implementation MR:
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/35351

**Problem**

The documentation link linter checks links defined by `help_page_path`
helper, however it ignores links from `help_page_url`.

**Solution**

Extend the pattern to match both `help_page_path` and `help_page_url`
helper methods
上级 684630e8
No related branches found
No related tags found
加载中
...@@ -13,7 +13,7 @@ class DocumentationLinks < Linter ...@@ -13,7 +13,7 @@ class DocumentationLinks < Linter
DOCS_DIRECTORY = File.join(File.expand_path('../..', __dir__), 'doc') DOCS_DIRECTORY = File.join(File.expand_path('../..', __dir__), 'doc')
HELP_PATH_LINK_PATTERN = <<~PATTERN HELP_PATH_LINK_PATTERN = <<~PATTERN
`(send nil? :help_page_path $...) `(send nil? {:help_page_url :help_page_path} $...)
PATTERN PATTERN
MARKDOWN_HEADER = %r{\A\#{1,6}\s+(?<header>.+)\Z}.freeze MARKDOWN_HEADER = %r{\A\#{1,6}\s+(?<header>.+)\Z}.freeze
......
...@@ -8,75 +8,85 @@ ...@@ -8,75 +8,85 @@
RSpec.describe HamlLint::Linter::DocumentationLinks do RSpec.describe HamlLint::Linter::DocumentationLinks do
include_context 'linter' include_context 'linter'
context 'when link_to points to the existing file path' do shared_examples 'link validation rules' do |link_pattern|
let(:haml) { "= link_to 'Description', help_page_path('README.md')" } context 'when link_to points to the existing file path' do
let(:haml) { "= link_to 'Description', #{link_pattern}('README.md')" }
it { is_expected.not_to report_lint } it { is_expected.not_to report_lint }
end end
context 'when link_to points to the existing file with valid anchor' do context 'when link_to points to the existing file with valid anchor' do
let(:haml) { "= link_to 'Description', help_page_path('README.md', anchor: 'overview'), target: '_blank'" } let(:haml) { "= link_to 'Description', #{link_pattern}('README.md', anchor: 'overview'), target: '_blank'" }
it { is_expected.not_to report_lint } it { is_expected.not_to report_lint }
end end
context 'when link_to points to the existing file path without .md extension' do context 'when link_to points to the existing file path without .md extension' do
let(:haml) { "= link_to 'Description', help_page_path('README')" } let(:haml) { "= link_to 'Description', #{link_pattern}('README')" }
it { is_expected.not_to report_lint } it { is_expected.not_to report_lint }
end end
context 'when anchor is not correct' do context 'when anchor is not correct' do
let(:haml) { "= link_to 'Description', help_page_path('README.md', anchor: 'wrong')" } let(:haml) { "= link_to 'Description', #{link_pattern}('README.md', anchor: 'wrong')" }
it { is_expected.to report_lint } it { is_expected.to report_lint }
context 'when help_page_path has multiple options' do context "when #{link_pattern} has multiple options" do
let(:haml) { "= link_to 'Description', help_page_path('README.md', key: :value, anchor: 'wrong')" } let(:haml) { "= link_to 'Description', #{link_pattern}('README.md', key: :value, anchor: 'wrong')" }
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'" }
it { is_expected.to report_lint } it { is_expected.to report_lint }
end end
end
context 'when file path is wrong' do context 'when link with wrong file path is assigned to a variable' do
let(:haml) { "= link_to 'Description', help_page_path('wrong.md'), target: '_blank'" } let(:haml) { "- my_link = link_to 'Description', #{link_pattern}('wrong.md')" }
it { is_expected.to report_lint } it { is_expected.to report_lint }
end end
context 'when link with wrong file path is assigned to a variable' do context 'when it is a broken code' do
let(:haml) { "- my_link = link_to 'Description', help_page_path('wrong.md')" } let(:haml) { "= I am broken! ]]]]" }
it { is_expected.to report_lint } it { is_expected.not_to report_lint }
end end
context 'when it is a broken code' do context 'when anchor belongs to a different element' do
let(:haml) { "= I am broken! ]]]]" } let(:haml) { "= link_to 'Description', #{link_pattern}('README.md'), target: (anchor: 'blank')" }
it { is_expected.not_to report_lint } it { is_expected.not_to report_lint }
end end
context 'when anchor belongs to a different element' do context "when a simple #{link_pattern}" do
let(:haml) { "= link_to 'Description', help_page_path('README.md'), target: (anchor: 'blank')" } let(:haml) { "- url = #{link_pattern}('wrong.md')" }
it { is_expected.not_to report_lint } it { is_expected.to report_lint }
end end
context 'when a simple help_page_path' do context 'when link is not a string' do
let(:haml) { "- url = help_page_path('wrong.md')" } let(:haml) { "- url = #{link_pattern}(help_url)" }
it { is_expected.to report_lint } it { is_expected.not_to report_lint }
end end
context 'when link is not a string' do context 'when link is a part of the tag' do
let(:haml) { "- url = help_page_path(help_url)" } let(:haml) { ".data-form{ data: { url: #{link_pattern}('wrong.md') } }" }
it { is_expected.not_to report_lint } it { is_expected.to report_lint }
end
end end
context 'when link is a part of the tag' do context 'help_page_path' do
let(:haml) { ".data-form{ data: { url: help_page_path('wrong.md') } }" } it_behaves_like 'link validation rules', 'help_page_path'
end
it { is_expected.to report_lint } context 'help_page_url' do
it_behaves_like 'link validation rules', 'help_page_url'
end end
end end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册