Skip to content

Stop stubbing a private generator method to silence Thor command noise - #1821

Merged
nbulaj merged 1 commit into
doorkeeper-gem:mainfrom
55728:55728/fix/generator-spec-thor-command-noise
May 19, 2026
Merged

Stop stubbing a private generator method to silence Thor command noise#1821
nbulaj merged 1 commit into
doorkeeper-gem:mainfrom
55728:55728/fix/generator-spec-thor-command-noise

Conversation

@55728

@55728 55728 commented May 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Running the test suite prints this line twice:

Could not find command "no_previous_refresh_token_column?".

It comes from spec/generators/previous_refresh_token_generator_spec.rb, which stubs the private method PreviousRefreshTokenGenerator#no_previous_refresh_token_column? via allow_any_instance_of(...).to receive(:no_previous_refresh_token_column?).

Root cause

It is not a bug in Doorkeeper's generator code, and it never happens when a real user runs rails generate doorkeeper:previous_refresh_token. It is an interaction between RSpec and Thor that only occurs in the spec:

  1. allow_any_instance_of(...).to receive(:no_previous_refresh_token_column?) (re)defines that method on the generator class.
  2. Defining a method on a Thor class fires Thor::Base.method_added, which registers the (transiently public) method as a runnable command.
  3. run_generatorThor::Group#invoke_all iterates over every registered command and calls Thor::Command#run.
  4. By then the stub has the original (private) visibility, so Thor::Command#run hits the private_method?(instance) branch and calls handle_no_command_error, which prints Could not find command "no_previous_refresh_token_column?".

So the trigger is "stub a private method on a Thor generator via allow_any_instance_of". Neither Thor nor RSpec is strictly misbehaving; the test just shouldn't stub the method under test on a Thor class.

Fix

Stub the underlying database dependency that no_previous_refresh_token_column? wraps (ActiveRecord::Base.connection.column_exists?) instead of the private method itself. No method is (re)defined on the generator class, so Thor never registers a spurious command and the noise disappears.

Bonus: the "column already exists" example now stubs column_exists? => true explicitly instead of relying on and_call_original against the test schema, making it deterministic.

Behavior change

None for the generator. Test-only change; the same scenarios are covered:

  • column missing → migration generated (with the expected version specifier)
  • column already present → no migration generated

Testing

  • bundle exec rspec spec/generators/previous_refresh_token_generator_spec.rb
    → 2 examples, 0 failures.
  • Full suite: bundle exec rspec → 1295 examples, 0 failures, and the Could not find command "..." line no longer appears anywhere in the output.
  • bundle exec rubocop spec/generators/previous_refresh_token_generator_spec.rb
    → no offenses.

Stubbing the private PreviousRefreshTokenGenerator#no_previous_refresh_token_column?
method via allow_any_instance_of transiently (re)defines it on the Thor
generator class. That triggers Thor's method_added hook, which registers
it as a runnable command; when Thor::Group#invoke_all later runs every
command, Thor::Command#run finds the method private and emits

    Could not find command "no_previous_refresh_token_column?".

to stdout during the test suite.

Stub the underlying ActiveRecord column check
(ActiveRecord::Base.connection.column_exists?) instead, so no method is
(re)defined on the generator class. The noise is gone, the test intent is
preserved, and the 'column already exists' case is now deterministic
rather than depending on the test schema state.

@nbulaj nbulaj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cool one, thanks!

@55728
55728 force-pushed the 55728/fix/generator-spec-thor-command-noise branch from b7db7df to d988a01 Compare May 19, 2026 14:14
@nbulaj
nbulaj merged commit 04cf40f into doorkeeper-gem:main May 19, 2026
22 checks passed
@nbulaj

nbulaj commented May 19, 2026

Copy link
Copy Markdown
Member

🙇

@nbulaj nbulaj added this to the 5.9.1 milestone May 19, 2026
@55728

55728 commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks a lot 😊

@55728
55728 deleted the 55728/fix/generator-spec-thor-command-noise branch May 19, 2026 22:46
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.

2 participants