Skip to content

Fix NoMethodError that is raised when the environment variable CUCUMBER_COLORS is defined#1641

Merged
aurelien-reeves merged 6 commits intocucumber:mainfrom
s2k:main
May 31, 2022
Merged

Fix NoMethodError that is raised when the environment variable CUCUMBER_COLORS is defined#1641
aurelien-reeves merged 6 commits intocucumber:mainfrom
s2k:main

Conversation

@s2k
Copy link
Copy Markdown
Member

@s2k s2k commented May 21, 2022

Description

This fixes a NoMethodError exception that is raised, when the environment variable CUCUMBER_COLORS is set, by allowing apply_custom_colors to be called right after its definition in Cucumber::Formatter::ANSIColor.

Defining the method as a class method of the module allows it to be called in the module itself.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

s2k added 2 commits May 21, 2022 21:37
Signed-off-by: Stephan Kämper <the.tester@seasidetesting.com>
Signed-off-by: Stephan Kämper <the.tester@seasidetesting.com>
@s2k s2k requested review from aurelien-reeves and mattwynne May 22, 2022 08:35
@s2k
Copy link
Copy Markdown
Member Author

s2k commented May 22, 2022

As a question for the reviewers: I wonder if there should be additional tests for this. While the existing tests had to be adapted to reflect the code change, I'm not (entirely) sure whether this is sufficient (given that the bug slipped through with the tests). Even though

> CUCUMBER_COLORS='passed=red,bold' cucumber --help
[…]/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/cucumber-8.0.0/lib/cucumber/formatter/ansicolor.rb:95:in `<module:ANSIColor>': undefined method `apply_custom_colors' for Cucumber::Formatter::ANSIColor:Module (NoMethodError)

      apply_custom_colors(ENV['CUCUMBER_COLORS']) if ENV['CUCUMBER_COLORS']
      ^^^^^^^^^^^^^^^^^^^

would have revealed the bug, I'm not sure this would be a good fit for another scenario in a feature file.

Copy link
Copy Markdown
Contributor

@aurelien-reeves aurelien-reeves left a comment

Choose a reason for hiding this comment

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

Thanks for your PR @s2k :)

Would it be possible to keep an instance methods - which would just call the class one - in order to prevent anything to break for users who would have hacked their cucumber?

And thus we could make sure to test the two methods actually

Also there is a call to apply_custom_colors right after it has been defined. I am surprised you did not had to change that call actually, but I may have missed something with ruby here 🤔

…e specs

Signed-off-by: Stephan Kämper <the.tester@seasidetesting.com>
@s2k
Copy link
Copy Markdown
Member Author

s2k commented May 23, 2022

Thanks for the feedback.

I had to change the definition of apply_custom_colors to be a class method, in order to be able to call it right after the definition. That's because the context is the module.

Here's an example:

module Example
  def for_instances(arg)
    p arg
  end

  def self.for_class(arg)
    p arg
  end

  begin
    for_class('Calling the class method is OK')
  rescue NoMethodError => e
    p e
  end
  
  begin
    for_instances('The instance method is not available in the class')
  rescue NoMethodError => e
    puts "That didn't work"
    puts "Message:"
    puts "*" * 40
    puts e.message
    puts "*" * 40
  end
end

Running it yields:

"Calling the class method is OK"
That didn't work
Message:
****************************************
undefined method `for_instances' for Example:Module

    for_instances('The instance method is not available in the class')
    ^^^^^^^^^^^^^
Did you mean?  for_class
****************************************

In other words: self inside a class (or module) definition is the class/module itself. To be able to call a method within the class/module definition it needs to be a class method (similar to attr_reader etc.).

I hesitate to provide an instance method, because the method is not meant to be called on an instance (but provided it anyway 🙂), so we can discuss it.

@s2k s2k requested a review from aurelien-reeves May 23, 2022 09:58
@aurelien-reeves
Copy link
Copy Markdown
Contributor

Thanks for the update.

@luke-hill what is your opinion on this?

Co-authored-by: Aurélien Reeves <aurelien.reeves@smartbear.com>
Copy link
Copy Markdown
Contributor

@luke-hill luke-hill left a comment

Choose a reason for hiding this comment

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

If we have a mixture of things we need (or you want to permit both the class method and the instance method variant), your better bet is simply manipulating yourself from instance to class by doing self.class.foo

@aurelien-reeves aurelien-reeves self-requested a review May 24, 2022 09:05
@s2k
Copy link
Copy Markdown
Member Author

s2k commented May 24, 2022

No worries, I'll just need to revert 1560c6e, right?

@aurelien-reeves
Copy link
Copy Markdown
Contributor

No worries, I'll just need to revert 1560c6e, right?

Yes, in addition to the last comments from @luke-hill

@luke-hill luke-hill dismissed their stale review May 26, 2022 15:40

For now this can be ignored.

@s2k s2k requested review from luke-hill and removed request for luke-hill and mattwynne May 26, 2022 19:59
@s2k
Copy link
Copy Markdown
Member Author

s2k commented May 29, 2022

OK, as far as I can see we could now merge this PR, no?

@aurelien-reeves aurelien-reeves merged commit f94be9c into cucumber:main May 31, 2022
@diabolo
Copy link
Copy Markdown

diabolo commented Dec 6, 2022

This needs releasing. The bug with CUCUMBER_COLORS is in the current release 8.0.0 7 months after it has been fixed in cucumber:main. Can we get a point release out to address this?

@mattwynne mattwynne added this to the 8.1.0 milestone Jan 3, 2023
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.

6 participants