From bbd73a724358802c1a4f6c3a0612bf634af13bcf Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Tue, 28 May 2024 11:03:33 +0200 Subject: [PATCH] feat: add shared checks for casks and formulae --- Library/.rubocop.yml | 4 +- ...iling_slash.rb => homepage_url_styling.rb} | 13 ++- .../cask/mixin/on_homepage_stanza.rbi | 2 +- Library/Homebrew/rubocops/homepage.rb | 81 +--------------- Library/Homebrew/rubocops/rubocop-cask.rb | 2 +- .../rubocops/shared/homepage_helper.rb | 95 +++++++++++++++++++ ...h_spec.rb => homepage_url_styling_spec.rb} | 2 +- 7 files changed, 113 insertions(+), 86 deletions(-) rename Library/Homebrew/rubocops/cask/{homepage_url_trailing_slash.rb => homepage_url_styling.rb} (75%) create mode 100644 Library/Homebrew/rubocops/shared/homepage_helper.rb rename Library/Homebrew/test/rubocops/cask/{homepage_url_trailing_slash_spec.rb => homepage_url_styling_spec.rb} (91%) diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml index ccc116378b6b4..43339e2660eff 100644 --- a/Library/.rubocop.yml +++ b/Library/.rubocop.yml @@ -29,8 +29,8 @@ Cask/Desc: Description: "Ensure that the desc stanza conforms to various content and style checks." Enabled: true -Cask/HomepageUrlTrailingSlash: - Description: "Ensure that the homepage url has a slash after the domain name." +Cask/HomepageUrlStyling: + Description: "Ensure that the homepage url has the correct format and styling." Enabled: true Cask/StanzaGrouping: diff --git a/Library/Homebrew/rubocops/cask/homepage_url_trailing_slash.rb b/Library/Homebrew/rubocops/cask/homepage_url_styling.rb similarity index 75% rename from Library/Homebrew/rubocops/cask/homepage_url_trailing_slash.rb rename to Library/Homebrew/rubocops/cask/homepage_url_styling.rb index cab96ee824d72..46a853eeb61a2 100644 --- a/Library/Homebrew/rubocops/cask/homepage_url_trailing_slash.rb +++ b/Library/Homebrew/rubocops/cask/homepage_url_styling.rb @@ -3,21 +3,24 @@ require "forwardable" require "uri" +require "rubocops/shared/homepage_helper" module RuboCop module Cop module Cask - # This cop checks that a cask's homepage ends with a slash - # if it does not have a path component. - class HomepageUrlTrailingSlash < Base + # This cop audits the `homepage` URL in casks. + class HomepageUrlStyling < Base include OnHomepageStanza include HelperFunctions + include HomepageHelper extend AutoCorrector MSG_NO_SLASH = "'%s' must have a slash after the domain." def on_homepage_stanza(stanza) - url_node = stanza.stanza_node.first_argument + @name = cask_block.header.cask_token + desc_call = stanza.stanza_node + url_node = desc_call.first_argument url = if url_node.dstr_type? # Remove quotes from interpolated string. @@ -26,6 +29,8 @@ def on_homepage_stanza(stanza) url_node.str_content end + audit_homepage(:cask, url, desc_call, url_node) + return unless url&.match?(%r{^.+://[^/]+$}) domain = URI(string_content(url_node, strip_dynamic: true)).host diff --git a/Library/Homebrew/rubocops/cask/mixin/on_homepage_stanza.rbi b/Library/Homebrew/rubocops/cask/mixin/on_homepage_stanza.rbi index b4aabf1446c1a..16240f863d35f 100644 --- a/Library/Homebrew/rubocops/cask/mixin/on_homepage_stanza.rbi +++ b/Library/Homebrew/rubocops/cask/mixin/on_homepage_stanza.rbi @@ -1,5 +1,5 @@ # typed: strict module RuboCop::Cop::Cask::OnHomepageStanza - requires_ancestor { RuboCop::Cop::Cask::HomepageUrlTrailingSlash } + requires_ancestor { RuboCop::Cop::Cask::HomepageUrlStyling } end diff --git a/Library/Homebrew/rubocops/homepage.rb b/Library/Homebrew/rubocops/homepage.rb index c29ca238830ac..4ceab117634d5 100644 --- a/Library/Homebrew/rubocops/homepage.rb +++ b/Library/Homebrew/rubocops/homepage.rb @@ -2,12 +2,14 @@ # frozen_string_literal: true require "rubocops/extend/formula_cop" +require "rubocops/shared/homepage_helper" module RuboCop module Cop module FormulaAudit # This cop audits the `homepage` URL in formulae. class Homepage < FormulaCop + include HomepageHelper extend AutoCorrector def audit_formula(_node, class_node, _parent_class_node, body_node) @@ -21,84 +23,9 @@ def audit_formula(_node, class_node, _parent_class_node, body_node) homepage_parameter_node = parameters(homepage_node).first offending_node(homepage_parameter_node) - homepage = string_content(homepage_parameter_node) + content = string_content(homepage_parameter_node) - problem "Formula should have a homepage." if homepage.empty? - - problem "The homepage should start with http or https." unless homepage.match?(%r{^https?://}) - - case homepage - # Freedesktop is complicated to handle - It has SSL/TLS, but only on certain subdomains. - # To enable https Freedesktop change the URL from http://project.freedesktop.org/wiki to - # https://wiki.freedesktop.org/project_name. - # "Software" is redirected to https://wiki.freedesktop.org/www/Software/project_name - when %r{^http://((?:www|nice|libopenraw|liboil|telepathy|xorg)\.)?freedesktop\.org/(?:wiki/)?} - if homepage.include?("Software") - problem "Freedesktop homepages should be styled " \ - "`https://wiki.freedesktop.org/www/Software/project_name`" - else - problem "Freedesktop homepages should be styled `https://wiki.freedesktop.org/project_name`" - end - - # Google Code homepages should end in a slash - when %r{^https?://code\.google\.com/p/[^/]+[^/]$} - problem "Google Code homepages should end with a slash" do |corrector| - corrector.replace(homepage_parameter_node.source_range, "\"#{homepage}/\"") - end - - when %r{^http://([^/]*)\.(sf|sourceforge)\.net(/|$)} - fixed = "https://#{Regexp.last_match(1)}.sourceforge.io/" - problem "Sourceforge homepages should be `#{fixed}`" do |corrector| - corrector.replace(homepage_parameter_node.source_range, "\"#{fixed}\"") - end - - when /readthedocs\.org/ - fixed = homepage.sub("readthedocs.org", "readthedocs.io") - problem "Readthedocs homepages should be `#{fixed}`" do |corrector| - corrector.replace(homepage_parameter_node.source_range, "\"#{fixed}\"") - end - - when %r{^https://github.com.*\.git$} - problem "GitHub homepages should not end with .git" do |corrector| - corrector.replace(homepage_parameter_node.source_range, "\"#{homepage.delete_suffix(".git")}\"") - end - - # People will run into mixed content sometimes, but we should enforce and then add - # exemptions as they are discovered. Treat mixed content on homepages as a bug. - # Justify each exemptions with a code comment so we can keep track here. - # - # Compact the above into this list as we're able to remove detailed notations, etc over time. - when - # Check for `http://` GitHub homepage URLs, `https://` is preferred. - # NOTE: Only check homepages that are repository pages, not `*.github.com` hosts. - %r{^http://github\.com/}, - %r{^http://[^/]*\.github\.io/}, - - # Savannah has full SSL/TLS support but no auto-redirect. - # Doesn't apply to the download URLs, only the homepage. - %r{^http://savannah\.nongnu\.org/}, - - %r{^http://[^/]*\.sourceforge\.io/}, - # There's an auto-redirect here, but this mistake is incredibly common too. - # Only applies to the homepage and subdomains for now, not the FTP URLs. - %r{^http://((?:build|cloud|developer|download|extensions|git| - glade|help|library|live|nagios|news|people| - projects|rt|static|wiki|www)\.)?gnome\.org}x, - %r{^http://[^/]*\.apache\.org}, - %r{^http://packages\.debian\.org}, - %r{^http://wiki\.freedesktop\.org/}, - %r{^http://((?:www)\.)?gnupg\.org/}, - %r{^http://ietf\.org}, - %r{^http://[^/.]+\.ietf\.org}, - %r{^http://[^/.]+\.tools\.ietf\.org}, - %r{^http://www\.gnu\.org/}, - %r{^http://code\.google\.com/}, - %r{^http://bitbucket\.org/}, - %r{^http://(?:[^/]*\.)?archive\.org} - problem "Please use https:// for #{homepage}" do |corrector| - corrector.replace(homepage_parameter_node.source_range, "\"#{homepage.sub("http", "https")}\"") - end - end + audit_homepage(:formula, content, homepage_node, homepage_parameter_node) end end end diff --git a/Library/Homebrew/rubocops/rubocop-cask.rb b/Library/Homebrew/rubocops/rubocop-cask.rb index 44abad52f94b0..668fcea57d18f 100644 --- a/Library/Homebrew/rubocops/rubocop-cask.rb +++ b/Library/Homebrew/rubocops/rubocop-cask.rb @@ -15,7 +15,7 @@ require_relative "cask/array_alphabetization" require_relative "cask/desc" require_relative "cask/discontinued" -require_relative "cask/homepage_url_trailing_slash" +require_relative "cask/homepage_url_styling" require_relative "cask/no_overrides" require_relative "cask/on_system_conditionals" require_relative "cask/shared_filelist_glob" diff --git a/Library/Homebrew/rubocops/shared/homepage_helper.rb b/Library/Homebrew/rubocops/shared/homepage_helper.rb new file mode 100644 index 0000000000000..921d9aebe65fc --- /dev/null +++ b/Library/Homebrew/rubocops/shared/homepage_helper.rb @@ -0,0 +1,95 @@ +# typed: true +# frozen_string_literal: true + +require "rubocops/shared/helper_functions" + +module RuboCop + module Cop + # This module performs common checks the `homepage` field in both formulae and casks. + module HomepageHelper + include HelperFunctions + + def audit_homepage(type, content, homepage_node, homepage_parameter_node) + @offensive_node = homepage_node + + problem "#{type.to_s.capitalize} should have a homepage." if content.empty? + + @offensive_node = homepage_parameter_node + problem "The homepage should start with http or https." unless content.match?(%r{^https?://}) + + case content + # Freedesktop is complicated to handle - It has SSL/TLS, but only on certain subdomains. + # To enable https Freedesktop change the URL from http://project.freedesktop.org/wiki to + # https://wiki.freedesktop.org/project_name. + # "Software" is redirected to https://wiki.freedesktop.org/www/Software/project_name + when %r{^http://((?:www|nice|libopenraw|liboil|telepathy|xorg)\.)?freedesktop\.org/(?:wiki/)?} + if content.include?("Software") + problem "Freedesktop homepages should be styled " \ + "`https://wiki.freedesktop.org/www/Software/project_name`" + else + problem "Freedesktop homepages should be styled `https://wiki.freedesktop.org/project_name`" + end + + # Google Code homepages should end in a slash + when %r{^https?://code\.google\.com/p/[^/]+[^/]$} + problem "Google Code homepages should end with a slash" do |corrector| + corrector.replace(homepage_parameter_node.source_range, "\"#{content}/\"") + end + + when %r{^http://([^/]*)\.(sf|sourceforge)\.net(/|$)} + fixed = "https://#{Regexp.last_match(1)}.sourceforge.io/" + problem "Sourceforge homepages should be `#{fixed}`" do |corrector| + corrector.replace(homepage_parameter_node.source_range, "\"#{fixed}\"") + end + + when /readthedocs\.org/ + fixed = content.sub("readthedocs.org", "readthedocs.io") + problem "Readthedocs homepages should be `#{fixed}`" do |corrector| + corrector.replace(homepage_parameter_node.source_range, "\"#{fixed}\"") + end + + when %r{^https://github.com.*\.git$} + problem "GitHub homepages should not end with .git" do |corrector| + corrector.replace(homepage_parameter_node.source_range, "\"#{content.delete_suffix(".git")}\"") + end + + # People will run into mixed content sometimes, but we should enforce and then add + # exemptions as they are discovered. Treat mixed content on homepages as a bug. + # Justify each exemptions with a code comment so we can keep track here. + # + # Compact the above into this list as we're able to remove detailed notations, etc over time. + when + # Check for `http://` GitHub homepage URLs, `https://` is preferred. + # NOTE: Only check homepages that are repository pages, not `*.github.com` hosts. + %r{^http://github\.com/}, + %r{^http://[^/]*\.github\.io/}, + + # Savannah has full SSL/TLS support but no auto-redirect. + # Doesn't apply to the download URLs, only the homepage. + %r{^http://savannah\.nongnu\.org/}, + + %r{^http://[^/]*\.sourceforge\.io/}, + # There's an auto-redirect here, but this mistake is incredibly common too. + # Only applies to the homepage and subdomains for now, not the FTP URLs. + %r{^http://((?:build|cloud|developer|download|extensions|git| + glade|help|library|live|nagios|news|people| + projects|rt|static|wiki|www)\.)?gnome\.org}x, + %r{^http://[^/]*\.apache\.org}, + %r{^http://packages\.debian\.org}, + %r{^http://wiki\.freedesktop\.org/}, + %r{^http://((?:www)\.)?gnupg\.org/}, + %r{^http://ietf\.org}, + %r{^http://[^/.]+\.ietf\.org}, + %r{^http://[^/.]+\.tools\.ietf\.org}, + %r{^http://www\.gnu\.org/}, + %r{^http://code\.google\.com/}, + %r{^http://bitbucket\.org/}, + %r{^http://(?:[^/]*\.)?archive\.org} + problem "Please use https:// for #{content}" do |corrector| + corrector.replace(homepage_parameter_node.source_range, "\"#{content.sub("http", "https")}\"") + end + end + end + end + end +end diff --git a/Library/Homebrew/test/rubocops/cask/homepage_url_trailing_slash_spec.rb b/Library/Homebrew/test/rubocops/cask/homepage_url_styling_spec.rb similarity index 91% rename from Library/Homebrew/test/rubocops/cask/homepage_url_trailing_slash_spec.rb rename to Library/Homebrew/test/rubocops/cask/homepage_url_styling_spec.rb index a89971b68a4c5..28b4978856326 100644 --- a/Library/Homebrew/test/rubocops/cask/homepage_url_trailing_slash_spec.rb +++ b/Library/Homebrew/test/rubocops/cask/homepage_url_styling_spec.rb @@ -2,7 +2,7 @@ require "rubocops/rubocop-cask" -RSpec.describe RuboCop::Cop::Cask::HomepageUrlTrailingSlash, :config do +RSpec.describe RuboCop::Cop::Cask::HomepageUrlStyling, :config do it "accepts a homepage URL ending with a slash" do expect_no_offenses <<~CASK cask 'foo' do