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

Add report duplicates config option for let it be #293

Conversation

lHydra
Copy link
Contributor

@lHydra lHydra commented May 17, 2024

What is the purpose of this pull request?

Add report_duplicates config option to avoid surprises when overriding let_it_be in a nested context

Is there anything you'd like reviewers to focus on?

I added a stub method, to track overrides (define_method("let_it_be_#{identifier}") {}).
But somehow it looks doubtful, maybe I should use another approach?

Also I decided to use the method that comes with rspec (RSpec.warn_with) instead of Kernel.warn.

Also, I'm not sure if the default option report_duplicates = :warn is necessary?

Checklist

  • I've added tests for this change
  • I've added a Changelog entry
  • I've updated a documentation

Closes #291

@lHydra lHydra closed this May 17, 2024
@lHydra lHydra reopened this May 17, 2024
end

def report_duplicates
@report_duplicates ||= :warn
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think, the default should be false meaning that we don't need any warnings (current behaviour).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -130,6 +155,9 @@ def let_it_be(identifier, **options, &block)
super()
end
end

# Used to to track method overrides
define_method("let_it_be_#{identifier}") {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't like this idea. Let's try to use the information we already have to detect if there is already a let_it_be-defined method.

For example, we can check that the instance_method_defined?(identifier) and then check the location of the definition, and if it's the current file—it's defined by let_it_be. Something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I didn't realize that we can check the source file where the method is defined

Done

@lHydra lHydra requested a review from palkan May 22, 2024 17:46
Copy link
Collaborator

@palkan palkan 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 👍 Thanks!

Please, rebase, and we can merge it.

@lHydra lHydra force-pushed the add-report-duplicates-config-option-for-let_it_be branch from 1dd85e9 to 4806b1f Compare June 3, 2024 13:09
@lHydra
Copy link
Contributor Author

lHydra commented Jun 3, 2024

@palkan some weird test fails in CI... Locally there is no such problem. Can you help me, please?

Copy link
Collaborator

@palkan palkan left a comment

Choose a reason for hiding this comment

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

Left a few more comments.

CI failures are unrelated. They caused by the recent Timecop release: travisjeffery/timecop#419

end

def report_duplicates
@report_duplicates ||= false
Copy link
Collaborator

Choose a reason for hiding this comment

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

This memoization is useless; we can skip setting to false and use attr_reader instead

Copy link
Contributor Author

@lHydra lHydra Jun 5, 2024

Choose a reason for hiding this comment

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

removed excess memoization

@@ -32,6 +34,19 @@ def register_modifier(key, on: :let, &block)
LetItBe.modifiers[key] = Modifier.new(on, block)
end

def report_duplicates=(value)
value = value.to_sym
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about report_duplicates = false? (For example, to temporary disable reporting).

I think, we can remove the value validation altogether and be less defensive 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.

What about report_duplicates = false? (For example, to temporary disable reporting).

We could omit this value, which would be equiv

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the validation

@lHydra lHydra force-pushed the add-report-duplicates-config-option-for-let_it_be branch from 4806b1f to dd99311 Compare June 5, 2024 13:20
Copy link
Collaborator

@palkan palkan left a comment

Choose a reason for hiding this comment

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

Perfect! Thank you!

@palkan palkan merged commit dcb835c into test-prof:master Jun 5, 2024
17 checks passed
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.

Warn on let_it_be duplications
2 participants