From 3e9cbeabfe7d329b17cb602ebba65b8264c733e6 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Wed, 17 Mar 2021 08:54:09 +0000 Subject: [PATCH] Merge pull request #2487 from rspec/fix-method-name-truncation-for-multibyte-strings Fix method name truncation for multibyte strings (redux) --- Changelog.md | 8 ++++++++ lib/rspec/rails/example/system_example_group.rb | 2 +- .../rspec/rails/example/system_example_group_spec.rb | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 3c8b7c6be5..84d8436d46 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,11 @@ +### Development +[Full Changelog](https://github.com/rspec/rspec-rails/compare/v5.0.0...main) + +Bug Fixes: + +* Limit multibyte example descriptions when used in system tests for #method_name + which ends up as screenshot names etc. (@y-yagi, #2405, #2487) + ### 5.0.0 / 2021-03-09 [Full Changelog](https://github.com/rspec/rspec-rails/compare/v4.1.1...v5.0.0) diff --git a/lib/rspec/rails/example/system_example_group.rb b/lib/rspec/rails/example/system_example_group.rb index 59c5606cca..e0f395bce0 100644 --- a/lib/rspec/rails/example/system_example_group.rb +++ b/lib/rspec/rails/example/system_example_group.rb @@ -41,7 +41,7 @@ def method_name @method_name ||= [ self.class.name.underscore, RSpec.current_example.description.underscore - ].join("_").tr(CHARS_TO_TRANSLATE.join, "_")[0...200] + "_#{rand(1000)}" + ].join("_").tr(CHARS_TO_TRANSLATE.join, "_").byteslice(0...200).scrub("") + "_#{rand(1000)}" end # Delegates to `Rails.application`. diff --git a/spec/rspec/rails/example/system_example_group_spec.rb b/spec/rspec/rails/example/system_example_group_spec.rb index 533bf79d62..cd5328e2bf 100644 --- a/spec/rspec/rails/example/system_example_group_spec.rb +++ b/spec/rspec/rails/example/system_example_group_spec.rb @@ -15,6 +15,18 @@ module RSpec::Rails expect(example.send(:method_name)).to start_with('method_name') end end + + it "handles long method names which include unicode characters" do + group = + RSpec::Core::ExampleGroup.describe do + include SystemExampleGroup + end + + example = group.new + allow(example.class).to receive(:name) { "really long unicode example name - #{'あ'*100}" } + + expect(example.send(:method_name).bytesize).to be <= 210 + end end describe '#driver' do