2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-25 11:12:05 +01:00
|
|
|
require "formula"
|
|
|
|
|
2024-04-03 09:12:47 -07:00
|
|
|
RSpec.describe "patching", type: :system do
|
2023-03-08 23:14:46 +00:00
|
|
|
let(:formula_subclass) do
|
|
|
|
Class.new(Formula) do
|
2023-01-26 09:42:57 -08:00
|
|
|
# These are defined within an anonymous class to avoid polluting the global namespace.
|
2023-02-18 23:09:23 +00:00
|
|
|
# rubocop:disable RSpec/LeakyConstantDeclaration,Lint/ConstantDefinitionInBlock
|
2023-12-16 11:47:46 +00:00
|
|
|
TESTBALL_URL = "file://#{TEST_FIXTURE_DIR}/tarballs/testball-0.1.tbz".freeze
|
|
|
|
TESTBALL_PATCHES_URL = "file://#{TEST_FIXTURE_DIR}/tarballs/testball-0.1-patches.tgz".freeze
|
|
|
|
PATCH_URL_A = "file://#{TEST_FIXTURE_DIR}/patches/noop-a.diff".freeze
|
|
|
|
PATCH_URL_B = "file://#{TEST_FIXTURE_DIR}/patches/noop-b.diff".freeze
|
2024-10-23 12:37:53 +01:00
|
|
|
PATCH_URL_D = "file://#{TEST_FIXTURE_DIR}/patches/noop-d.diff".freeze
|
2023-01-26 09:42:57 -08:00
|
|
|
PATCH_A_CONTENTS = File.read("#{TEST_FIXTURE_DIR}/patches/noop-a.diff").freeze
|
|
|
|
PATCH_B_CONTENTS = File.read("#{TEST_FIXTURE_DIR}/patches/noop-b.diff").freeze
|
|
|
|
APPLY_A = "noop-a.diff"
|
|
|
|
APPLY_B = "noop-b.diff"
|
|
|
|
APPLY_C = "noop-c.diff"
|
2024-10-23 12:37:53 +01:00
|
|
|
APPLY_D = "noop-d.diff"
|
2023-02-18 23:09:23 +00:00
|
|
|
# rubocop:enable RSpec/LeakyConstantDeclaration,Lint/ConstantDefinitionInBlock
|
2023-01-26 09:42:57 -08:00
|
|
|
|
2017-02-25 11:12:05 +01:00
|
|
|
url TESTBALL_URL
|
|
|
|
sha256 TESTBALL_SHA256
|
2023-03-08 23:14:46 +00:00
|
|
|
end
|
|
|
|
end
|
2023-01-26 09:42:57 -08:00
|
|
|
|
|
|
|
def formula(name = "formula_name", path: Formulary.core_path(name), spec: :stable, alias_path: nil, &block)
|
|
|
|
formula_subclass.class_eval(&block)
|
2024-03-07 16:20:20 +00:00
|
|
|
formula_subclass.new(name, path, spec, alias_path:)
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
matcher :be_patched do
|
|
|
|
match do |formula|
|
2017-07-29 19:55:05 +02:00
|
|
|
formula.brew do
|
|
|
|
formula.patch
|
|
|
|
s = File.read("libexec/NOOP")
|
|
|
|
expect(s).not_to include("NOOP"), "libexec/NOOP was not patched as expected"
|
|
|
|
expect(s).to include("ABCD"), "libexec/NOOP was not patched as expected"
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-23 12:37:53 +01:00
|
|
|
matcher :be_patched_with_homebrew_prefix do
|
|
|
|
match do |formula|
|
|
|
|
formula.brew do
|
|
|
|
formula.patch
|
|
|
|
s = File.read("libexec/NOOP")
|
|
|
|
expect(s).not_to include("NOOP"), "libexec/NOOP was not patched as expected"
|
|
|
|
expect(s).not_to include("HOMEBREW_PREFIX"), "libexec/NOOP was not patched as expected"
|
|
|
|
expect(s).to include(HOMEBREW_PREFIX.to_s), "libexec/NOOP was not patched as expected"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-21 08:29:38 -08:00
|
|
|
matcher :have_its_resource_patched do
|
|
|
|
match do |formula|
|
|
|
|
formula.brew do
|
|
|
|
formula.resources.first.stage Pathname.pwd/"resource_dir"
|
|
|
|
s = File.read("resource_dir/libexec/NOOP")
|
|
|
|
expect(s).not_to include("NOOP"), "libexec/NOOP was not patched as expected"
|
|
|
|
expect(s).to include("ABCD"), "libexec/NOOP was not patched as expected"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-25 11:12:05 +01:00
|
|
|
matcher :be_sequentially_patched do
|
|
|
|
match do |formula|
|
2017-07-29 19:55:05 +02:00
|
|
|
formula.brew do
|
|
|
|
formula.patch
|
|
|
|
s = File.read("libexec/NOOP")
|
|
|
|
expect(s).not_to include("NOOP"), "libexec/NOOP was not patched as expected"
|
|
|
|
expect(s).not_to include("ABCD"), "libexec/NOOP was not patched as expected"
|
|
|
|
expect(s).to include("1234"), "libexec/NOOP was not patched as expected"
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
matcher :miss_apply do
|
|
|
|
match do |formula|
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2017-07-29 19:55:05 +02:00
|
|
|
formula.brew do
|
|
|
|
formula.patch
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
2023-03-08 23:14:46 +00:00
|
|
|
end.to raise_error(MissingApplyError)
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "single_patch_dsl" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch do
|
|
|
|
url PATCH_URL_A
|
|
|
|
sha256 PATCH_A_SHA256
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to be_patched
|
|
|
|
end
|
|
|
|
|
2018-01-21 08:29:38 -08:00
|
|
|
specify "single_patch_dsl_for_resource" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
resource "some_resource" do
|
|
|
|
url TESTBALL_URL
|
|
|
|
sha256 TESTBALL_SHA256
|
|
|
|
|
|
|
|
patch do
|
|
|
|
url PATCH_URL_A
|
|
|
|
sha256 PATCH_A_SHA256
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to have_its_resource_patched
|
|
|
|
end
|
|
|
|
|
2017-02-25 11:12:05 +01:00
|
|
|
specify "single_patch_dsl_with_apply" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch do
|
|
|
|
url TESTBALL_PATCHES_URL
|
|
|
|
sha256 TESTBALL_PATCHES_SHA256
|
|
|
|
apply APPLY_A
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to be_patched
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "single_patch_dsl_with_sequential_apply" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch do
|
|
|
|
url TESTBALL_PATCHES_URL
|
|
|
|
sha256 TESTBALL_PATCHES_SHA256
|
|
|
|
apply APPLY_A, APPLY_C
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to be_sequentially_patched
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "single_patch_dsl_with_strip" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch :p1 do
|
|
|
|
url PATCH_URL_A
|
|
|
|
sha256 PATCH_A_SHA256
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to be_patched
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "single_patch_dsl_with_strip_with_apply" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch :p1 do
|
|
|
|
url TESTBALL_PATCHES_URL
|
|
|
|
sha256 TESTBALL_PATCHES_SHA256
|
|
|
|
apply APPLY_A
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to be_patched
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "single_patch_dsl_with_incorrect_strip" do
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2017-07-29 19:55:05 +02:00
|
|
|
f = formula do
|
|
|
|
patch :p0 do
|
|
|
|
url PATCH_URL_A
|
|
|
|
sha256 PATCH_A_SHA256
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
end
|
2017-07-29 19:55:05 +02:00
|
|
|
|
|
|
|
f.brew { |formula, _staging| formula.patch }
|
2023-03-08 23:14:46 +00:00
|
|
|
end.to raise_error(BuildError)
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
specify "single_patch_dsl_with_incorrect_strip_with_apply" do
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2017-07-29 19:55:05 +02:00
|
|
|
f = formula do
|
|
|
|
patch :p0 do
|
|
|
|
url TESTBALL_PATCHES_URL
|
|
|
|
sha256 TESTBALL_PATCHES_SHA256
|
|
|
|
apply APPLY_A
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
end
|
2017-07-29 19:55:05 +02:00
|
|
|
|
|
|
|
f.brew { |formula, _staging| formula.patch }
|
2023-03-08 23:14:46 +00:00
|
|
|
end.to raise_error(BuildError)
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
specify "patch_p0_dsl" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch :p0 do
|
|
|
|
url PATCH_URL_B
|
|
|
|
sha256 PATCH_B_SHA256
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to be_patched
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "patch_p0_dsl_with_apply" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch :p0 do
|
|
|
|
url TESTBALL_PATCHES_URL
|
|
|
|
sha256 TESTBALL_PATCHES_SHA256
|
|
|
|
apply APPLY_B
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to be_patched
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "patch_string" do
|
|
|
|
expect(formula { patch PATCH_A_CONTENTS }).to be_patched
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "patch_string_with_strip" do
|
|
|
|
expect(formula { patch :p0, PATCH_B_CONTENTS }).to be_patched
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "single_patch_dsl_missing_apply_fail" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch do
|
|
|
|
url TESTBALL_PATCHES_URL
|
|
|
|
sha256 TESTBALL_PATCHES_SHA256
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to miss_apply
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "single_patch_dsl_with_apply_enoent_fail" do
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2017-07-29 19:55:05 +02:00
|
|
|
f = formula do
|
|
|
|
patch do
|
|
|
|
url TESTBALL_PATCHES_URL
|
|
|
|
sha256 TESTBALL_PATCHES_SHA256
|
|
|
|
apply "patches/#{APPLY_A}"
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
end
|
2017-07-29 19:55:05 +02:00
|
|
|
|
|
|
|
f.brew { |formula, _staging| formula.patch }
|
2024-10-23 12:37:53 +01:00
|
|
|
end.to raise_error(Errno::ENOENT)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "patch_dsl_with_homebrew_prefix" do
|
|
|
|
expect(
|
|
|
|
formula do
|
|
|
|
patch do
|
|
|
|
url PATCH_URL_D
|
|
|
|
sha256 PATCH_D_SHA256
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
).to be_patched_with_homebrew_prefix
|
2017-02-25 11:12:05 +01:00
|
|
|
end
|
|
|
|
end
|