Skip to content

Commit 7041f7e

Browse files
authored
Merge pull request #2911 from GauthamGoli/audit_urls_rubocop_part_1
audit: Port audit_urls partially to rubocop and add corresponding tests
2 parents 0b0ab9b + b7ddd27 commit 7041f7e

File tree

4 files changed

+145
-49
lines changed

4 files changed

+145
-49
lines changed

Library/Homebrew/dev-cmd/audit.rb

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,56 +1282,7 @@ def audit_download_strategy
12821282
end
12831283

12841284
def audit_urls
1285-
# Check GNU urls; doesn't apply to mirrors
1286-
if url =~ %r{^(?:https?|ftp)://ftpmirror.gnu.org/(.*)}
1287-
problem "Please use \"https://ftp.gnu.org/gnu/#{Regexp.last_match(1)}\" instead of #{url}."
1288-
end
1289-
1290-
# Fossies upstream requests they aren't used as primary URLs
1291-
# https://github.com/Homebrew/homebrew-core/issues/14486#issuecomment-307753234
1292-
if url =~ %r{^https?://fossies\.org/}
1293-
problem "Please don't use fossies.org in the url (using as a mirror is fine)"
1294-
end
1295-
1296-
if mirrors.include?(url)
1297-
problem "URL should not be duplicated as a mirror: #{url}"
1298-
end
1299-
13001285
urls = [url] + mirrors
1301-
1302-
# Check a variety of SSL/TLS URLs that don't consistently auto-redirect
1303-
# or are overly common errors that need to be reduced & fixed over time.
1304-
urls.each do |p|
1305-
case p
1306-
when %r{^http://ftp\.gnu\.org/},
1307-
%r{^http://ftpmirror\.gnu\.org/},
1308-
%r{^http://download\.savannah\.gnu\.org/},
1309-
%r{^http://download-mirror\.savannah\.gnu\.org/},
1310-
%r{^http://[^/]*\.apache\.org/},
1311-
%r{^http://code\.google\.com/},
1312-
%r{^http://fossies\.org/},
1313-
%r{^http://mirrors\.kernel\.org/},
1314-
%r{^http://(?:[^/]*\.)?bintray\.com/},
1315-
%r{^http://tools\.ietf\.org/},
1316-
%r{^http://launchpad\.net/},
1317-
%r{^http://github\.com/},
1318-
%r{^http://bitbucket\.org/},
1319-
%r{^http://anonscm\.debian\.org/},
1320-
%r{^http://cpan\.metacpan\.org/},
1321-
%r{^http://hackage\.haskell\.org/},
1322-
%r{^http://(?:[^/]*\.)?archive\.org},
1323-
%r{^http://(?:[^/]*\.)?freedesktop\.org},
1324-
%r{^http://(?:[^/]*\.)?mirrorservice\.org/}
1325-
problem "Please use https:// for #{p}"
1326-
when %r{^http://search\.mcpan\.org/CPAN/(.*)}i
1327-
problem "#{p} should be `https://cpan.metacpan.org/#{Regexp.last_match(1)}`"
1328-
when %r{^(http|ftp)://ftp\.gnome\.org/pub/gnome/(.*)}i
1329-
problem "#{p} should be `https://download.gnome.org/#{Regexp.last_match(2)}`"
1330-
when %r{^git://anonscm\.debian\.org/users/(.*)}i
1331-
problem "#{p} should be `https://anonscm.debian.org/git/users/#{Regexp.last_match(1)}`"
1332-
end
1333-
end
1334-
13351286
# Prefer HTTP/S when possible over FTP protocol due to possible firewalls.
13361287
urls.each do |p|
13371288
case p

Library/Homebrew/rubocops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
require_relative "./rubocops/legacy_patches_cop"
1010
require_relative "./rubocops/conflicts_cop"
1111
require_relative "./rubocops/options_cop"
12+
require_relative "./rubocops/urls_cop"
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require_relative "./extend/formula_cop"
2+
3+
module RuboCop
4+
module Cop
5+
module FormulaAudit
6+
# This cop audits urls and mirrors in Formulae
7+
class Urls < FormulaCop
8+
def audit_formula(_node, _class_node, _parent_class_node, body_node)
9+
urls = find_every_method_call_by_name(body_node, :url)
10+
mirrors = find_every_method_call_by_name(body_node, :mirror)
11+
12+
# GNU urls; doesn't apply to mirrors
13+
gnu_pattern = %r{^(?:https?|ftp)://ftpmirror.gnu.org/(.*)}
14+
audit_urls(urls, gnu_pattern) do |match, url|
15+
problem "Please use \"https://ftp.gnu.org/gnu/#{match[1]}\" instead of #{url}."
16+
end
17+
18+
# Fossies upstream requests they aren't used as primary URLs
19+
# https://github.com/Homebrew/homebrew-core/issues/14486#issuecomment-307753234
20+
fossies_pattern = %r{^https?://fossies\.org/}
21+
audit_urls(urls, fossies_pattern) do
22+
problem "Please don't use fossies.org in the url (using as a mirror is fine)"
23+
end
24+
25+
audit_urls(mirrors, /.*/) do |_, mirror|
26+
urls.each do |url|
27+
url_string = string_content(parameters(url).first)
28+
next unless url_string.eql?(mirror)
29+
problem "URL should not be duplicated as a mirror: #{url_string}"
30+
end
31+
end
32+
33+
urls += mirrors
34+
35+
# Check a variety of SSL/TLS URLs that don't consistently auto-redirect
36+
# or are overly common errors that need to be reduced & fixed over time.
37+
http_to_https_patterns = Regexp.union([%r{^http://ftp\.gnu\.org/},
38+
%r{^http://ftpmirror\.gnu\.org/},
39+
%r{^http://download\.savannah\.gnu\.org/},
40+
%r{^http://download-mirror\.savannah\.gnu\.org/},
41+
%r{^http://[^/]*\.apache\.org/},
42+
%r{^http://code\.google\.com/},
43+
%r{^http://fossies\.org/},
44+
%r{^http://mirrors\.kernel\.org/},
45+
%r{^http://(?:[^/]*\.)?bintray\.com/},
46+
%r{^http://tools\.ietf\.org/},
47+
%r{^http://launchpad\.net/},
48+
%r{^http://github\.com/},
49+
%r{^http://bitbucket\.org/},
50+
%r{^http://anonscm\.debian\.org/},
51+
%r{^http://cpan\.metacpan\.org/},
52+
%r{^http://hackage\.haskell\.org/},
53+
%r{^http://(?:[^/]*\.)?archive\.org},
54+
%r{^http://(?:[^/]*\.)?freedesktop\.org},
55+
%r{^http://(?:[^/]*\.)?mirrorservice\.org/}])
56+
audit_urls(urls, http_to_https_patterns) do |_, url|
57+
problem "Please use https:// for #{url}"
58+
end
59+
60+
cpan_pattern = %r{^http://search\.mcpan\.org/CPAN/(.*)}i
61+
audit_urls(urls, cpan_pattern) do |match, url|
62+
problem "#{url} should be `https://cpan.metacpan.org/#{match[1]}`"
63+
end
64+
65+
gnome_pattern = %r{^(http|ftp)://ftp\.gnome\.org/pub/gnome/(.*)}i
66+
audit_urls(urls, gnome_pattern) do |match, url|
67+
problem "#{url} should be `https://download.gnome.org/#{match[2]}`"
68+
end
69+
70+
debian_pattern = %r{^git://anonscm\.debian\.org/users/(.*)}i
71+
audit_urls(urls, debian_pattern) do |match, url|
72+
problem "#{url} should be `https://anonscm.debian.org/git/users/#{match[1]}`"
73+
end
74+
end
75+
76+
private
77+
78+
def audit_urls(urls, regex)
79+
urls.each do |url_node|
80+
url_string_node = parameters(url_node).first
81+
url_string = string_content(url_string_node)
82+
match_object = regex_match_group(url_string_node, regex)
83+
offending_node(url_string_node.parent) if match_object
84+
yield match_object, url_string if match_object
85+
end
86+
end
87+
end
88+
end
89+
end
90+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require "rubocop"
2+
require "rubocop/rspec/support"
3+
require_relative "../../extend/string"
4+
require_relative "../../rubocops/urls_cop"
5+
6+
describe RuboCop::Cop::FormulaAudit::Urls do
7+
subject(:cop) { described_class.new }
8+
9+
context "When auditing urls" do
10+
it "with offenses" do
11+
formulas = [{
12+
"url" => "https://ftpmirror.gnu.org/lightning/lightning-2.1.0.tar.gz",
13+
"msg" => 'Please use "https://ftp.gnu.org/gnu/lightning/lightning-2.1.0.tar.gz" instead of https://ftpmirror.gnu.org/lightning/lightning-2.1.0.tar.gz.',
14+
"col" => 2,
15+
}, {
16+
"url" => "https://fossies.org/linux/privat/monit-5.23.0.tar.gz",
17+
"msg" => "Please don't use fossies.org in the url (using as a mirror is fine)",
18+
"col" => 2,
19+
}, {
20+
"url" => "http://tools.ietf.org/tools/rfcmarkup/rfcmarkup-1.119.tgz",
21+
"msg" => "Please use https:// for http://tools.ietf.org/tools/rfcmarkup/rfcmarkup-1.119.tgz",
22+
"col" => 2,
23+
}, {
24+
"url" => "http://search.mcpan.org/CPAN/authors/id/Z/ZE/ZEFRAM/Perl4-CoreLibs-0.003.tar.gz",
25+
"msg" => "http://search.mcpan.org/CPAN/authors/id/Z/ZE/ZEFRAM/Perl4-CoreLibs-0.003.tar.gz should be `https://cpan.metacpan.org/authors/id/Z/ZE/ZEFRAM/Perl4-CoreLibs-0.003.tar.gz`",
26+
"col" => 2,
27+
}, {
28+
"url" => "http://ftp.gnome.org/pub/GNOME/binaries/mac/banshee/banshee-2.macosx.intel.dmg",
29+
"msg" => "http://ftp.gnome.org/pub/GNOME/binaries/mac/banshee/banshee-2.macosx.intel.dmg should be `https://download.gnome.org/binaries/mac/banshee/banshee-2.macosx.intel.dmg`",
30+
"col" => 2,
31+
}]
32+
33+
formulas.each do |formula|
34+
source = <<-EOS.undent
35+
class Foo < Formula
36+
desc "foo"
37+
url "#{formula["url"]}"
38+
end
39+
EOS
40+
expected_offenses = [{ message: formula["msg"],
41+
severity: :convention,
42+
line: 3,
43+
column: formula["col"],
44+
source: source }]
45+
46+
inspect_source(cop, source)
47+
48+
expected_offenses.zip(cop.offenses.reverse).each do |expected, actual|
49+
expect_offense(expected, actual)
50+
end
51+
end
52+
end
53+
end
54+
end

0 commit comments

Comments
 (0)