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:
parent
931327df1f
commit
b586d97f84
@ -8,13 +8,13 @@ module RuboCop
|
|||||||
#
|
#
|
||||||
# @example
|
# @example
|
||||||
# # bad
|
# # bad
|
||||||
# url "https://example.com/foo.dmg",
|
# url "https://example.com/download/foo.dmg",
|
||||||
# verified: "https://example.com"
|
# verified: "https://example.com/download"
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# # good
|
# # good
|
||||||
# url "https://example.com/foo.dmg",
|
# url "https://example.com/download/foo.dmg",
|
||||||
# verified: "example.com"
|
# verified: "example.com/download/"
|
||||||
#
|
#
|
||||||
class Url < Base
|
class Url < Base
|
||||||
extend AutoCorrector
|
extend AutoCorrector
|
||||||
@ -30,13 +30,24 @@ module RuboCop
|
|||||||
hash_node.each_pair do |key_node, value_node|
|
hash_node.each_pair do |key_node, value_node|
|
||||||
next unless key_node.source == "verified"
|
next unless key_node.source == "verified"
|
||||||
next unless value_node.str_type?
|
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://.",
|
||||||
|
) do |corrector|
|
||||||
|
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(
|
add_offense(
|
||||||
value_node.source_range,
|
value_node.source_range,
|
||||||
message: "Verified URL parameter value should not start with https:// or http://.",
|
message: "Verified URL parameter value should end with a /.",
|
||||||
) do |corrector|
|
) do |corrector|
|
||||||
corrector.replace(value_node.source_range, value_node.source.gsub(%r{^"https?://}, "\""))
|
corrector.replace(value_node.source_range, value_node.source.gsub(/"$/, "/\""))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -54,4 +54,50 @@ describe RuboCop::Cop::Cask::Url do
|
|||||||
include_examples "reports offenses"
|
include_examples "reports offenses"
|
||||||
include_examples "autocorrects source"
|
include_examples "autocorrects source"
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user