Skip to content

Commit 02bbff0

Browse files
authoredFeb 20, 2025··
Be more strict with frozen string literals (#18)
There were no runtime warnings already, but this adds the config in CI to fail the test suite if frozen string mutations are attempted.
1 parent 9f86d72 commit 02bbff0

7 files changed

+44
-21
lines changed
 

‎.github/workflows/ci.yml

+31-8
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,46 @@ env:
77
BUNDLE_PATH: vendor/bundle
88

99
jobs:
10-
test:
11-
name: Tests and Lint
10+
test_baseline_ruby:
11+
name: "Tests (Ruby 2.6 baseline)"
1212
runs-on: ubuntu-22.04
13-
strategy:
14-
matrix:
15-
ruby:
16-
- '2.6'
17-
- '3.4'
1813
steps:
1914
- name: Checkout
2015
uses: actions/checkout@v4
2116
- name: Setup Ruby
2217
uses: ruby/setup-ruby@v1
2318
with:
24-
ruby-version: ${{ matrix.ruby }}
19+
ruby-version: '2.6'
2520
bundler-cache: true
2621
- name: "Tests"
2722
run: bundle exec rspec --backtrace --fail-fast
23+
24+
test_newest_ruby:
25+
name: "Tests (Ruby 3.4 with frozen string literals)"
26+
runs-on: ubuntu-22.04
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@v4
30+
- name: Setup Ruby
31+
uses: ruby/setup-ruby@v1
32+
with:
33+
ruby-version: '3.4.1'
34+
bundler-cache: true
35+
- name: "Tests" # Make the test suite hard-crash on frozen string literal violations
36+
env:
37+
RUBYOPT: "--enable=frozen-string-literal --debug=frozen-string-literal"
38+
run: "bundle exec rspec --backtrace --fail-fast"
39+
40+
lint_baseline_ruby: # We need to use syntax appropriate for the minimum supported Ruby version
41+
name: Lint (Ruby 2.6 syntax)
42+
runs-on: ubuntu-22.04
43+
steps:
44+
- name: Checkout
45+
uses: actions/checkout@v4
46+
- name: Setup Ruby
47+
uses: ruby/setup-ruby@v1
48+
with:
49+
ruby-version: '2.6'
50+
bundler-cache: true
2851
- name: "Lint"
2952
run: bundle exec rake standard

‎spec/zip_kit/block_deflate_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def tag_deflated(deflated_string, raw_string)
100100

101101
it "does not write the end marker" do
102102
input_string = "compressible" * (1024 * 1024 * 10)
103-
output_string = ""
103+
output_string = +""
104104

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

110110
it "returns the number of bytes written" do
111111
input_string = "compressible" * (1024 * 1024 * 10)
112-
output_string = ""
112+
output_string = +""
113113

114114
num_bytes = described_class.deflate_in_blocks(StringIO.new(input_string),
115115
StringIO.new(output_string))

‎spec/zip_kit/block_write_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
end
3434

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

3939
adapter << "hello"
@@ -57,7 +57,7 @@
5757

5858
it "does not change the encoding of source strings" do
5959
hello = "hello".encode(Encoding::UTF_8)
60-
accum_string = "".force_encoding(Encoding::BINARY)
60+
accum_string = (+"").force_encoding(Encoding::BINARY)
6161
adapter = described_class.new { |s| accum_string << s }
6262
adapter << hello
6363
expect(accum_string.encoding).to eq(Encoding::BINARY)

‎spec/zip_kit/file_reader_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def write_end_of_central_directory(**kwargs)
138138

139139
entry = entries.first
140140

141-
readback = ""
141+
readback = +""
142142
reader = entry.extractor_from(zipfile)
143143
readback << reader.extract(10) until reader.eof?
144144

‎spec/zip_kit/streamer_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def stream_with_just_write.write(bytes)
7272
expect(fake_writer).to receive(:write_central_directory_file_header)
7373
expect(fake_writer).to receive(:write_end_of_central_directory)
7474

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

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

‎spec/zip_kit/write_and_tell_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

88
adapter << "hello"
@@ -19,16 +19,16 @@
1919
[12, 123, 0, 3].pack("C*")
2020
]
2121

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

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

6262
it "advances the internal pointer using advance_position_by" do
63-
str = ""
63+
str = +""
6464

6565
adapter = described_class.new(str)
6666
expect(adapter.tell).to be_zero

‎spec/zip_kit/zip_writer_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def seek_to_start_of_signature(signature)
526526
end
527527

528528
it "writes out the custom comment" do
529-
buf = ""
529+
buf = +""
530530
comment = "Ohai mate"
531531
subject.write_end_of_central_directory(io: buf,
532532
start_of_central_directory_location: 9_091_211,

0 commit comments

Comments
 (0)
Please sign in to comment.