2017-02-15 15:19:06 +01:00
|
|
|
require "patch"
|
|
|
|
|
|
|
|
describe Patch do
|
|
|
|
describe "#create" do
|
|
|
|
context "simple patch" do
|
|
|
|
subject { described_class.create(:p2, nil) }
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2017-02-15 15:19:06 +01:00
|
|
|
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") }
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2017-02-15 15:19:06 +01:00
|
|
|
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) }
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2017-02-15 15:19:06 +01:00
|
|
|
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) }
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2017-02-15 15:19:06 +01:00
|
|
|
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) }
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2017-02-15 15:19:06 +01:00
|
|
|
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
|
2018-01-21 08:29:38 -08:00
|
|
|
its(:resource) { is_expected.to be_kind_of Resource::PatchResource }
|
2017-02-15 15:19:06 +01:00
|
|
|
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
|
2018-11-28 20:51:55 +01:00
|
|
|
patches = described_class.normalize_legacy_patches("https://brew.sh/patch.diff")
|
2017-02-15 15:19:06 +01:00
|
|
|
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(
|
2018-11-28 20:51:55 +01:00
|
|
|
%w[https://brew.sh/patch1.diff https://brew.sh/patch2.diff],
|
2017-02-15 15:19:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(patches.length).to eq(2)
|
2018-09-17 19:44:12 +02:00
|
|
|
expect(patches.first.strip).to eq(:p1)
|
|
|
|
expect(patches.second.strip).to eq(:p1)
|
2017-02-15 15:19:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can create patches from a :p0 hash" do
|
2018-03-25 13:30:37 +01:00
|
|
|
patches = described_class.normalize_legacy_patches(
|
2018-11-28 20:51:55 +01:00
|
|
|
p0: "https://brew.sh/patch.diff",
|
2017-02-15 15:19:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(patches.length).to eq(1)
|
|
|
|
expect(patches.first.strip).to eq(:p0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can create patches from a :p1 hash" do
|
2018-03-25 13:30:37 +01:00
|
|
|
patches = described_class.normalize_legacy_patches(
|
2018-11-28 20:51:55 +01:00
|
|
|
p1: "https://brew.sh/patch.diff",
|
2017-02-15 15:19:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(patches.length).to eq(1)
|
|
|
|
expect(patches.first.strip).to eq(:p1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can create patches from a mixed hash" do
|
2018-03-25 13:30:37 +01:00
|
|
|
patches = described_class.normalize_legacy_patches(
|
2018-11-28 20:51:55 +01:00
|
|
|
p1: "https://brew.sh/patch1.diff",
|
|
|
|
p0: "https://brew.sh/patch0.diff",
|
2017-02-15 15:19:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2018-03-25 13:30:37 +01:00
|
|
|
patches = described_class.normalize_legacy_patches(
|
2017-02-15 15:19:06 +01:00
|
|
|
p1: [
|
2018-11-28 20:51:55 +01:00
|
|
|
"https://brew.sh/patch10.diff",
|
|
|
|
"https://brew.sh/patch11.diff",
|
2017-02-15 15:19:06 +01:00
|
|
|
],
|
|
|
|
p0: [
|
2018-11-28 20:51:55 +01:00
|
|
|
"https://brew.sh/patch00.diff",
|
|
|
|
"https://brew.sh/patch01.diff",
|
2017-02-15 15:19:06 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2018-03-25 13:30:37 +01:00
|
|
|
expect(described_class.normalize_legacy_patches(nil)).to be_empty
|
2017-02-15 15:19:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe EmbeddedPatch do
|
|
|
|
describe "#new" do
|
|
|
|
subject { described_class.new(:p1) }
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2017-02-15 15:19:06 +01:00
|
|
|
its(:inspect) { is_expected.to eq("#<EmbeddedPatch: :p1>") }
|
|
|
|
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('#<ExternalPatch: :p1 "file:///my.patch">') }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#cached_download" do
|
2018-03-25 13:30:37 +01:00
|
|
|
before do
|
2017-02-15 15:19:06 +01:00
|
|
|
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
|