From 21da0743464e1d81f0768b266b888cda68fbe8c7 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Sun, 2 Apr 2023 19:39:23 +0100 Subject: [PATCH] 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. ``` --- Library/Homebrew/rubocops/cask/url.rb | 5 +- .../Homebrew/test/rubocops/cask/url_spec.rb | 129 ++++++++++++++++-- 2 files changed, 122 insertions(+), 12 deletions(-) diff --git a/Library/Homebrew/rubocops/cask/url.rb b/Library/Homebrew/rubocops/cask/url.rb index 49b850edfb..a82546610e 100644 --- a/Library/Homebrew/rubocops/cask/url.rb +++ b/Library/Homebrew/rubocops/cask/url.rb @@ -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( diff --git a/Library/Homebrew/test/rubocops/cask/url_spec.rb b/Library/Homebrew/test/rubocops/cask/url_spec.rb index daef684d95..818e11f4f9 100644 --- a/Library/Homebrew/test/rubocops/cask/url_spec.rb +++ b/Library/Homebrew/test/rubocops/cask/url_spec.rb @@ -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