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

Bugfix: Prevent generators from overwriting files #274

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

maxemitchell
Copy link

This adds a create method that raises an error if the file already exists.

This allows the write method to still be used by specs and when we want to override (e.g. for editing the routes file).

Addresses #181

@@ -88,7 +151,7 @@ class #{inflector.camelize(action)} < #{inflector.camelize(app)}::View
EXPECTED

expect(fs.read("app/templates/#{controller}/#{action}.html.erb")).to eq(template_file)
expect(output).to include("Created app/views/#{controller}/#{action}.rb")
expect(output).to include("Created app/templates/#{controller}/#{action}.html.erb")
Copy link
Author

Choose a reason for hiding this comment

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

this was looking for the wrong file

Choose a reason for hiding this comment

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

nice find

Copy link
Member

Choose a reason for hiding this comment

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

Nice find indeed! How did this even pass before? 😅

@@ -305,47 +375,45 @@ class #{inflector.camelize(action)} < #{inflector.camelize(app)}::View
EXPECTED

expect(fs.read("app/templates/#{controller}/#{action}.html.erb")).to eq(template_file)
expect(output).to include("Created app/views/#{controller}/#{action}.rb")
expect(output).to include("Created app/templates/#{controller}/#{action}.html.erb")
Copy link
Author

Choose a reason for hiding this comment

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

same thing here with wrong file

Comment on lines +382 to +383
context "with nested action name" do
let(:context) { Hanami::CLI::Generators::App::ActionContext.new(inflector, app, nil, %w[api users], "thing") }
Copy link
Author

Choose a reason for hiding this comment

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

added a context for this

end
end

context "deeply nested action" do
Copy link
Author

Choose a reason for hiding this comment

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

this deeply nested action context was duplicate

@maxemitchell maxemitchell marked this pull request as ready for review November 14, 2024 23:18
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.

Thank you for taking care of this, @maxemitchell! Especially continuing the work on it even after RubyConf finished — truly heroic! 🤩

The changes here all look good to me 👍🏼

I'd be happy to merge this as is, because this PR alone is already a valuable improvement on what we're doing now.

Before I do that, I'd like to take this moment to get your thoughts on how this PR fits into the bigger picture.


With this PR in its current state, if a user ran a generator that encountered an existing file, the hanami CLI would crash with the new FileAlreadyExistsError and print out a stack trace. On the plus side, this prevents unwanted file overwriting, but the rest of the user experience here doesn't feel so good. The error and stacktrace will be overwhelming, and it may be hard for the user to identify the cause for the CLI command crashing.

I think a better experience here might be fore our generate command classes to catch this FileAlreadyExistsError and print a nicer error message before then cleanly exiting (with a non-zero exit code). This might be something we can put into a single base command class for all the generators, so that we don't have to implement it in every single command class.

What do you think about this? Any thoughts on how else this might be made nicer? And now the big question: is this something you'd also be interested in working on? 😇 I'd be very happy for it to be in a follow-up PR so that we can merge this one and stay focused on smaller, incremental changes. Let me know!

@@ -88,7 +151,7 @@ class #{inflector.camelize(action)} < #{inflector.camelize(app)}::View
EXPECTED

expect(fs.read("app/templates/#{controller}/#{action}.html.erb")).to eq(template_file)
expect(output).to include("Created app/views/#{controller}/#{action}.rb")
expect(output).to include("Created app/templates/#{controller}/#{action}.html.erb")
Copy link
Member

Choose a reason for hiding this comment

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

Nice find indeed! How did this even pass before? 😅

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.

3 participants