Skip to content

Commit b3c9fad

Browse files
committed
Consider the multibyte value in the method name of system test
`String#[]` returns a value that considers multibyte value. But some file systems use byte for maximum filename length. So if applications use that file system and multibyte value to a method name, currently check doesn't work expected. This PR fixes to use `String#byteslice` instead of `String#[]`. Also, added `String#scrub` to avoid generating an invalid byte sequence.
1 parent 934276b commit b3c9fad

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/rspec/rails/example/system_example_group.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ def passed?
3838

3939
# @private
4040
def method_name
41-
@method_name ||= [
42-
self.class.name.underscore,
43-
RSpec.current_example.description.underscore
44-
].join("_").tr(CHARS_TO_TRANSLATE.join, "_")[0...200] + "_#{rand(1000)}"
41+
@method_name ||= begin
42+
m = [
43+
self.class.name.underscore,
44+
RSpec.current_example.description.underscore
45+
].join("_").tr(CHARS_TO_TRANSLATE.join, "_")
46+
47+
if RbConfig::CONFIG["host_os"] =~ /linux/
48+
m.byteslice(0...200).scrub("") + "_#{rand(1000)}"
49+
else
50+
m[0...200] + "_#{rand(1000)}"
51+
end
52+
end
4553
end
4654

4755
# Delegates to `Rails.application`.

spec/rspec/rails/example/system_example_group_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ module RSpec::Rails
1616
expect(example.send(:method_name)).to start_with('method_name')
1717
end
1818
end
19+
20+
it 'slices long method name' do
21+
group = RSpec::Core::ExampleGroup.describe ActionPack do
22+
include SystemExampleGroup
23+
end
24+
25+
example = group.new
26+
example_class_mock = double('name' => 'あ'*100)
27+
allow(example).to receive(:class).and_return(example_class_mock)
28+
expect(example.send(:method_name).bytesize).to be <= 210
29+
end
1930
end
2031

2132
describe '#driver' do

0 commit comments

Comments
 (0)