Skip to content

Conversation

@stephannv
Copy link
Contributor

Exits with status 1 when a command tries to generate an already existing file.

Fixes #181

This is a rebased version of #279

Copy link
Member

@timriley timriley left a comment

Choose a reason for hiding this comment

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

This is looking great, thanks @stephannv! :)

I've got one comment/suggestion regarding the testing. Let me know what you think and then we can merge this either now or after that change.

Comment on lines +53 to +59
it "exits with error message" do
expect do
within_application_directory { generate_action }
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
Copy link
Member

Choose a reason for hiding this comment

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

All these tests for file conflict handling, spread across each generator, get pretty repetitious. With you having the experience of authoring these changes, do you have thoughts about keeping the tests more maintainable?

I think my preference would be not to worry about testing for conflicts in every generator spec, instead add e.g. a spec/unit/hanami/cli/commands/app/generate/file_conflict_handling_spec.rb that demonstrates the conflict handling for just one of the generators, which would be sufficient for us to know the code is doing what it needs.

This feels appropriate given that code is centralised into a single place, rather than something that actually requires special handling inside every generator class.

It would also make each generator spec simpler and more focused on its happy path.

Is this something you'd be happy to do, either here or in a follow-up PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, @timriley. I will do it in a next PR since these tests are already in the codebase currently, I just adapted them to work with the new exception. I will explore some alternatives and propose some changes.

Copy link
Member

Choose a reason for hiding this comment

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

This sounds good to me, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Regarding the testing approach I suggested above, we do have an existing example in this repo already: spec/unit/hanami/cli/commands/app/generate/slice_detection_spec.rb. This is a feature that applies to all generate commands, but it would have been wasteful to test the identical thing for every single one of them, so we extracted it to a dedicated file and tested it just once.

@timriley timriley merged commit 1e2bd49 into hanami:main Sep 24, 2025
8 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.

Prevent file overwriting

2 participants