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:
Issy Long 2023-04-02 19:39:23 +01:00
parent 17c0eaab25
commit 21da074346
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
2 changed files with 122 additions and 12 deletions

View File

@ -24,6 +24,7 @@ module RuboCop
def on_url_stanza(stanza)
return if stanza.stanza_node.block_type?
url_string = stanza.stanza_node.first_argument.str_content
hash_node = stanza.stanza_node.last_argument
return unless hash_node.hash_type?
@ -40,7 +41,9 @@ module RuboCop
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?("/")
add_offense(

View File

@ -14,7 +14,7 @@ describe RuboCop::Cop::Cask::Url do
<<~CASK
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com"
verified: "example.com/download/"
end
CASK
end
@ -27,7 +27,7 @@ describe RuboCop::Cop::Cask::Url do
<<~CASK
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "https://example.com"
verified: "https://example.com/download/"
end
CASK
end
@ -38,7 +38,7 @@ describe RuboCop::Cop::Cask::Url do
severity: :convention,
line: 3,
column: 16,
source: "\"https://example.com\"",
source: "\"https://example.com/download/\"",
}]
end
@ -46,7 +46,114 @@ describe RuboCop::Cop::Cask::Url do
<<~CASK
cask "foo" do
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
CASK
end
@ -59,8 +166,8 @@ describe RuboCop::Cop::Cask::Url do
let(:source) do
<<~CASK
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/"
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
verified: "github.com/Foo/foo/"
end
CASK
end
@ -72,8 +179,8 @@ describe RuboCop::Cop::Cask::Url do
let(:source) do
<<~CASK
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download"
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
verified: "github.com/Foo/foo"
end
CASK
end
@ -84,15 +191,15 @@ describe RuboCop::Cop::Cask::Url do
severity: :convention,
line: 3,
column: 16,
source: "\"example.com/download\"",
source: "\"github.com/Foo/foo\"",
}]
end
let(:correct_source) do
<<~CASK
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/"
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
verified: "github.com/Foo/foo/"
end
CASK
end