Only 'verified' stanzas with 0 or >1 path components should end with "/"
Handle good things like: ```ruby url "https://example.org/download", verified: "example.org/download" # This is fine. ``` And bad things like: ```ruby url "https://example.org/", verified: "example.org" # This should end with a slash. ```
This commit is contained in:
parent
17c0eaab25
commit
21da074346
@ -24,6 +24,7 @@ module RuboCop
|
|||||||
def on_url_stanza(stanza)
|
def on_url_stanza(stanza)
|
||||||
return if stanza.stanza_node.block_type?
|
return if stanza.stanza_node.block_type?
|
||||||
|
|
||||||
|
url_string = stanza.stanza_node.first_argument.str_content
|
||||||
hash_node = stanza.stanza_node.last_argument
|
hash_node = stanza.stanza_node.last_argument
|
||||||
return unless hash_node.hash_type?
|
return unless hash_node.hash_type?
|
||||||
|
|
||||||
@ -40,7 +41,9 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
next unless value_node.str_content.gsub(%r{https?://}, "").include?("/") # Skip if the stanza has no path.
|
# Skip if the URL and the verified value are the same.
|
||||||
|
next if value_node.str_content == url_string.delete_prefix("https://").delete_prefix("http://")
|
||||||
|
# Skip if the verified value ends with a slash.
|
||||||
next if value_node.str_content.end_with?("/")
|
next if value_node.str_content.end_with?("/")
|
||||||
|
|
||||||
add_offense(
|
add_offense(
|
||||||
|
@ -14,7 +14,7 @@ describe RuboCop::Cop::Cask::Url do
|
|||||||
<<~CASK
|
<<~CASK
|
||||||
cask "foo" do
|
cask "foo" do
|
||||||
url "https://example.com/download/foo-v1.2.0.dmg",
|
url "https://example.com/download/foo-v1.2.0.dmg",
|
||||||
verified: "example.com"
|
verified: "example.com/download/"
|
||||||
end
|
end
|
||||||
CASK
|
CASK
|
||||||
end
|
end
|
||||||
@ -27,7 +27,7 @@ describe RuboCop::Cop::Cask::Url do
|
|||||||
<<~CASK
|
<<~CASK
|
||||||
cask "foo" do
|
cask "foo" do
|
||||||
url "https://example.com/download/foo-v1.2.0.dmg",
|
url "https://example.com/download/foo-v1.2.0.dmg",
|
||||||
verified: "https://example.com"
|
verified: "https://example.com/download/"
|
||||||
end
|
end
|
||||||
CASK
|
CASK
|
||||||
end
|
end
|
||||||
@ -38,7 +38,7 @@ describe RuboCop::Cop::Cask::Url do
|
|||||||
severity: :convention,
|
severity: :convention,
|
||||||
line: 3,
|
line: 3,
|
||||||
column: 16,
|
column: 16,
|
||||||
source: "\"https://example.com\"",
|
source: "\"https://example.com/download/\"",
|
||||||
}]
|
}]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -46,7 +46,114 @@ describe RuboCop::Cop::Cask::Url do
|
|||||||
<<~CASK
|
<<~CASK
|
||||||
cask "foo" do
|
cask "foo" do
|
||||||
url "https://example.com/download/foo-v1.2.0.dmg",
|
url "https://example.com/download/foo-v1.2.0.dmg",
|
||||||
verified: "example.com"
|
verified: "example.com/download/"
|
||||||
|
end
|
||||||
|
CASK
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples "reports offenses"
|
||||||
|
include_examples "autocorrects source"
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when url 'verified' value does not have a path component" do
|
||||||
|
context "when the URL ends with a slash" do
|
||||||
|
let(:source) do
|
||||||
|
<<~CASK
|
||||||
|
cask "foo" do
|
||||||
|
url "https://example.org/",
|
||||||
|
verified: "example.org/"
|
||||||
|
end
|
||||||
|
CASK
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples "does not report any offenses"
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the URL does not end with a slash" do
|
||||||
|
let(:source) do
|
||||||
|
<<~CASK
|
||||||
|
cask "foo" do
|
||||||
|
url "https://example.org/",
|
||||||
|
verified: "example.org"
|
||||||
|
end
|
||||||
|
CASK
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:expected_offenses) do
|
||||||
|
[{
|
||||||
|
message: "Verified URL parameter value should end with a /.",
|
||||||
|
severity: :convention,
|
||||||
|
line: 3,
|
||||||
|
column: 16,
|
||||||
|
source: "\"example.org\"",
|
||||||
|
}]
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:correct_source) do
|
||||||
|
<<~CASK
|
||||||
|
cask "foo" do
|
||||||
|
url "https://example.org/",
|
||||||
|
verified: "example.org/"
|
||||||
|
end
|
||||||
|
CASK
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples "reports offenses"
|
||||||
|
include_examples "autocorrects source"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the URL does not end with a slash" do
|
||||||
|
let(:source) do
|
||||||
|
<<~CASK
|
||||||
|
cask "foo" do
|
||||||
|
url "https://github.com/Foo",
|
||||||
|
verified: "github.com/Foo"
|
||||||
|
end
|
||||||
|
CASK
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples "does not report any offenses"
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the url ends with a / and the verified value does too" do
|
||||||
|
let(:source) do
|
||||||
|
<<~CASK
|
||||||
|
cask "foo" do
|
||||||
|
url "https://github.com/",
|
||||||
|
verified: "github.com/"
|
||||||
|
end
|
||||||
|
CASK
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples "does not report any offenses"
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the url ends with a / and the verified value does not" do
|
||||||
|
let(:source) do
|
||||||
|
<<~CASK
|
||||||
|
cask "foo" do
|
||||||
|
url "https://github.com/",
|
||||||
|
verified: "github.com"
|
||||||
|
end
|
||||||
|
CASK
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:expected_offenses) do
|
||||||
|
[{
|
||||||
|
message: "Verified URL parameter value should end with a /.",
|
||||||
|
severity: :convention,
|
||||||
|
line: 3,
|
||||||
|
column: 16,
|
||||||
|
source: "\"github.com\"",
|
||||||
|
}]
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:correct_source) do
|
||||||
|
<<~CASK
|
||||||
|
cask "foo" do
|
||||||
|
url "https://github.com/",
|
||||||
|
verified: "github.com/"
|
||||||
end
|
end
|
||||||
CASK
|
CASK
|
||||||
end
|
end
|
||||||
@ -59,8 +166,8 @@ describe RuboCop::Cop::Cask::Url do
|
|||||||
let(:source) do
|
let(:source) do
|
||||||
<<~CASK
|
<<~CASK
|
||||||
cask "foo" do
|
cask "foo" do
|
||||||
url "https://example.com/download/foo-v1.2.0.dmg",
|
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
|
||||||
verified: "example.com/download/"
|
verified: "github.com/Foo/foo/"
|
||||||
end
|
end
|
||||||
CASK
|
CASK
|
||||||
end
|
end
|
||||||
@ -72,8 +179,8 @@ describe RuboCop::Cop::Cask::Url do
|
|||||||
let(:source) do
|
let(:source) do
|
||||||
<<~CASK
|
<<~CASK
|
||||||
cask "foo" do
|
cask "foo" do
|
||||||
url "https://example.com/download/foo-v1.2.0.dmg",
|
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
|
||||||
verified: "example.com/download"
|
verified: "github.com/Foo/foo"
|
||||||
end
|
end
|
||||||
CASK
|
CASK
|
||||||
end
|
end
|
||||||
@ -84,15 +191,15 @@ describe RuboCop::Cop::Cask::Url do
|
|||||||
severity: :convention,
|
severity: :convention,
|
||||||
line: 3,
|
line: 3,
|
||||||
column: 16,
|
column: 16,
|
||||||
source: "\"example.com/download\"",
|
source: "\"github.com/Foo/foo\"",
|
||||||
}]
|
}]
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:correct_source) do
|
let(:correct_source) do
|
||||||
<<~CASK
|
<<~CASK
|
||||||
cask "foo" do
|
cask "foo" do
|
||||||
url "https://example.com/download/foo-v1.2.0.dmg",
|
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
|
||||||
verified: "example.com/download/"
|
verified: "github.com/Foo/foo/"
|
||||||
end
|
end
|
||||||
CASK
|
CASK
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user