diff --git a/Library/Homebrew/test/patch_spec.rb b/Library/Homebrew/test/patch_spec.rb new file mode 100644 index 0000000000..22c1036627 --- /dev/null +++ b/Library/Homebrew/test/patch_spec.rb @@ -0,0 +1,165 @@ +require "patch" + +describe Patch do + describe "#create" do + context "simple patch" do + subject { described_class.create(:p2, nil) } + it { is_expected.to be_kind_of ExternalPatch } + it { is_expected.to be_external } + its(:strip) { is_expected.to eq(:p2) } + end + + context "string patch" do + subject { described_class.create(:p0, "foo") } + it { is_expected.to be_kind_of StringPatch } + its(:strip) { is_expected.to eq(:p0) } + end + + context "string patch without strip" do + subject { described_class.create("foo", nil) } + it { is_expected.to be_kind_of StringPatch } + its(:strip) { is_expected.to eq(:p1) } + end + + context "data patch" do + subject { described_class.create(:p0, :DATA) } + it { is_expected.to be_kind_of DATAPatch } + its(:strip) { is_expected.to eq(:p0) } + end + + context "data patch without strip" do + subject { described_class.create(:DATA, nil) } + it { is_expected.to be_kind_of DATAPatch } + its(:strip) { is_expected.to eq(:p1) } + end + + it "raises an error for unknown values" do + expect { + described_class.create(Object.new) + }.to raise_error(ArgumentError) + + expect { + described_class.create(Object.new, Object.new) + }.to raise_error(ArgumentError) + end + end + + describe "#patch_files" do + subject { described_class.create(:p2, nil) } + + context "empty patch" do + its(:resource) { is_expected.to be_kind_of Resource::Patch } + its(:patch_files) { is_expected.to eq(subject.resource.patch_files) } + its(:patch_files) { is_expected.to eq([]) } + end + + it "returns applied patch files" do + subject.resource.apply("patch1.diff") + expect(subject.patch_files).to eq(["patch1.diff"]) + + subject.resource.apply("patch2.diff", "patch3.diff") + expect(subject.patch_files).to eq(["patch1.diff", "patch2.diff", "patch3.diff"]) + + subject.resource.apply(["patch4.diff", "patch5.diff"]) + expect(subject.patch_files.count).to eq(5) + + subject.resource.apply("patch4.diff", ["patch5.diff", "patch6.diff"], "patch7.diff") + expect(subject.patch_files.count).to eq(7) + end + end + + describe "#normalize_legacy_patches" do + it "can create a patch from a single string" do + patches = described_class.normalize_legacy_patches("http://example.com/patch.diff") + expect(patches.length).to eq(1) + expect(patches.first.strip).to eq(:p1) + end + + it "can create patches from an array" do + patches = described_class.normalize_legacy_patches( + %w[http://example.com/patch1.diff http://example.com/patch2.diff], + ) + + expect(patches.length).to eq(2) + expect(patches[0].strip).to eq(:p1) + expect(patches[1].strip).to eq(:p1) + end + + it "can create patches from a :p0 hash" do + patches = Patch.normalize_legacy_patches( + p0: "http://example.com/patch.diff", + ) + + expect(patches.length).to eq(1) + expect(patches.first.strip).to eq(:p0) + end + + it "can create patches from a :p1 hash" do + patches = Patch.normalize_legacy_patches( + p1: "http://example.com/patch.diff", + ) + + expect(patches.length).to eq(1) + expect(patches.first.strip).to eq(:p1) + end + + it "can create patches from a mixed hash" do + patches = Patch.normalize_legacy_patches( + p1: "http://example.com/patch1.diff", + p0: "http://example.com/patch0.diff", + ) + + expect(patches.length).to eq(2) + expect(patches.count { |p| p.strip == :p0 }).to eq(1) + expect(patches.count { |p| p.strip == :p1 }).to eq(1) + end + + it "can create patches from a mixed hash with array" do + patches = Patch.normalize_legacy_patches( + p1: [ + "http://example.com/patch10.diff", + "http://example.com/patch11.diff", + ], + p0: [ + "http://example.com/patch00.diff", + "http://example.com/patch01.diff", + ], + ) + + expect(patches.length).to eq(4) + expect(patches.count { |p| p.strip == :p0 }).to eq(2) + expect(patches.count { |p| p.strip == :p1 }).to eq(2) + end + + it "returns an empty array if given nil" do + expect(Patch.normalize_legacy_patches(nil)).to be_empty + end + end +end + +describe EmbeddedPatch do + describe "#new" do + subject { described_class.new(:p1) } + its(:inspect) { is_expected.to eq("#") } + end +end + +describe ExternalPatch do + subject { described_class.new(:p1) { url "file:///my.patch" } } + + describe "#url" do + its(:url) { is_expected.to eq("file:///my.patch") } + end + + describe "#inspect" do + its(:inspect) { is_expected.to eq('#') } + end + + describe "#cached_download" do + before(:each) do + allow(subject.resource).to receive(:cached_download).and_return("/tmp/foo.tar.gz") + end + + its(:cached_download) { is_expected.to eq("/tmp/foo.tar.gz") } + end +end diff --git a/Library/Homebrew/test/patch_test.rb b/Library/Homebrew/test/patch_test.rb deleted file mode 100644 index 3055eaf48d..0000000000 --- a/Library/Homebrew/test/patch_test.rb +++ /dev/null @@ -1,155 +0,0 @@ -require "testing_env" -require "patch" - -class PatchTests < Homebrew::TestCase - def test_create_simple - patch = Patch.create(:p2, nil) - assert_kind_of ExternalPatch, patch - assert_predicate patch, :external? - assert_equal :p2, patch.strip - end - - def test_create_string - patch = Patch.create(:p0, "foo") - assert_kind_of StringPatch, patch - assert_equal :p0, patch.strip - end - - def test_create_string_without_strip - patch = Patch.create("foo", nil) - assert_kind_of StringPatch, patch - assert_equal :p1, patch.strip - end - - def test_create_data - patch = Patch.create(:p0, :DATA) - assert_kind_of DATAPatch, patch - assert_equal :p0, patch.strip - end - - def test_create_data_without_strip - patch = Patch.create(:DATA, nil) - assert_kind_of DATAPatch, patch - assert_equal :p1, patch.strip - end - - def test_raises_for_unknown_values - assert_raises(ArgumentError) { Patch.create(Object.new) } - assert_raises(ArgumentError) { Patch.create(Object.new, Object.new) } - end -end - -class LegacyPatchTests < Homebrew::TestCase - def test_patch_single_string - patches = Patch.normalize_legacy_patches("http://example.com/patch.diff") - assert_equal 1, patches.length - assert_equal :p1, patches.first.strip - end - - def test_patch_array - patches = Patch.normalize_legacy_patches( - %w[http://example.com/patch1.diff http://example.com/patch2.diff], - ) - - assert_equal 2, patches.length - assert_equal :p1, patches[0].strip - assert_equal :p1, patches[1].strip - end - - def test_p0_hash_to_string - patches = Patch.normalize_legacy_patches( - p0: "http://example.com/patch.diff", - ) - - assert_equal 1, patches.length - assert_equal :p0, patches.first.strip - end - - def test_p1_hash_to_string - patches = Patch.normalize_legacy_patches( - p1: "http://example.com/patch.diff", - ) - - assert_equal 1, patches.length - assert_equal :p1, patches.first.strip - end - - def test_mixed_hash_to_strings - patches = Patch.normalize_legacy_patches( - p1: "http://example.com/patch1.diff", - p0: "http://example.com/patch0.diff", - ) - assert_equal 2, patches.length - assert_equal 1, patches.count { |p| p.strip == :p0 } - assert_equal 1, patches.count { |p| p.strip == :p1 } - end - - def test_mixed_hash_to_arrays - patches = Patch.normalize_legacy_patches( - p1: ["http://example.com/patch10.diff", - "http://example.com/patch11.diff"], - p0: ["http://example.com/patch00.diff", - "http://example.com/patch01.diff"], - ) - - assert_equal 4, patches.length - assert_equal 2, patches.count { |p| p.strip == :p0 } - assert_equal 2, patches.count { |p| p.strip == :p1 } - end - - def test_nil - assert_empty Patch.normalize_legacy_patches(nil) - end -end - -class EmbeddedPatchTests < Homebrew::TestCase - def test_inspect - p = EmbeddedPatch.new :p1 - assert_equal "#", p.inspect - end -end - -class ExternalPatchTests < Homebrew::TestCase - def setup - super - @p = ExternalPatch.new(:p1) { url "file:///my.patch" } - end - - def test_url - assert_equal "file:///my.patch", @p.url - end - - def test_inspect - assert_equal '#', @p.inspect - end - - def test_cached_download - @p.resource.stubs(:cached_download).returns "/tmp/foo.tar.gz" - assert_equal "/tmp/foo.tar.gz", @p.cached_download - end -end - -class ApplyPatchTests < Homebrew::TestCase - def test_empty_patch_files - patch = Patch.create(:p2, nil) - resource = patch.resource - patch_files = patch.patch_files - assert_kind_of Resource::Patch, resource - assert_equal patch_files, resource.patch_files - assert_equal patch_files, [] - end - - def test_resource_patch_apply_method - patch = Patch.create(:p2, nil) - resource = patch.resource - patch_files = patch.patch_files - resource.apply("patch1.diff") - assert_equal patch_files, ["patch1.diff"] - resource.apply("patch2.diff", "patch3.diff") - assert_equal patch_files, ["patch1.diff", "patch2.diff", "patch3.diff"] - resource.apply(["patch4.diff", "patch5.diff"]) - assert_equal patch_files.count, 5 - resource.apply("patch4.diff", ["patch5.diff", "patch6.diff"], "patch7.diff") - assert_equal patch_files.count, 7 - end -end