brew/Library/Homebrew/test/rubocops/patches_spec.rb

321 lines
12 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "rubocops/patches"
describe RuboCop::Cop::FormulaAudit::Patches do
subject(:cop) { described_class.new }
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)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
end
2017-10-21 03:12:50 +02:00
RUBY
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)
class Foo < Formula
homepage "ftp://brew.sh/foo"
url "https://brew.sh/foo-1.0.tgz"
def patches
2017-10-21 03:12:50 +02:00
^^^^^^^^^^^ Use the patch DSL instead of defining a 'patches' method
DATA
end
end
2017-10-21 03:12:50 +02:00
RUBY
end
2021-01-12 16:54:53 +11:00
it "reports an offense for various patch URLs" do
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",
"https://github.com/dlang/dub/commit/2c916b1a7999a050ac4970c3415ff8f91cd487aa.patch",
]
patch_urls.each do |patch_url|
2017-10-15 02:28:32 +02:00
source = <<~EOS
class Foo < Formula
homepage "ftp://brew.sh/foo"
url "https://brew.sh/foo-1.0.tgz"
def patches
"#{patch_url}"
end
end
EOS
2020-07-07 13:12:37 +01:00
expected_offense = if patch_url.include?("/raw.github.com/")
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
GitHub/Gist patches should specify a revision:
2019-04-01 16:02:13 -04:00
#{patch_url}
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 4,
2018-11-02 17:18:07 +00:00
source: source }]
2020-07-07 13:12:37 +01:00
elsif patch_url.include?("macports/trunk")
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
MacPorts patches should specify a revision instead of trunk:
2019-04-01 16:02:13 -04:00
#{patch_url}
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 4,
2018-11-02 17:18:07 +00:00
source: source }]
2020-05-22 08:52:26 +01:00
elsif patch_url.start_with?("http://trac.macports.org")
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
Patches from MacPorts Trac should be https://, not http:
2019-04-01 16:02:13 -04:00
#{patch_url}
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 4,
2018-11-02 17:18:07 +00:00
source: source }]
2020-05-22 08:52:26 +01:00
elsif patch_url.start_with?("http://bugs.debian.org")
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
Patches from Debian should be https://, not http:
2019-04-01 16:02:13 -04:00
#{patch_url}
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 4,
2018-11-02 17:18:07 +00:00
source: source }]
2019-12-19 12:01:51 +00:00
# rubocop:disable Layout/LineLength
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
[{ message: "Use a commit hash URL rather than patch-diff: #{patch_url}",
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 4,
2018-11-02 17:18:07 +00:00
source: source }]
elsif patch_url.match?(%r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)})
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS,
GitHub patches should use the full_index parameter:
#{patch_url}?full_index=1
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 4,
2018-11-02 17:18:07 +00:00
source: source }]
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])
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
class Foo < Formula
homepage "ftp://brew.sh/foo"
url "https://brew.sh/foo-1.0.tgz"
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
2018-11-02 17:18:07 +00:00
expected_offenses = [{ message: "Use the patch DSL instead of defining a 'patches' method",
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 4,
column: 2,
source: source },
{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
Patches from MacPorts Trac should be https://, not http:
2019-04-01 16:02:13 -04:00
http://trac.macports.org/export/68507/trunk/dports/net/trafshow/files/
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 8,
2021-01-12 16:54:53 +11:00
column: 25,
2018-11-02 17:18:07 +00:00
source: source }]
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])
end
end
end
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
^^^^^^^^^^^ patch is missing '__END__'
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__
^^^^^^^ patch is missing 'DATA'
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
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",
"https://github.com/uber/h3/pull/362.patch?full_index=1",
"https://gitlab.gnome.org/GNOME/gitg/-/merge_requests/142.diff",
"https://github.com/michaeldv/pit/commit/f64978d.diff?full_index=1",
"https://gitlab.gnome.org/GNOME/msitools/commit/248450a.diff",
]
patch_urls.each do |patch_url|
2018-07-11 15:17:40 +02:00
source = <<~RUBY
class Foo < Formula
homepage "ftp://brew.sh/foo"
url "https://brew.sh/foo-1.0.tgz"
patch do
url "#{patch_url}"
sha256 "63376b8fdd6613a91976106d9376069274191860cd58f039b29ff16de1925621"
end
end
2018-07-11 15:17:40 +02:00
RUBY
2020-07-07 13:12:37 +01:00
expected_offense = if patch_url.include?("/raw.github.com/")
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
GitHub/Gist patches should specify a revision:
2019-04-01 16:02:13 -04:00
#{patch_url}
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
2018-11-02 17:18:07 +00:00
source: source }]
2020-07-07 13:12:37 +01:00
elsif patch_url.include?("macports/trunk")
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
MacPorts patches should specify a revision instead of trunk:
2019-04-01 16:02:13 -04:00
#{patch_url}
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
2018-11-02 17:18:07 +00:00
source: source }]
2020-05-22 08:52:26 +01:00
elsif patch_url.start_with?("http://trac.macports.org")
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
Patches from MacPorts Trac should be https://, not http:
2019-04-01 16:02:13 -04:00
#{patch_url}
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
2018-11-02 17:18:07 +00:00
source: source }]
2020-05-22 08:52:26 +01:00
elsif patch_url.start_with?("http://bugs.debian.org")
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS.chomp,
Patches from Debian should be https://, not http:
2019-04-01 16:02:13 -04:00
#{patch_url}
2018-11-02 17:18:07 +00:00
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
2018-11-02 17:18:07 +00:00
source: source }]
elsif patch_url.match?(%r{https://github.com/[^/]*/[^/]*/pull})
[{ message: "Use a commit hash URL rather than an unstable pull request URL: #{patch_url}",
severity: :convention,
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
source: source }]
elsif patch_url.match?(%r{.*gitlab.*/merge_request.*})
[{ message: "Use a commit hash URL rather than an unstable merge request URL: #{patch_url}",
severity: :convention,
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
source: source }]
elsif patch_url.match?(%r{https://github.com/[^/]*/[^/]*/commit/})
[{ message:
<<~EOS.chomp,
GitHub patches should end with .patch, not .diff:
#{patch_url}
EOS
severity: :convention,
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
source: source }]
elsif patch_url.match?(%r{.*gitlab.*/commit/})
[{ message:
<<~EOS.chomp,
GitLab patches should end with .patch, not .diff:
#{patch_url}
EOS
severity: :convention,
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
source: source }]
2019-12-19 12:01:51 +00:00
# rubocop:disable Layout/LineLength
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
[{ message: "Use a commit hash URL rather than patch-diff: #{patch_url}",
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
2021-01-12 16:54:53 +11:00
column: 8,
2018-11-02 17:18:07 +00:00
source: source }]
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])
end
end
end
end
end