rubocops/cask: Ensure that "verified" URLs with paths end with "/"

- These were being fixed manually[1], so let's make a RuboCop for any
  further occurrences since this is a good rule to enforce[2].

[1] - https://github.com/Homebrew/homebrew-cask/pull/144179#issuecomment-1489857249
[2] - https://github.com/Homebrew/homebrew-cask/pull/80965#issuecomment-616232313
This commit is contained in:
Issy Long 2023-04-02 15:29:17 +01:00
parent 931327df1f
commit b586d97f84
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
2 changed files with 64 additions and 7 deletions

View File

@ -8,13 +8,13 @@ module RuboCop
#
# @example
# # bad
# url "https://example.com/foo.dmg",
# verified: "https://example.com"
# url "https://example.com/download/foo.dmg",
# verified: "https://example.com/download"
#
#
# # good
# url "https://example.com/foo.dmg",
# verified: "example.com"
# url "https://example.com/download/foo.dmg",
# verified: "example.com/download/"
#
class Url < Base
extend AutoCorrector
@ -30,8 +30,8 @@ module RuboCop
hash_node.each_pair do |key_node, value_node|
next unless key_node.source == "verified"
next unless value_node.str_type?
next unless value_node.source.start_with?(%r{^"https?://})
if value_node.source.start_with?(%r{^"https?://})
add_offense(
value_node.source_range,
message: "Verified URL parameter value should not start with https:// or http://.",
@ -39,6 +39,17 @@ module RuboCop
corrector.replace(value_node.source_range, value_node.source.gsub(%r{^"https?://}, "\""))
end
end
next unless value_node.str_content.gsub(%r{https?://}, "").include?("/") # Skip if the stanza has no path.
next if value_node.str_content.end_with?("/")
add_offense(
value_node.source_range,
message: "Verified URL parameter value should end with a /.",
) do |corrector|
corrector.replace(value_node.source_range, value_node.source.gsub(/"$/, "/\""))
end
end
end
end
end

View File

@ -54,4 +54,50 @@ describe RuboCop::Cop::Cask::Url do
include_examples "reports offenses"
include_examples "autocorrects source"
end
context "when url 'verified' value has a path component that ends with a /" do
let(:source) do
<<~CASK
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/"
end
CASK
end
include_examples "does not report any offenses"
end
context "when the url 'verified' value has a path component that doesn't end with a /" do
let(:source) do
<<~CASK
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download"
end
CASK
end
let(:expected_offenses) do
[{
message: "Verified URL parameter value should end with a /.",
severity: :convention,
line: 3,
column: 14,
source: "\"example.com/download\"",
}]
end
let(:correct_source) do
<<~CASK
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/"
end
CASK
end
include_examples "reports offenses"
include_examples "autocorrects source"
end
end