Refactor unpack strategy specs.

This commit is contained in:
Markus Reiter 2018-07-09 20:04:33 +02:00
parent 8ea7ad2218
commit 633c590aac
2 changed files with 160 additions and 104 deletions

View File

@ -1,127 +1,192 @@
require "unpack_strategy" require "unpack_strategy"
describe UnpackStrategy, :focus do RSpec.shared_examples "UnpackStrategy::detect" do
matcher :be_detected_as_a do |klass| it "is correctly detected" do
match do |expected| expect(UnpackStrategy.detect(path)).to be_a described_class
@detected = described_class.detect(expected) end
@detected.is_a?(klass) end
end
failure_message do RSpec.shared_examples "#extract" do |children: []|
<<~EOS specify "#extract" do
expected: #{klass} mktmpdir do |unpack_dir|
detected: #{@detected} described_class.new(path).extract(to: unpack_dir)
EOS expect(unpack_dir.children(false).map(&:to_s)).to match_array children
end end
end end
end
describe "::detect" do describe UncompressedUnpackStrategy do
it "correctly detects JAR files" do let(:path) {
expect(TEST_FIXTURE_DIR/"test.jar").to be_detected_as_a UncompressedUnpackStrategy (mktmpdir/"test").tap do |path|
FileUtils.touch path
end end
}
it "correctly detects ZIP files" do include_examples "UnpackStrategy::detect"
expect(TEST_FIXTURE_DIR/"cask/MyFancyApp.zip").to be_detected_as_a ZipUnpackStrategy end
end
it "correctly detects BZIP2 files" do describe P7ZipUnpackStrategy do
expect(TEST_FIXTURE_DIR/"cask/container.bz2").to be_detected_as_a Bzip2UnpackStrategy let(:path) { TEST_FIXTURE_DIR/"cask/container.7z" }
end
it "correctly detects GZIP files" do include_examples "UnpackStrategy::detect"
expect(TEST_FIXTURE_DIR/"cask/container.gz").to be_detected_as_a GzipUnpackStrategy end
end
it "correctly detects compressed TAR files" do describe XarUnpackStrategy do
expect(TEST_FIXTURE_DIR/"cask/container.tar.gz").to be_detected_as_a TarUnpackStrategy let(:path) { TEST_FIXTURE_DIR/"cask/container.xar" }
end
it "correctly detects 7-ZIP files" do include_examples "UnpackStrategy::detect"
expect(TEST_FIXTURE_DIR/"cask/container.7z").to be_detected_as_a P7ZipUnpackStrategy include_examples "#extract", children: ["container"]
end end
it "correctly detects XAR files" do describe XzUnpackStrategy do
expect(TEST_FIXTURE_DIR/"cask/container.xar").to be_detected_as_a XarUnpackStrategy let(:path) { TEST_FIXTURE_DIR/"cask/container.xz" }
end
it "correctly detects XZ files" do include_examples "UnpackStrategy::detect"
expect(TEST_FIXTURE_DIR/"cask/container.xz").to be_detected_as_a XzUnpackStrategy end
end
it "correctly detects RAR files" do describe RarUnpackStrategy do
expect(TEST_FIXTURE_DIR/"cask/container.rar").to be_detected_as_a RarUnpackStrategy let(:path) { TEST_FIXTURE_DIR/"cask/container.rar" }
end
it "correctly detects LZIP files" do include_examples "UnpackStrategy::detect"
expect(TEST_FIXTURE_DIR/"test.lz").to be_detected_as_a LzipUnpackStrategy end
end
it "correctly detects LHA files" do describe LzipUnpackStrategy do
expect(TEST_FIXTURE_DIR/"test.lha").to be_detected_as_a LhaUnpackStrategy let(:path) { TEST_FIXTURE_DIR/"test.lz" }
end
it "correctly detects Git repositories" do include_examples "UnpackStrategy::detect"
mktmpdir do |repo| end
system "git", "-C", repo, "init"
expect(repo).to be_detected_as_a GitUnpackStrategy describe LhaUnpackStrategy do
let(:path) { TEST_FIXTURE_DIR/"test.lha" }
include_examples "UnpackStrategy::detect"
end
describe JarUnpackStrategy do
let(:path) { TEST_FIXTURE_DIR/"test.jar" }
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
end end
end }
it "correctly detects Subversion repositories" do include_examples "UnpackStrategy::detect"
mktmpdir do |path| end
repo = path/"repo" end
working_copy = path/"working_copy"
system "svnadmin", "create", repo describe GzipUnpackStrategy do
system "svn", "checkout", "file://#{repo}", working_copy let(:path) { TEST_FIXTURE_DIR/"cask/container.gz" }
expect(working_copy).to be_detected_as_a SubversionUnpackStrategy 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
describe TarUnpackStrategy do
let(:path) { TEST_FIXTURE_DIR/"cask/container.tar.gz" }
include_examples "UnpackStrategy::detect"
include_examples "#extract", children: ["container"]
context "when TAR archive is corrupted" do
let(:path) {
(mktmpdir/"test.tar").tap do |path|
FileUtils.touch path
end end
end }
include_examples "UnpackStrategy::detect"
end end
end end
describe GitUnpackStrategy do describe GitUnpackStrategy do
describe "#extract" do let(:repo) {
it "correctly extracts a Subversion repository" do mktmpdir.tap do |repo|
mktmpdir do |path| system "git", "-C", repo, "init"
repo = path/"repo"
repo.mkpath FileUtils.touch repo/"test"
system "git", "-C", repo, "add", "test"
system "git", "-C", repo, "init" system "git", "-C", repo, "commit", "-m", "Add `test` file."
FileUtils.touch repo/"test"
system "git", "-C", repo, "add", "test"
system "git", "-C", repo, "commit", "-m", "Add `test` file."
unpack_dir = path/"unpack_dir"
GitUnpackStrategy.new(repo).extract(to: unpack_dir)
expect(unpack_dir.children(false).map(&:to_s)).to match_array [".git", "test"]
end
end end
end }
let(:path) { repo }
include_examples "UnpackStrategy::detect"
include_examples "#extract", children: [".git", "test"]
end end
describe SubversionUnpackStrategy do describe SubversionUnpackStrategy do
describe "#extract" do let(:repo) {
it "correctly extracts a Subversion repository" do mktmpdir.tap do |repo|
mktmpdir do |path| system "svnadmin", "create", repo
repo = path/"repo"
working_copy = path/"working_copy"
system "svnadmin", "create", repo
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."
unpack_dir = path/"unpack_dir"
SubversionUnpackStrategy.new(working_copy).extract(to: unpack_dir)
expect(unpack_dir.children(false).map(&:to_s)).to match_array ["test"]
end
end end
end }
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"
end end

