Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Undefine methods before redefining them #1584

Merged
merged 1 commit into from
Jun 6, 2014

Conversation

sikachu
Copy link
Contributor

@sikachu sikachu commented Jun 6, 2014

If you run RSpec test suite and turn on Ruby warnings (ruby -w), there are several warnings about method redefined:

  • /lib/rspec/core/configuration.rb:191: warning: method redefined; discarding old pattern=
  • /lib/rspec/core/example.rb:210: warning: method redefined; discarding old run
  • /lib/rspec/core/example.rb:204: warning: previous definition of run was here

By undef these method first we can silence those warnings from Ruby.

@@ -185,6 +185,7 @@ def output_stream=(value)
# @macro add_setting
# Load files matching this pattern (default: `'**/*_spec.rb'`)
add_setting :pattern
undef :pattern=
Copy link
Member

Choose a reason for hiding this comment

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

Why is this one after the add_setting call. Does this one actually fix the warning?

Copy link
Member

Choose a reason for hiding this comment

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

It fixes it because there's a definition of pattern= below. I think there's a better approach, though: rather than using add_setting, use define_reader. add_setting is for cases where we want both the reader and writer methods generated but we want only the reader here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, right, I was confused a bit. Let me update this; just attr_reader make sense.

Copy link
Member

Choose a reason for hiding this comment

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

We actually need define_reader to be used -- our settings need that for the semantics of spec_helper settings being overridable when CLI options are used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, gotcha. I read that one wrong the first time.

@fables-tales
Copy link
Member

@sikachu Can you make a little gist which highlights these warnings? It'd be good to see them in action. Also, it'd be nice if we could add some tests to this pull request to check that those warnings are actually no longer happening. Can you take a look at that?

@myronmarston
Copy link
Member

Thanks @sikachu. We definitely want to be warning free. We already run our specs with --warnings but I'm realizing that takes effect after these files are loaded, which is why we haven't noticed these warnings.

What do you think about setting $VERBOSE = true at the top of script/rspec_with_simplecov? That should help us notice these sorts of things.

@myronmarston
Copy link
Member

@samphippen -- here's the output you asked about:

✗ script/rspec_with_simplecov spec
/Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/configuration.rb:191: warning: method redefined; discarding old pattern=
/Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:210: warning: method redefined; discarding old run
/Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:204: warning: previous definition of run was here
Invalid gemspec in [/Users/myron/.gem/ruby/1.9.2/specifications/ZenTest-4.9.5.gemspec]: Illformed requirement ["< 3.0, >= 1.8"]
Run options:
  include {:focus=>true}
  exclude {:ruby=>#<Proc:./spec/spec_helper.rb:139>}

All examples were filtered out; ignoring {:focus=>true}
...............................................................................*............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Pending:
  RSpec::Core::Metadata for an example points :example_group to the same hash object as other examples in the same group
    # Cannot maintain this and provide full `:example_group` backwards compatibility (see GH #1490):(
    # ./spec/rspec/core/metadata_spec.rb:111

Finished in 2.75 seconds (files took 0.97433 seconds to load)
1244 examples, 0 failures, 1 pending

Randomized with seed 9244

@@ -207,6 +207,7 @@ class Procsy
Proc.public_instance_methods(false).each do |name|
define_method(name) { |*a, &b| @proc.__send__(name, *a, &b) }
end
undef :run
Copy link
Member

Choose a reason for hiding this comment

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

I think a better solution to this is to add a next if name.to_sym == :run in the Example.public_instance_methods(false).each do |name| loop. Then there's no reason to undef it.

@sikachu
Copy link
Contributor Author

sikachu commented Jun 6, 2014

Yeah, I'll fix those issues and add $VERBOSE = true in that rspec_with_simplecov file. I'm not sure how or if I should add a test for this, though.

@myronmarston
Copy link
Member

I'm not sure how or if I should add a test for this, though.

Don't worry about it.

If you run RSpec test suite and turn on Ruby warnings (`ruby -w`), there
are several warnings about method redefined:

* /lib/rspec/core/configuration.rb:191: warning: method redefined;
  discarding old pattern=
* /lib/rspec/core/example.rb:210: warning: method redefined; discarding
  old run
* /lib/rspec/core/example.rb:204: warning: previous definition of run
  was here

By `undef` these method first we can silence those warnings from Ruby.
@sikachu
Copy link
Contributor Author

sikachu commented Jun 6, 2014

@samphippen @myronmarston code is updated. How does it look now?

@@ -201,7 +201,9 @@ class Procsy
attr_reader :example

Example.public_instance_methods(false).each do |name|
define_method(name) { |*a, &b| @example.__send__(name, *a, &b) }
unless name.to_sym == :run
Copy link
Member

Choose a reason for hiding this comment

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

I tend to prefer using a next guard clause, but that's just subjective preference :). Not a merge blocker.

@myronmarston
Copy link
Member

Looks good, I'll merge when green.

myronmarston added a commit that referenced this pull request Jun 6, 2014
Undefine methods before redefining them
@myronmarston myronmarston merged commit b6dd27a into rspec:master Jun 6, 2014
@myronmarston
Copy link
Member

Thanks, @sikachu!

myronmarston added a commit that referenced this pull request Jun 6, 2014
myronmarston added a commit that referenced this pull request Jun 6, 2014
MatheusRich pushed a commit to MatheusRich/rspec-core that referenced this pull request Oct 30, 2020
Undefine methods before redefining them
MatheusRich pushed a commit to MatheusRich/rspec-core that referenced this pull request Oct 30, 2020
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
[ci skip]

---
This commit was imported from rspec/rspec-core@4df38c6.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants