Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be more strict with frozen string literals #18

Merged
merged 9 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,46 @@ env:
BUNDLE_PATH: vendor/bundle

jobs:
test:
name: Tests and Lint
test_baseline_ruby:
name: "Tests (Ruby 2.6 baseline)"
runs-on: ubuntu-22.04
strategy:
matrix:
ruby:
- '2.6'
- '3.4'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
ruby-version: '2.6'
bundler-cache: true
- name: "Tests"
run: bundle exec rspec --backtrace --fail-fast

test_newest_ruby:
name: "Tests (Ruby 3.4 with frozen string literals)"
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.4.1'
bundler-cache: true
- name: "Tests" # Make the test suite hard-crash on frozen string literal violations
env:
RUBYOPT: "--enable=frozen-string-literal --debug=frozen-string-literal"
run: "bundle exec rspec --backtrace --fail-fast"

lint_baseline_ruby: # We need to use syntax appropriate for the minimum supported Ruby version
name: Lint (Ruby 2.6 syntax)
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '2.6'
bundler-cache: true
- name: "Lint"
run: bundle exec rake standard
4 changes: 2 additions & 2 deletions spec/zip_kit/block_deflate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def tag_deflated(deflated_string, raw_string)

it "does not write the end marker" do
input_string = "compressible" * (1024 * 1024 * 10)
output_string = ""
output_string = +""

described_class.deflate_in_blocks(StringIO.new(input_string), StringIO.new(output_string))
expect(output_string).not_to be_empty
Expand All @@ -109,7 +109,7 @@ def tag_deflated(deflated_string, raw_string)

it "returns the number of bytes written" do
input_string = "compressible" * (1024 * 1024 * 10)
output_string = ""
output_string = +""

num_bytes = described_class.deflate_in_blocks(StringIO.new(input_string),
StringIO.new(output_string))
Expand Down
4 changes: 2 additions & 2 deletions spec/zip_kit/block_write_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
end

it "can write in all possible encodings, even if the strings are frozen" do
accum_string = ""
accum_string = +""
adapter = described_class.new { |s| accum_string << s }

adapter << "hello"
Expand All @@ -57,7 +57,7 @@

it "does not change the encoding of source strings" do
hello = "hello".encode(Encoding::UTF_8)
accum_string = "".force_encoding(Encoding::BINARY)
accum_string = (+"").force_encoding(Encoding::BINARY)
adapter = described_class.new { |s| accum_string << s }
adapter << hello
expect(accum_string.encoding).to eq(Encoding::BINARY)
Expand Down
2 changes: 1 addition & 1 deletion spec/zip_kit/file_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def write_end_of_central_directory(**kwargs)

entry = entries.first

readback = ""
readback = +""
reader = entry.extractor_from(zipfile)
readback << reader.extract(10) until reader.eof?

Expand Down
4 changes: 2 additions & 2 deletions spec/zip_kit/streamer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def stream_with_just_write.write(bytes)
expect(fake_writer).to receive(:write_central_directory_file_header)
expect(fake_writer).to receive(:write_end_of_central_directory)

described_class.open("", writer: fake_writer) do |zip|
described_class.open(+"", writer: fake_writer) do |zip|
zip.write_deflated_file("stored.txt") do |sink|
sink << File.read(__dir__ + "/war-and-peace.txt")
end
Expand Down Expand Up @@ -273,7 +273,7 @@ def stream_with_just_write.write(bytes)

# Rubyzip does not properly set the encoding of the entries it reads
expect(second_entry.gp_flags).to eq(2_048)
expect(second_entry.name).to eq("второй-файл.bin".force_encoding(Encoding::BINARY))
expect(second_entry.name).to eq((+"второй-файл.bin").force_encoding(Encoding::BINARY))
end
end

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

describe ZipKit::WriteAndTell do
it "maintains the count of bytes written" do
adapter = described_class.new("")
adapter = described_class.new(+"")
expect(adapter.tell).to be_zero

adapter << "hello"
Expand All @@ -19,16 +19,16 @@
[12, 123, 0, 3].pack("C*")
]

buf = "превед".force_encoding(Encoding::BINARY)
buf = (+"превед").force_encoding(Encoding::BINARY)
writer = described_class.new(buf)
strs.each { |s| writer << s }
expect(writer.tell).to eq(79)
expect(buf.bytesize).to eq(91) # It already contained some bytes
end

it "does not change the encoding of the source string" do
str = "текста кусок".force_encoding(Encoding::UTF_8)
buf = "превед".force_encoding(Encoding::BINARY)
str = (+"текста кусок").force_encoding(Encoding::UTF_8)
buf = (+"превед").force_encoding(Encoding::BINARY)
writer = described_class.new(buf)
writer << str
expect(buf.bytesize).to eq(35)
Expand Down Expand Up @@ -60,7 +60,7 @@ def stream_with_just_write.write(bytes)
end

it "advances the internal pointer using advance_position_by" do
str = ""
str = +""

adapter = described_class.new(str)
expect(adapter.tell).to be_zero
Expand Down
2 changes: 1 addition & 1 deletion spec/zip_kit/zip_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def seek_to_start_of_signature(signature)
end

it "writes out the custom comment" do
buf = ""
buf = +""
comment = "Ohai mate"
subject.write_end_of_central_directory(io: buf,
start_of_central_directory_location: 9_091_211,
Expand Down