Skip to content

Commit

Permalink
Consider the multibyte value in the method name of system test
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
y-yagi committed Nov 26, 2020
1 parent 934276b commit 85533a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/rspec/rails/example/system_example_group.rb
Expand Up @@ -38,10 +38,18 @@ def passed?

# @private
def method_name
@method_name ||= [
self.class.name.underscore,
RSpec.current_example.description.underscore
].join("_").tr(CHARS_TO_TRANSLATE.join, "_")[0...200] + "_#{rand(1000)}"
@method_name ||= begin
m = [
self.class.name.underscore,
RSpec.current_example.description.underscore
].join("_").tr(CHARS_TO_TRANSLATE.join, "_")

if RbConfig::CONFIG["host_os"] =~ /linux/
m.byteslice(0...200).scrub("") + "_#{rand(1000)}"
else
m[0...200] + "_#{rand(1000)}"
end
end
end

# Delegates to `Rails.application`.
Expand Down
11 changes: 11 additions & 0 deletions spec/rspec/rails/example/system_example_group_spec.rb
Expand Up @@ -16,6 +16,17 @@ module RSpec::Rails
expect(example.send(:method_name)).to start_with('method_name')
end
end

it 'slices long method name' do
group = RSpec::Core::ExampleGroup.describe ActionPack do
include SystemExampleGroup
end

example = group.new
example_class_mock = double('name' => "#{'あ'*100}")
allow(example).to receive(:class).and_return(example_class_mock)
expect(example.send(:method_name).bytesize).to be <= 210
end
end

describe '#driver' do
Expand Down

0 comments on commit 85533a1

Please sign in to comment.