2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-26 19:41:14 +01:00
|
|
|
require "rubocops/patches"
|
2017-05-25 22:21:23 +05:30
|
|
|
|
2024-02-18 15:11:11 -08:00
|
|
|
RSpec.describe RuboCop::Cop::FormulaAudit::Patches do
|
2017-05-25 22:21:23 +05:30
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
2021-01-30 12:17:30 -05:00
|
|
|
def expect_offense_hash(message:, severity:, line:, column:, source:)
|
2024-03-07 16:20:20 +00:00
|
|
|
[{ message:, severity:, line:, column:, source: }]
|
2021-01-30 12:17:30 -05:00
|
|
|
end
|
|
|
|
|
2021-01-14 18:55:31 -08:00
|
|
|
context "when auditing legacy patches" do
|
|
|
|
it "reports no offenses if there is no legacy patch" do
|
2017-10-21 03:12:50 +02:00
|
|
|
expect_no_offenses(<<~RUBY)
|
2017-05-25 22:21:23 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
url 'https://brew.sh/foo-1.0.tgz'
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
2017-10-21 03:12:50 +02:00
|
|
|
RUBY
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
|
|
|
|
2021-01-12 16:54:53 +11:00
|
|
|
it "reports an offense if `def patches` is present" do
|
2017-10-21 03:12:50 +02:00
|
|
|
expect_offense(<<~RUBY)
|
2017-05-25 22:21:23 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
homepage "ftp://brew.sh/foo"
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
2017-05-25 22:21:23 +05:30
|
|
|
def patches
|
2023-04-07 17:16:48 +01:00
|
|
|
^^^^^^^^^^^ FormulaAudit/Patches: Use the patch DSL instead of defining a 'patches' method
|
2017-05-25 22:21:23 +05:30
|
|
|
DATA
|
|
|
|
end
|
|
|
|
end
|
2017-10-21 03:12:50 +02:00
|
|
|
RUBY
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
|
|
|
|
2021-01-12 16:54:53 +11:00
|
|
|
it "reports an offense for various patch URLs" do
|
2017-05-25 22:21:23 +05:30
|
|
|
patch_urls = [
|
|
|
|
"https://raw.github.com/mogaal/sendemail",
|
|
|
|
"https://mirrors.ustc.edu.cn/macports/trunk/",
|
|
|
|
"http://trac.macports.org/export/102865/trunk/dports/mail/uudeview/files/inews.c.patch",
|
|
|
|
"http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=5;filename=patch-libunac1.txt;att=1;bug=623340",
|
2017-08-01 20:59:17 +05:30
|
|
|
"https://patch-diff.githubusercontent.com/raw/foo/foo-bar/pull/100.patch",
|
2020-07-24 20:49:26 -04:00
|
|
|
"https://github.com/dlang/dub/commit/2c916b1a7999a050ac4970c3415ff8f91cd487aa.patch",
|
2025-05-01 22:40:20 -04:00
|
|
|
"https://bitbucket.org/multicoreware/x265_git/commits/b354c009a60bcd6d7fc04014e200a1ee9c45c167/raw",
|
2017-05-25 22:21:23 +05:30
|
|
|
]
|
|
|
|
patch_urls.each do |patch_url|
|
2017-10-15 02:28:32 +02:00
|
|
|
source = <<~EOS
|
2017-05-25 22:21:23 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
homepage "ftp://brew.sh/foo"
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
2017-05-25 22:21:23 +05:30
|
|
|
def patches
|
|
|
|
"#{patch_url}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
|
2020-07-07 13:12:37 +01:00
|
|
|
expected_offense = if patch_url.include?("/raw.github.com/")
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: GitHub/Gist patches should specify a revision: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2020-07-07 13:12:37 +01:00
|
|
|
elsif patch_url.include?("macports/trunk")
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: MacPorts patches should specify a revision instead of trunk: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2022-05-20 01:44:05 +01:00
|
|
|
elsif patch_url.start_with?("http://trac.macports.org/")
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: Patches from MacPorts Trac should be https://, not http: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2022-05-20 01:44:05 +01:00
|
|
|
elsif patch_url.start_with?("http://bugs.debian.org/")
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: Patches from Debian should be https://, not http: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2025-01-12 16:56:48 +00:00
|
|
|
# GitHub patch diff regexps can't be any shorter.
|
2019-12-19 12:01:51 +00:00
|
|
|
# rubocop:disable Layout/LineLength
|
2019-10-13 19:26:39 +01:00
|
|
|
elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)})
|
2019-12-19 12:01:51 +00:00
|
|
|
# rubocop:enable Layout/LineLength
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: Use a commit hash URL rather than patch-diff: #{patch_url}
|
|
|
|
EOS
|
2019-10-13 19:26:39 +01:00
|
|
|
elsif patch_url.match?(%r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)})
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: GitHub patches should use the full_index parameter: #{patch_url}?full_index=1
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2025-05-01 22:40:20 -04:00
|
|
|
elsif patch_url.start_with?("https://bitbucket.org/")
|
|
|
|
commit = "b354c009a60bcd6d7fc04014e200a1ee9c45c167"
|
|
|
|
fixed_url = "https://api.bitbucket.org/2.0/repositories/multicoreware/x265_git/diff/#{commit}"
|
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source:)
|
|
|
|
FormulaAudit/Patches: Bitbucket patches should use the api url: #{fixed_url}
|
|
|
|
EOS
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
2021-01-12 16:54:53 +11:00
|
|
|
expected_offense.zip([inspect_source(source).last]).each do |expected, actual|
|
2017-10-21 03:12:50 +02:00
|
|
|
expect(actual.message).to eq(expected[:message])
|
|
|
|
expect(actual.severity).to eq(expected[:severity])
|
|
|
|
expect(actual.line).to eq(expected[:line])
|
|
|
|
expect(actual.column).to eq(expected[:column])
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-12 16:54:53 +11:00
|
|
|
it "reports an offense with nested `def patches`" do
|
2018-07-11 15:17:40 +02:00
|
|
|
source = <<~RUBY
|
2017-05-25 22:21:23 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
homepage "ftp://brew.sh/foo"
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
2017-05-25 22:21:23 +05:30
|
|
|
def patches
|
|
|
|
files = %w[patch-domain_resolver.c patch-colormask.c patch-trafshow.c patch-trafshow.1 patch-configure]
|
|
|
|
{
|
|
|
|
:p0 =>
|
|
|
|
files.collect{|p| "http://trac.macports.org/export/68507/trunk/dports/net/trafshow/files/\#{p}"}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2018-07-11 15:17:40 +02:00
|
|
|
RUBY
|
2017-05-25 22:21:23 +05:30
|
|
|
|
2023-04-07 17:16:48 +01:00
|
|
|
expected_offenses = [
|
|
|
|
{
|
|
|
|
message: "FormulaAudit/Patches: Use the patch DSL instead of defining a 'patches' method",
|
|
|
|
severity: :convention,
|
|
|
|
line: 4,
|
|
|
|
column: 2,
|
2024-03-07 16:20:20 +00:00
|
|
|
source:,
|
2023-04-07 17:16:48 +01:00
|
|
|
}, {
|
|
|
|
message: "FormulaAudit/Patches: Patches from MacPorts Trac should be https://, not http: " \
|
|
|
|
"http://trac.macports.org/export/68507/trunk/dports/net/trafshow/files/",
|
|
|
|
severity: :convention,
|
|
|
|
line: 8,
|
|
|
|
column: 25,
|
2024-03-07 16:20:20 +00:00
|
|
|
source:,
|
2023-04-07 17:16:48 +01:00
|
|
|
}
|
|
|
|
]
|
2017-05-25 22:21:23 +05:30
|
|
|
|
2021-01-12 16:54:53 +11:00
|
|
|
expected_offenses.zip(inspect_source(source)).each do |expected, actual|
|
2017-10-21 03:12:50 +02:00
|
|
|
expect(actual.message).to eq(expected[:message])
|
|
|
|
expect(actual.severity).to eq(expected[:severity])
|
|
|
|
expect(actual.line).to eq(expected[:line])
|
|
|
|
expect(actual.column).to eq(expected[:column])
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-01 20:59:17 +05:30
|
|
|
|
2021-01-14 18:55:31 -08:00
|
|
|
context "when auditing inline patches" do
|
2020-06-25 22:29:34 +08:00
|
|
|
it "reports no offenses for valid inline patches" do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class Foo < Formula
|
|
|
|
url 'https://brew.sh/foo-1.0.tgz'
|
|
|
|
patch :DATA
|
|
|
|
end
|
|
|
|
__END__
|
|
|
|
patch content here
|
2020-06-27 03:13:50 +08:00
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reports no offenses for valid nested inline patches" do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class Foo < Formula
|
|
|
|
url 'https://brew.sh/foo-1.0.tgz'
|
|
|
|
stable do
|
|
|
|
patch :DATA
|
|
|
|
end
|
|
|
|
end
|
|
|
|
__END__
|
|
|
|
patch content here
|
2020-06-25 22:29:34 +08:00
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reports an offense when DATA is found with no __END__" do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Foo < Formula
|
|
|
|
url 'https://brew.sh/foo-1.0.tgz'
|
|
|
|
patch :DATA
|
2023-04-07 17:16:48 +01:00
|
|
|
^^^^^^^^^^^ FormulaAudit/Patches: patch is missing '__END__'
|
2020-06-25 22:29:34 +08:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reports an offense when __END__ is found with no DATA" do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Foo < Formula
|
|
|
|
url 'https://brew.sh/foo-1.0.tgz'
|
|
|
|
end
|
|
|
|
__END__
|
2023-04-07 17:16:48 +01:00
|
|
|
^^^^^^^ FormulaAudit/Patches: patch is missing 'DATA'
|
2020-06-25 22:29:34 +08:00
|
|
|
patch content here
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-14 18:55:31 -08:00
|
|
|
context "when auditing external patches" do
|
2021-01-12 16:54:53 +11:00
|
|
|
it "reports an offense for various patch URLs" do
|
2017-08-01 20:59:17 +05:30
|
|
|
patch_urls = [
|
|
|
|
"https://raw.github.com/mogaal/sendemail",
|
|
|
|
"https://mirrors.ustc.edu.cn/macports/trunk/",
|
|
|
|
"http://trac.macports.org/export/102865/trunk/dports/mail/uudeview/files/inews.c.patch",
|
|
|
|
"http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=5;filename=patch-libunac1.txt;att=1;bug=623340",
|
|
|
|
"https://patch-diff.githubusercontent.com/raw/foo/foo-bar/pull/100.patch",
|
2020-07-24 20:49:26 -04:00
|
|
|
"https://github.com/uber/h3/pull/362.patch?full_index=1",
|
|
|
|
"https://gitlab.gnome.org/GNOME/gitg/-/merge_requests/142.diff",
|
2020-09-11 09:07:20 -07:00
|
|
|
"https://github.com/michaeldv/pit/commit/f64978d.diff?full_index=1",
|
2021-04-19 11:19:35 -04:00
|
|
|
"https://gitlab.gnome.org/GNOME/msitools/commit/248450a.patch",
|
2017-08-01 20:59:17 +05:30
|
|
|
]
|
|
|
|
patch_urls.each do |patch_url|
|
2018-07-11 15:17:40 +02:00
|
|
|
source = <<~RUBY
|
2017-08-01 20:59:17 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
homepage "ftp://brew.sh/foo"
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
2017-08-01 20:59:17 +05:30
|
|
|
patch do
|
|
|
|
url "#{patch_url}"
|
|
|
|
sha256 "63376b8fdd6613a91976106d9376069274191860cd58f039b29ff16de1925621"
|
|
|
|
end
|
|
|
|
end
|
2018-07-11 15:17:40 +02:00
|
|
|
RUBY
|
2017-08-01 20:59:17 +05:30
|
|
|
|
2020-07-07 13:12:37 +01:00
|
|
|
expected_offense = if patch_url.include?("/raw.github.com/")
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: GitHub/Gist patches should specify a revision: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2020-07-07 13:12:37 +01:00
|
|
|
elsif patch_url.include?("macports/trunk")
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: MacPorts patches should specify a revision instead of trunk: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2022-05-20 01:44:05 +01:00
|
|
|
elsif patch_url.start_with?("http://trac.macports.org/")
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: Patches from MacPorts Trac should be https://, not http: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2022-05-20 01:44:05 +01:00
|
|
|
elsif patch_url.start_with?("http://bugs.debian.org/")
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: Patches from Debian should be https://, not http: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2020-07-24 20:49:26 -04:00
|
|
|
elsif patch_url.match?(%r{https://github.com/[^/]*/[^/]*/pull})
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: Use a commit hash URL rather than an unstable pull request URL: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2020-07-24 20:49:26 -04:00
|
|
|
elsif patch_url.match?(%r{.*gitlab.*/merge_request.*})
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: Use a commit hash URL rather than an unstable merge request URL: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2020-09-11 09:07:20 -07:00
|
|
|
elsif patch_url.match?(%r{https://github.com/[^/]*/[^/]*/commit/})
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: GitHub patches should end with .patch, not .diff: #{patch_url}
|
2021-01-30 12:17:30 -05:00
|
|
|
EOS
|
2021-04-19 11:19:35 -04:00
|
|
|
elsif patch_url.match?(%r{.*gitlab.*/commit/})
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: GitLab patches should end with .diff, not .patch: #{patch_url}
|
2021-04-19 11:19:35 -04:00
|
|
|
EOS
|
2025-01-12 16:56:48 +00:00
|
|
|
# GitHub patch diff regexps can't be any shorter.
|
2019-12-19 12:01:51 +00:00
|
|
|
# rubocop:disable Layout/LineLength
|
2019-10-13 19:26:39 +01:00
|
|
|
elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)})
|
2019-12-19 12:01:51 +00:00
|
|
|
# rubocop:enable Layout/LineLength
|
2024-03-07 16:20:20 +00:00
|
|
|
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
|
2023-04-07 17:16:48 +01:00
|
|
|
FormulaAudit/Patches: Use a commit hash URL rather than patch-diff: #{patch_url}
|
|
|
|
EOS
|
2017-08-01 20:59:17 +05:30
|
|
|
end
|
2021-01-12 16:54:53 +11:00
|
|
|
expected_offense.zip([inspect_source(source).last]).each do |expected, actual|
|
2017-10-21 03:12:50 +02:00
|
|
|
expect(actual.message).to eq(expected[:message])
|
|
|
|
expect(actual.severity).to eq(expected[:severity])
|
|
|
|
expect(actual.line).to eq(expected[:line])
|
|
|
|
expect(actual.column).to eq(expected[:column])
|
2017-08-01 20:59:17 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|