2018-07-01 23:35:29 +02:00
|
|
|
require "unpack_strategy"
|
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
RSpec.shared_examples "UnpackStrategy::detect" do
|
|
|
|
it "is correctly detected" do
|
|
|
|
expect(UnpackStrategy.detect(path)).to be_a described_class
|
|
|
|
end
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
RSpec.shared_examples "#extract" do |children: []|
|
|
|
|
specify "#extract" do
|
|
|
|
mktmpdir do |unpack_dir|
|
|
|
|
described_class.new(path).extract(to: unpack_dir)
|
|
|
|
expect(unpack_dir.children(false).map(&:to_s)).to match_array children
|
2018-07-01 23:35:29 +02:00
|
|
|
end
|
|
|
|
end
|
2018-07-09 20:04:33 +02:00
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-16 19:00:49 +02:00
|
|
|
describe UnpackStrategy do
|
|
|
|
describe "#extract_nestedly" do
|
2018-07-16 20:56:41 +02:00
|
|
|
subject(:strategy) { described_class.detect(path) }
|
2018-07-16 19:00:49 +02:00
|
|
|
|
|
|
|
let(:unpack_dir) { mktmpdir }
|
|
|
|
|
2018-07-16 20:56:41 +02:00
|
|
|
context "when extracting a GZIP nested in a BZIP2" do
|
|
|
|
let(:file_name) { "file" }
|
|
|
|
let(:path) {
|
|
|
|
dir = mktmpdir
|
|
|
|
|
|
|
|
(dir/"file").write "This file was inside a GZIP inside a BZIP2."
|
|
|
|
system "gzip", dir.children.first
|
|
|
|
system "bzip2", dir.children.first
|
|
|
|
|
|
|
|
dir.children.first
|
|
|
|
}
|
|
|
|
|
|
|
|
it "can extract nested archives" do
|
|
|
|
strategy.extract_nestedly(to: unpack_dir)
|
|
|
|
|
|
|
|
expect(File.read(unpack_dir/file_name)).to eq("This file was inside a GZIP inside a BZIP2.")
|
|
|
|
end
|
|
|
|
end
|
2018-07-16 19:00:49 +02:00
|
|
|
|
2018-07-16 20:56:41 +02:00
|
|
|
context "when extracting a directory with nested directories" do
|
|
|
|
let(:directories) { "A/B/C" }
|
|
|
|
let(:path) {
|
|
|
|
(mktmpdir/"file.tar").tap do |path|
|
|
|
|
mktmpdir do |dir|
|
|
|
|
(dir/directories).mkpath
|
|
|
|
system "tar", "-c", "-f", path, "-C", dir, "A/"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
it "does not recurse into nested directories" do
|
|
|
|
strategy.extract_nestedly(to: unpack_dir)
|
|
|
|
expect(Pathname.glob(unpack_dir/"**/*")).to include unpack_dir/directories
|
|
|
|
end
|
2018-07-16 19:00:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-18 13:02:32 +02:00
|
|
|
describe DirectoryUnpackStrategy do
|
|
|
|
let(:path) {
|
|
|
|
mktmpdir.tap do |path|
|
|
|
|
FileUtils.touch path/"file"
|
|
|
|
FileUtils.ln_s "file", path/"symlink"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
subject(:strategy) { described_class.new(path) }
|
|
|
|
let(:unpack_dir) { mktmpdir }
|
|
|
|
|
|
|
|
it "does not follow symlinks" do
|
|
|
|
strategy.extract(to: unpack_dir)
|
|
|
|
expect(unpack_dir/"symlink").to be_a_symlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe UncompressedUnpackStrategy do
|
|
|
|
let(:path) {
|
|
|
|
(mktmpdir/"test").tap do |path|
|
|
|
|
FileUtils.touch path
|
2018-07-01 23:35:29 +02:00
|
|
|
end
|
2018-07-09 20:04:33 +02:00
|
|
|
}
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe P7ZipUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"cask/container.7z" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-11 01:56:49 +02:00
|
|
|
describe XarUnpackStrategy, :needs_macos do
|
2018-07-09 20:04:33 +02:00
|
|
|
let(:path) { TEST_FIXTURE_DIR/"cask/container.xar" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["container"]
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe XzUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"cask/container.xz" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe RarUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"cask/container.rar" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe LzipUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"test.lz" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe LhaUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"test.lha" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe JarUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"test.jar" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["test.jar"]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ZipUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"cask/MyFancyApp.zip" }
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["MyFancyApp"]
|
|
|
|
|
|
|
|
context "when ZIP archive is corrupted" do
|
|
|
|
let(:path) {
|
|
|
|
(mktmpdir/"test.zip").tap do |path|
|
|
|
|
FileUtils.touch path
|
2018-07-01 23:35:29 +02:00
|
|
|
end
|
2018-07-09 20:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
2018-07-01 23:35:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe GzipUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"cask/container.gz" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["container"]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Bzip2UnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"cask/container.bz2" }
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["container"]
|
|
|
|
end
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe TarUnpackStrategy do
|
|
|
|
let(:path) { TEST_FIXTURE_DIR/"cask/container.tar.gz" }
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["container"]
|
2018-07-01 23:35:29 +02:00
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
context "when TAR archive is corrupted" do
|
|
|
|
let(:path) {
|
|
|
|
(mktmpdir/"test.tar").tap do |path|
|
|
|
|
FileUtils.touch path
|
2018-07-01 23:35:29 +02:00
|
|
|
end
|
2018-07-09 20:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
2018-07-01 23:35:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-09 20:04:33 +02:00
|
|
|
describe GitUnpackStrategy do
|
|
|
|
let(:repo) {
|
|
|
|
mktmpdir.tap do |repo|
|
|
|
|
system "git", "-C", repo, "init"
|
|
|
|
|
|
|
|
FileUtils.touch repo/"test"
|
|
|
|
system "git", "-C", repo, "add", "test"
|
|
|
|
system "git", "-C", repo, "commit", "-m", "Add `test` file."
|
|
|
|
end
|
|
|
|
}
|
|
|
|
let(:path) { repo }
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: [".git", "test"]
|
|
|
|
end
|
|
|
|
|
2018-07-01 23:35:29 +02:00
|
|
|
describe SubversionUnpackStrategy do
|
2018-07-09 20:04:33 +02:00
|
|
|
let(:repo) {
|
|
|
|
mktmpdir.tap do |repo|
|
|
|
|
system "svnadmin", "create", repo
|
2018-07-01 23:35:29 +02:00
|
|
|
end
|
2018-07-09 20:04:33 +02:00
|
|
|
}
|
|
|
|
let(:working_copy) {
|
|
|
|
mktmpdir.tap do |working_copy|
|
|
|
|
system "svn", "checkout", "file://#{repo}", working_copy
|
|
|
|
|
|
|
|
FileUtils.touch working_copy/"test"
|
|
|
|
system "svn", "add", working_copy/"test"
|
|
|
|
system "svn", "commit", working_copy, "-m", "Add `test` file."
|
|
|
|
end
|
|
|
|
}
|
|
|
|
let(:path) { working_copy }
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["test"]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe CvsUnpackStrategy do
|
|
|
|
let(:repo) {
|
|
|
|
mktmpdir.tap do |repo|
|
|
|
|
FileUtils.touch repo/"test"
|
|
|
|
(repo/"CVS").mkpath
|
|
|
|
end
|
|
|
|
}
|
|
|
|
let(:path) { repo }
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["CVS", "test"]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe BazaarUnpackStrategy do
|
|
|
|
let(:repo) {
|
|
|
|
mktmpdir.tap do |repo|
|
|
|
|
FileUtils.touch repo/"test"
|
|
|
|
(repo/".bzr").mkpath
|
|
|
|
end
|
|
|
|
}
|
|
|
|
let(:path) { repo }
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
|
|
|
include_examples "#extract", children: ["test"]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe MercurialUnpackStrategy do
|
|
|
|
let(:repo) {
|
|
|
|
mktmpdir.tap do |repo|
|
|
|
|
(repo/".hg").mkpath
|
|
|
|
end
|
|
|
|
}
|
|
|
|
let(:path) { repo }
|
|
|
|
|
|
|
|
include_examples "UnpackStrategy::detect"
|
2018-07-01 23:35:29 +02:00
|
|
|
end
|