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

Enable logging non-string - fix #1410 #1412

Merged
merged 4 commits into from
Apr 28, 2020

Conversation

vincent-psarga
Copy link
Contributor

Summary

See #1410

Details

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Refactor (code change that does not change external functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I've added tests for my code
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Copy link
Contributor

@olleolleolle olleolleolle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@@ -108,8 +108,7 @@ def embed(file, mime_type, _label = 'Screenshot')
end

def log(message)
raise Cucumber::LogTypeInvalid unless message.is_a?(String)
attach(message.dup, 'text/x.cucumber.log+plain')
attach(message.to_s.dup, 'text/x.cucumber.log+plain')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the dup?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise we log the object itself and it will display its value at display time, not at log time (there a test showing that and it fails without the dup)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why we used to do message.dup, but I don't think we need message.to_s.dup. I think message.to_s is sufficient.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Me, a reader, thought this was because the string would be used in a mutable context.)

Copy link
Contributor Author

@vincent-psarga vincent-psarga Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though the to_s would be enough too. In fact it's enough for non-string objects (obviously :D ) but when using a String we need the dup.

An example from the tests:

        describe 'when modifying the printed variable after the call to puts' do
          define_feature <<-FEATURE
        Feature: Banana party

          Scenario: Monkey eats banana
            When puts is called twice for the same variable
          FEATURE

          define_steps do
            When(/^puts is called twice for the same variable$/) do
              foo = String.new('a')
              puts foo
              foo.upcase!
              puts foo
            end
          end

This test fails without the dup (we output A\nA instead of a\nA). I suspect that String.to_s do not return a duplicate but imply itself (also note, the test still uses puts instead of log, but that'll be removed once we removed the override of puts by log)

And @olleolleolle you're not wrong, it's just that the string might be mutated between the moment it I logged and the moment we actually display it.

dsl.Given(/Loud/) do
log ['Not', 1, 'string']
end
expect { run_step 'Loud' }.to raise_exception(Cucumber::LogTypeInvalid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Cucumber::LogTypeInvalid still used? Should it be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed

@vincent-psarga vincent-psarga merged commit 3c96f89 into master Apr 28, 2020
@luke-hill luke-hill deleted the 1410-enable-logging-objects branch August 22, 2023 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants