From d5448ff54af6b0d366c4b5fb93472015bc48c417 Mon Sep 17 00:00:00 2001 From: Bevan Kay Date: Fri, 31 Dec 2021 11:08:47 +1100 Subject: [PATCH 1/7] rubocops (cask/url): add rubocop to use csv instead of before_comma and after_comma in cask --- .../rubocops/cask/mixin/on_url_stanza.rb | 29 +++++++++++++++ .../cask/url_legacy_comma_separators.rb | 35 +++++++++++++++++++ Library/Homebrew/rubocops/rubocop-cask.rb | 2 ++ .../cask/Casks/hockeyapp-with-appcast.rb | 2 +- .../cask/Casks/hockeyapp-without-appcast.rb | 2 +- 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 Library/Homebrew/rubocops/cask/mixin/on_url_stanza.rb create mode 100644 Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb diff --git a/Library/Homebrew/rubocops/cask/mixin/on_url_stanza.rb b/Library/Homebrew/rubocops/cask/mixin/on_url_stanza.rb new file mode 100644 index 0000000000..ad26e67a11 --- /dev/null +++ b/Library/Homebrew/rubocops/cask/mixin/on_url_stanza.rb @@ -0,0 +1,29 @@ +# typed: false +# frozen_string_literal: true + +module RuboCop + module Cop + module Cask + # Common functionality for checking url stanzas. + module OnUrlStanza + extend Forwardable + include CaskHelp + + def on_cask(cask_block) + @cask_block = cask_block + + toplevel_stanzas.select(&:url?).each do |stanza| + on_url_stanza(stanza) + end + end + + private + + attr_reader :cask_block + + def_delegators :cask_block, + :toplevel_stanzas + end + end + end +end diff --git a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb new file mode 100644 index 0000000000..257251bdda --- /dev/null +++ b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb @@ -0,0 +1,35 @@ +# typed: true +# frozen_string_literal: true + +require "forwardable" +require "uri" + +module RuboCop + module Cop + module Cask + # This cop checks for version.before_comma and version.after_comma + class UrlLegacyCommaSeparators < Base + include OnUrlStanza + extend AutoCorrector + + MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma' and 'version.csv.second' instead of 'version.after_comma'." + + def on_url_stanza(stanza) + url_node = stanza.stanza_node.first_argument + + legacy_comma_separator_pattern = /version.(before|after)_comma/ + + url = url_node.source + + return unless url.match?(legacy_comma_separator_pattern) + + corrected_url = url.sub("before_comma", "csv.first")&.sub("after_comma", "csv.second") + + add_offense(url_node.loc.expression, message: format(MSG_CSV, url: url)) do |corrector| + corrector.replace(url_node.source_range, corrected_url) + end + end + end + end + end +end diff --git a/Library/Homebrew/rubocops/rubocop-cask.rb b/Library/Homebrew/rubocops/rubocop-cask.rb index 4dac4d062f..63b02b0e19 100644 --- a/Library/Homebrew/rubocops/rubocop-cask.rb +++ b/Library/Homebrew/rubocops/rubocop-cask.rb @@ -12,8 +12,10 @@ require_relative "cask/extend/string" require_relative "cask/extend/node" require_relative "cask/mixin/cask_help" require_relative "cask/mixin/on_homepage_stanza" +require_relative "cask/mixin/on_url_stanza" require_relative "cask/desc" require_relative "cask/homepage_url_trailing_slash" require_relative "cask/no_dsl_version" require_relative "cask/stanza_order" require_relative "cask/stanza_grouping" +require_relative "cask/url_legacy_comma_separators" diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/hockeyapp-with-appcast.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/hockeyapp-with-appcast.rb index 48385f356a..05a6aee83a 100644 --- a/Library/Homebrew/test/support/fixtures/cask/Casks/hockeyapp-with-appcast.rb +++ b/Library/Homebrew/test/support/fixtures/cask/Casks/hockeyapp-with-appcast.rb @@ -3,7 +3,7 @@ cask "hockeyapp-with-appcast" do sha256 "a69e7357bea014f4c14ac9699274f559086844ffa46563c4619bf1addfd72ad9" # rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1 was verified as official when first introduced to the cask - url "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1/app_versions/#{version.after_comma}?format=zip" + url "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1/app_versions/#{version.csv.second}?format=zip" appcast "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1" name "HockeyApp" homepage "https://www.brew.sh/" diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/hockeyapp-without-appcast.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/hockeyapp-without-appcast.rb index c4000dac81..179d530fd5 100644 --- a/Library/Homebrew/test/support/fixtures/cask/Casks/hockeyapp-without-appcast.rb +++ b/Library/Homebrew/test/support/fixtures/cask/Casks/hockeyapp-without-appcast.rb @@ -3,7 +3,7 @@ cask "hockeyapp-without-appcast" do sha256 "a69e7357bea014f4c14ac9699274f559086844ffa46563c4619bf1addfd72ad9" # rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1 was verified as official when first introduced to the cask - url "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1/app_versions/#{version.after_comma}?format=zip" + url "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1/app_versions/#{version.csv.second}?format=zip" name "HockeyApp" homepage "https://www.brew.sh/" From cc2b99c8cfa2e08d44505674e25f0360378bbc7c Mon Sep 17 00:00:00 2001 From: Bevan Kay Date: Mon, 10 Jan 2022 21:27:20 +1100 Subject: [PATCH 2/7] Add check for url :block --- Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb index 257251bdda..66aff943e2 100644 --- a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb +++ b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb @@ -14,7 +14,9 @@ module RuboCop MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma' and 'version.csv.second' instead of 'version.after_comma'." - def on_url_stanza(stanza) + def on_url_stanza(stanza) + return if stanza.stanza_node.type == :block + url_node = stanza.stanza_node.first_argument legacy_comma_separator_pattern = /version.(before|after)_comma/ From 22d68fb99b02b1b41afd3f7e109d0cb6af099005 Mon Sep 17 00:00:00 2001 From: Bevan Kay Date: Mon, 10 Jan 2022 21:34:51 +1100 Subject: [PATCH 3/7] fix style offences --- .../Homebrew/rubocops/cask/url_legacy_comma_separators.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb index 66aff943e2..b22dbc59b1 100644 --- a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb +++ b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb @@ -12,11 +12,12 @@ module RuboCop include OnUrlStanza extend AutoCorrector - MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma' and 'version.csv.second' instead of 'version.after_comma'." + MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma'"\ + "and 'version.csv.second' instead of 'version.after_comma'." - def on_url_stanza(stanza) + def on_url_stanza(stanza) return if stanza.stanza_node.type == :block - + url_node = stanza.stanza_node.first_argument legacy_comma_separator_pattern = /version.(before|after)_comma/ From 7e24930e4e5161a8deb6aadbc3cbde71c27224e0 Mon Sep 17 00:00:00 2001 From: Bevan Kay Date: Tue, 11 Jan 2022 01:22:22 +1100 Subject: [PATCH 4/7] Update Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb Co-authored-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> --- Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb index b22dbc59b1..5425999f20 100644 --- a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb +++ b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb @@ -12,7 +12,7 @@ module RuboCop include OnUrlStanza extend AutoCorrector - MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma'"\ + MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma' " \ "and 'version.csv.second' instead of 'version.after_comma'." def on_url_stanza(stanza) From 6f62b0e0296b02dbeebe53ddb90e515b9683db86 Mon Sep 17 00:00:00 2001 From: Bevan Kay Date: Tue, 11 Jan 2022 13:18:06 +1100 Subject: [PATCH 5/7] add tests --- .../cask/url_legacy_comma_separators_spec.rb | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb diff --git a/Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb b/Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb new file mode 100644 index 0000000000..8a4606f1a5 --- /dev/null +++ b/Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb @@ -0,0 +1,103 @@ +# typed: false +# frozen_string_literal: true + +require "rubocops/rubocop-cask" +require "test/rubocops/cask/shared_examples/cask_cop" + +describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do + include CaskCop + + subject(:cop) { described_class.new } + + context "when url version interpolation does not include version.before_comma or version.after_comma" do + let(:source) do + <<-CASK.undent + cask 'foo' do + version '1.1' + url 'https://foo.brew.sh/foo-#{version}.dmg' + end + CASK + end + + include_examples "does not report any offenses" + end + + context "when the url uses csv" do + let(:source) do + <<-CASK.undent + cask 'foo' do + version '1.1,111' + url 'https://foo.brew.sh/foo-#{version.csv.first}.dmg' + end + CASK + end + + include_examples "does not report any offenses" + end + + context "when the url uses version.before_comma" do + let(:source) do + <<-CASK.undent + cask 'foo' do + version '1.1,111' + url 'https://foo.brew.sh/foo-#{version.before_comma}.dmg' + end + CASK + end + let(:correct_source) do + <<-CASK.undent + cask 'foo' do + version '1.1,111' + url 'https://foo.brew.sh/foo-#{version.csv.first}.dmg' + end + CASK + end + let(:expected_offenses) do + [{ + message: "'https://foo.brew.sh/foo-#{version.before_comma}.dmg' should use "\ + "#{version.csv.first} instead of #{version.before_comma}.'", + severity: :convention, + line: 2, + column: 11, + source: "'https://foo.brew.sh/foo-#{version.before_comma}.dmg'", + }] + end + + include_examples "reports offenses" + + include_examples "autocorrects source" + end + + context "when the url uses version.after_comma" do + let(:source) do + <<-CASK.undent + cask 'foo' do + version '1.1,111' + url 'https://foo.brew.sh/foo-#{version.after_comma}.dmg' + end + CASK + end + let(:correct_source) do + <<-CASK.undent + cask 'foo' do + version '1.1,111' + url 'https://foo.brew.sh/foo-#{version.csv.second}.dmg' + end + CASK + end + let(:expected_offenses) do + [{ + message: "'https://foo.brew.sh/foo-#{version.after_comma}.dmg' should use "\ + "#{version.csv.second} instead of #{version.after_comma}.'", + severity: :convention, + line: 2, + column: 11, + source: "'https://foo.brew.sh/foo-#{version.after_comma}.dmg'", + }] + end + + include_examples "reports offenses" + + include_examples "autocorrects source" + end +end From d371547d5a04dbe0a6a496b1636add688797e46a Mon Sep 17 00:00:00 2001 From: Bevan Kay Date: Wed, 12 Jan 2022 10:39:38 +1100 Subject: [PATCH 6/7] fix tests --- .../cask/url_legacy_comma_separators.rb | 2 +- .../cask/url_legacy_comma_separators_spec.rb | 32 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb index 5425999f20..7dadc4bd07 100644 --- a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb +++ b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb @@ -13,7 +13,7 @@ module RuboCop extend AutoCorrector MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma' " \ - "and 'version.csv.second' instead of 'version.after_comma'." + "and 'version.csv.second' instead of 'version.after_comma'" def on_url_stanza(stanza) return if stanza.stanza_node.type == :block diff --git a/Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb b/Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb index 8a4606f1a5..9573f927a4 100644 --- a/Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb +++ b/Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb @@ -14,7 +14,7 @@ describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do <<-CASK.undent cask 'foo' do version '1.1' - url 'https://foo.brew.sh/foo-#{version}.dmg' + url 'https://foo.brew.sh/foo-\#{version}.dmg' end CASK end @@ -27,7 +27,7 @@ describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do <<-CASK.undent cask 'foo' do version '1.1,111' - url 'https://foo.brew.sh/foo-#{version.csv.first}.dmg' + url 'https://foo.brew.sh/foo-\#{version.csv.first}.dmg' end CASK end @@ -40,7 +40,7 @@ describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do <<-CASK.undent cask 'foo' do version '1.1,111' - url 'https://foo.brew.sh/foo-#{version.before_comma}.dmg' + url 'https://foo.brew.sh/foo-\#{version.before_comma}.dmg' end CASK end @@ -48,18 +48,18 @@ describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do <<-CASK.undent cask 'foo' do version '1.1,111' - url 'https://foo.brew.sh/foo-#{version.csv.first}.dmg' + url 'https://foo.brew.sh/foo-\#{version.csv.first}.dmg' end CASK end let(:expected_offenses) do [{ - message: "'https://foo.brew.sh/foo-#{version.before_comma}.dmg' should use "\ - "#{version.csv.first} instead of #{version.before_comma}.'", + message: "Use 'version.csv.first' instead of 'version.before_comma' "\ + "and 'version.csv.second' instead of 'version.after_comma'", severity: :convention, - line: 2, - column: 11, - source: "'https://foo.brew.sh/foo-#{version.before_comma}.dmg'", + line: 3, + column: 6, + source: "'https://foo.brew.sh/foo-\#{version.before_comma}.dmg'", }] end @@ -73,7 +73,7 @@ describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do <<-CASK.undent cask 'foo' do version '1.1,111' - url 'https://foo.brew.sh/foo-#{version.after_comma}.dmg' + url 'https://foo.brew.sh/foo-\#{version.after_comma}.dmg' end CASK end @@ -81,18 +81,18 @@ describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do <<-CASK.undent cask 'foo' do version '1.1,111' - url 'https://foo.brew.sh/foo-#{version.csv.second}.dmg' + url 'https://foo.brew.sh/foo-\#{version.csv.second}.dmg' end CASK end let(:expected_offenses) do [{ - message: "'https://foo.brew.sh/foo-#{version.after_comma}.dmg' should use "\ - "#{version.csv.second} instead of #{version.after_comma}.'", + message: "Use 'version.csv.first' instead of 'version.before_comma' "\ + "and 'version.csv.second' instead of 'version.after_comma'", severity: :convention, - line: 2, - column: 11, - source: "'https://foo.brew.sh/foo-#{version.after_comma}.dmg'", + line: 3, + column: 6, + source: "'https://foo.brew.sh/foo-\#{version.after_comma}.dmg'", }] end From 8c82ec964f12d1c96658d496ffc0f2fc9f778505 Mon Sep 17 00:00:00 2001 From: Bevan Kay Date: Wed, 12 Jan 2022 10:40:22 +1100 Subject: [PATCH 7/7] Update Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb Co-authored-by: Issy Long --- Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb index 7dadc4bd07..d164e625a2 100644 --- a/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb +++ b/Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb @@ -20,7 +20,7 @@ module RuboCop url_node = stanza.stanza_node.first_argument - legacy_comma_separator_pattern = /version.(before|after)_comma/ + legacy_comma_separator_pattern = /version\.(before|after)_comma/ url = url_node.source