Skip to content

Commit

Permalink
Remove ZipInspection, manage tempfiles better
Browse files Browse the repository at this point in the history
Inspecting with an external tool was nice, but it has run its course
  • Loading branch information
julik committed Feb 29, 2024
1 parent cfb053b commit 69a6024
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 49 deletions.
6 changes: 0 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@

require_relative "support/read_monitor"
require_relative "support/managed_tempfile"
require_relative "support/zip_inspection"
require_relative "support/chunked_encoding"
require_relative "support/allocate_under_matcher"

RSpec.configure do |config|
config.include ZipInspection
config.include ChunkedEncoding

config.after :each do
ManagedTempfile.prune!
end

config.after :suite do
$stderr << $zip_inspection_buf.string if $zip_inspection_buf
end
end
15 changes: 0 additions & 15 deletions spec/support/zip_inspection.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/zip_kit/output_enumerator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe ZipKit::OutputEnumerator do
it "returns parts of the ZIP file when called via #each with immediate yield" do
output_buf = Tempfile.new("output")
output_buf = ManagedTempfile.new("output")

file_body = Random.new.bytes(1024 * 1024 + 8981)

Expand Down Expand Up @@ -34,7 +34,7 @@
end

it "returns parts of the ZIP file when called using an Enumerator" do
output_buf = Tempfile.new("output")
output_buf = ManagedTempfile.new("output")

file_body = Random.new.bytes(1024 * 1024 + 8981)

Expand Down
2 changes: 1 addition & 1 deletion spec/zip_kit/rack_body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe ZipKit::RackBody do
it "is usable as a Rack response body, supports each()" do
output_buf = Tempfile.new("output")
output_buf = ManagedTempfile.new("output")

file_body = Random.new.bytes(1024 * 1024 + 8981)

Expand Down
2 changes: 1 addition & 1 deletion spec/zip_kit/remote_io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

describe "#read" do
before :each do
@buf = Tempfile.new("simulated-http")
@buf = ManagedTempfile.new("simulated-http")
@buf.binmode
5.times { @buf << Random.new.bytes(1024 * 1024 * 3) }
@buf.rewind
Expand Down
8 changes: 4 additions & 4 deletions spec/zip_kit/remote_uncap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@

it 'returns an array of remote entries that can be used to fetch the segments \
from within the ZIP' do
payload1 = Tempfile.new "payload1"
payload1 = ManagedTempfile.new "payload1"
payload1 << Random.new.bytes((1024 * 1024 * 5) + 10)
payload1.flush
payload1.rewind

payload2 = Tempfile.new "payload2"
payload2 = ManagedTempfile.new "payload2"
payload2 << Random.new.bytes(1024 * 1024 * 3)
payload2.flush
payload2.rewind
Expand Down Expand Up @@ -79,11 +79,11 @@
end

it "can cope with an empty file within the zip" do
payload1 = Tempfile.new "payload1"
payload1 = ManagedTempfile.new "payload1"
payload1.flush
payload1.rewind

payload2 = Tempfile.new "payload2"
payload2 = ManagedTempfile.new "payload2"
payload2 << Random.new.bytes(1024)
payload2.flush
payload2.rewind
Expand Down
27 changes: 7 additions & 20 deletions spec/zip_kit/streamer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def stream_with_just_write.write(bytes)
end

it "can write and then read the block-deflated files" do
f = Tempfile.new("raw")
f = ManagedTempfile.new("raw")
f.binmode

rewind_after(f) do
Expand All @@ -123,7 +123,7 @@ def stream_with_just_write.write(bytes)
end

# Perform the zipping
zip_file = Tempfile.new("z")
zip_file = ManagedTempfile.new("z")
zip_file.binmode

described_class.open(zip_file) do |zip|
Expand All @@ -147,13 +147,11 @@ def stream_with_just_write.write(bytes)
expect(per_filename["compressed-file.bin"].bytesize).to eq(f.size)
expect(Digest::SHA1.hexdigest(per_filename["compressed-file.bin"])).to \
eq(Digest::SHA1.hexdigest(f.read))

inspect_zip_with_external_tool(zip_file.path)
end

it "can write and then read an empty directory" do
# Perform the zipping
zip_file = Tempfile.new("z")
zip_file = ManagedTempfile.new("z")
zip_file.binmode

described_class.open(zip_file) do |zip|
Expand All @@ -172,8 +170,6 @@ def stream_with_just_write.write(bytes)
end

expect(per_filename["Tunes/"].bytesize).to eq(154)

inspect_zip_with_external_tool(zip_file.path)
end

it "can write the data descriptor and updates the last entry as well" do
Expand All @@ -197,7 +193,7 @@ def stream_with_just_write.write(bytes)
end

it "archives files which can then be read using the usual means with Rubyzip" do
zip_buf = Tempfile.new("zipp")
zip_buf = ManagedTempfile.new("zipp")
zip_buf.binmode
output_io = double("IO")

Expand Down Expand Up @@ -242,17 +238,10 @@ def stream_with_just_write.write(bytes)

expect(per_filename["first-file.bin"].unpack("C*")).to eq(raw_file1.unpack("C*"))
expect(per_filename["second-file.bin"].unpack("C*")).to eq(raw_file2.unpack("C*"))

wd = Dir.pwd
Dir.mktmpdir do |td|
Dir.chdir(td)
inspect_zip_with_external_tool(zip_buf.path)
end
Dir.chdir(wd)
end

it "sets the general-purpose flag for entries with UTF8 names" do
zip_buf = Tempfile.new("zipp")
zip_buf = ManagedTempfile.new("zipp")
zip_buf.binmode

# Generate a couple of random files
Expand Down Expand Up @@ -416,8 +405,6 @@ def stream_with_just_write.write(bytes)
readback.force_encoding(Encoding::BINARY)
expect(readback[0..10]).to eq(File.binread(__dir__ + "/war-and-peace.txt")[0..10])
end

inspect_zip_with_external_tool(tf.path)
end

it "can create a valid ZIP archive without any files" do
Expand Down Expand Up @@ -556,7 +543,7 @@ def stream_with_just_write.write(bytes)
end

it "supports automatic mode selection using a heuristic" do
zip_file = Tempfile.new
zip_file = ManagedTempfile.new
rng = Random.new(42)
repeating_string = "many many delicious, compressible words"
n_writes = 24000
Expand Down Expand Up @@ -609,7 +596,7 @@ def stream_with_just_write.write(bytes)
end

it "rolls back entries where writes have failed" do
zip_file = Tempfile.new("zipp")
zip_file = ManagedTempfile.new("zipp")
described_class.open(zip_file) do |zip|
begin
zip.write_deflated_file("deflated.txt", modification_time: Time.new(2018, 1, 1)) do |sink|
Expand Down

0 comments on commit 69a6024

Please sign in to comment.