View File

@ -31,7 +31,7 @@ class UnpackStrategy
magic_number = if path.directory? magic_number = if path.directory?
"" ""
else else
File.binread(path, MAX_MAGIC_NUMBER_LENGTH) File.binread(path, MAX_MAGIC_NUMBER_LENGTH) || ""
end end
strategy = strategies.detect do |s| strategy = strategies.detect do |s|
@ -40,7 +40,7 @@ class UnpackStrategy
# This is so that bad files produce good error messages. # This is so that bad files produce good error messages.
strategy ||= case path.extname strategy ||= case path.extname
when ".tar.gz", ".tgz", ".tar.bz2", ".tbz", ".tar.xz", ".txz" when ".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tbz", ".tar.xz", ".txz"
TarUnpackStrategy TarUnpackStrategy
when ".zip" when ".zip"
ZipUnpackStrategy ZipUnpackStrategy
@ -58,16 +58,11 @@ class UnpackStrategy
end end
def extract(to: nil, basename: nil) def extract(to: nil, basename: nil)
basename ||= path.basename
unpack_dir = Pathname(to || Dir.pwd).expand_path unpack_dir = Pathname(to || Dir.pwd).expand_path
unpack_dir.mkpath unpack_dir.mkpath
extract_to_dir(unpack_dir, basename: basename) extract_to_dir(unpack_dir, basename: basename)
end end
private
def extract_to_dir(_unpack_dir, basename:)
raise NotImplementedError
end
end end
class DirectoryUnpackStrategy < UnpackStrategy class DirectoryUnpackStrategy < UnpackStrategy
@ -83,10 +78,6 @@ class DirectoryUnpackStrategy < UnpackStrategy
end end
class UncompressedUnpackStrategy < UnpackStrategy class UncompressedUnpackStrategy < UnpackStrategy
def self.can_extract?(path:, magic_number:)
false
end
private private
def extract_to_dir(unpack_dir, basename:) def extract_to_dir(unpack_dir, basename:)