brew/Library/Homebrew/test/test_patching.rb
ilovezfs cc3d041c26 DSL method "apply" to specify patch files
The "apply" DSL method can be called from patch-do blocks to specify
the paths within an archive of the desired patch files, which will be
applied in the order in which they were supplied to the "apply" calls.
If "apply" isn't used, raise an error whenever the extracted directory
doesn't contain exactly one file.

The "apply" method can be called zero or more times within a patch-do
block with the following syntaxes supported:

    apply "single_apply"
    apply "multiple_apply_1", "multiple_apply_2"
    apply [array_of_apply]

If apply must be used, a single call using the second syntax above is
usually best practice. Each apply leaf should be the relative path to a
specific patch file in the extracted directory.

For example, if extracting this-v123-patches.tar.gz gives you

    this-123
    this-123/.DS_Store
    this-123/LICENSE.txt
    this-123/patches
    this-123/patches/A.diff
    this-123/patches/B.diff
    this-123/patches/C.diff
    this-123/README.txt

and you want to apply only B.diff and C.diff, then you need to use
"patches/B.diff" and "patches/C.diff" for the lowest-level apply leaves.

The code was provided by Xu Cheng. Any mistakes are mine.
2016-02-01 18:37:36 +00:00

254 lines
5.4 KiB
Ruby

require "testing_env"
require "formula"
class PatchingTests < Homebrew::TestCase
TESTBALL_URL = "file://#{TEST_DIRECTORY}/tarballs/testball-0.1.tbz"
TESTBALL_PATCHES_URL = "file://#{TEST_DIRECTORY}/tarballs/testball-0.1-patches.tgz"
PATCH_URL_A = "file://#{TEST_DIRECTORY}/patches/noop-a.diff"
PATCH_URL_B = "file://#{TEST_DIRECTORY}/patches/noop-b.diff"
PATCH_A_CONTENTS = File.read "#{TEST_DIRECTORY}/patches/noop-a.diff"
PATCH_B_CONTENTS = File.read "#{TEST_DIRECTORY}/patches/noop-b.diff"
APPLY_A = "noop-a.diff"
APPLY_B = "noop-b.diff"
APPLY_C = "noop-c.diff"
def formula(*args, &block)
super do
url TESTBALL_URL
sha256 TESTBALL_SHA256
class_eval(&block)
end
end
def teardown
@_f.clear_cache
@_f.patchlist.each { |p| p.clear_cache if p.external? }
end
def assert_patched(formula)
shutup do
formula.brew do
formula.patch
s = File.read("libexec/NOOP")
refute_includes s, "NOOP", "libexec/NOOP was not patched as expected"
assert_includes s, "ABCD", "libexec/NOOP was not patched as expected"
end
end
end
def assert_sequentially_patched(formula)
shutup do
formula.brew do
formula.patch
s = File.read("libexec/NOOP")
refute_includes s, "NOOP", "libexec/NOOP was not patched as expected"
refute_includes s, "ABCD", "libexec/NOOP was not patched as expected"
assert_includes s, "1234", "libexec/NOOP was not patched as expected"
end
end
end
def assert_missing_apply_fail(formula)
assert_raises(MissingApplyError) do
shutup do
formula.brew do
formula.patch
end
end
end
end
def test_single_patch
assert_patched formula {
def patches
PATCH_URL_A
end
}
end
def test_single_patch_dsl
assert_patched formula {
patch do
url PATCH_URL_A
sha256 PATCH_A_SHA256
end
}
end
def test_single_patch_dsl_with_apply
assert_patched formula {
patch do
url TESTBALL_PATCHES_URL
sha256 TESTBALL_PATCHES_SHA256
apply APPLY_A
end
}
end
def test_single_patch_dsl_with_sequential_apply
assert_sequentially_patched formula {
patch do
url TESTBALL_PATCHES_URL
sha256 TESTBALL_PATCHES_SHA256
apply APPLY_A, APPLY_C
end
}
end
def test_single_patch_dsl_with_strip
assert_patched formula {
patch :p1 do
url PATCH_URL_A
sha256 PATCH_A_SHA256
end
}
end
def test_single_patch_dsl_with_strip_with_apply
assert_patched formula {
patch :p1 do
url TESTBALL_PATCHES_URL
sha256 TESTBALL_PATCHES_SHA256
apply APPLY_A
end
}
end
def test_single_patch_dsl_with_incorrect_strip
assert_raises(ErrorDuringExecution) do
shutup do
formula do
patch :p0 do
url PATCH_URL_A
sha256 PATCH_A_SHA256
end
end.brew(&:patch)
end
end
end
def test_single_patch_dsl_with_incorrect_strip_with_apply
assert_raises(ErrorDuringExecution) do
shutup do
formula do
patch :p0 do
url TESTBALL_PATCHES_URL
sha256 TESTBALL_PATCHES_SHA256
apply APPLY_A
end
end.brew(&:patch)
end
end
end
def test_patch_p0_dsl
assert_patched formula {
patch :p0 do
url PATCH_URL_B
sha256 PATCH_B_SHA256
end
}
end
def test_patch_p0_dsl_with_apply
assert_patched formula {
patch :p0 do
url TESTBALL_PATCHES_URL
sha256 TESTBALL_PATCHES_SHA256
apply APPLY_B
end
}
end
def test_patch_p0
assert_patched formula {
def patches
{ :p0 => PATCH_URL_B }
end
}
end
def test_patch_array
assert_patched formula {
def patches
[PATCH_URL_A]
end
}
end
def test_patch_hash
assert_patched formula {
def patches
{ :p1 => PATCH_URL_A }
end
}
end
def test_patch_hash_array
assert_patched formula {
def patches
{ :p1 => [PATCH_URL_A] }
end
}
end
def test_patch_string
assert_patched formula { patch PATCH_A_CONTENTS }
end
def test_patch_string_with_strip
assert_patched formula { patch :p0, PATCH_B_CONTENTS }
end
def test_patch_DATA_constant
assert_patched formula("test", Pathname.new(__FILE__).expand_path) {
def patches
:DATA
end
}
end
def test_single_patch_missing_apply_fail
assert_missing_apply_fail formula {
def patches
TESTBALL_PATCHES_URL
end
}
end
def test_single_patch_dsl_missing_apply_fail
assert_missing_apply_fail formula {
patch do
url TESTBALL_PATCHES_URL
sha256 TESTBALL_PATCHES_SHA256
end
}
end
def test_single_patch_dsl_with_apply_enoent_fail
assert_raises(ErrorDuringExecution) do
shutup do
formula do
patch do
url TESTBALL_PATCHES_URL
sha256 TESTBALL_PATCHES_SHA256
apply "patches/#{APPLY_A}"
end
end.brew(&:patch)
end
end
end
end
__END__
diff --git a/libexec/NOOP b/libexec/NOOP
index bfdda4c..e08d8f4 100755
--- a/libexec/NOOP
+++ b/libexec/NOOP
@@ -1,2 +1,2 @@
#!/bin/bash
-echo NOOP
\ No newline at end of file
+echo ABCD
\ No newline at end of file