This project is archived and is in readonly mode.
In generators #gsub_file should not silently alter files.
Reported by Sam Goldstein | February 17th, 2010 @ 04:04 PM
This issue was originally filed in cucumber-rails, but it appears the root issue is actually in Rails' built in generators. The use of #gsub_file in generator code will overwrite/alter files without providing any indication to the user that it has done so. Possibly it should call #force_file_collision.
Here is a link to the original cucumber-rails ticket discussing this, and some excerpts from it:
http://github.com/aslakhellesoy/cucumber-rails/issues/issue/15
=============
samg: Running script/generate cucumber rewrites my database.yml file, yet gives no indication that it has done this.
... which was difficult to track down since I had no indication that the generate script would touch database.yml. The resulting error was difficult to trace since database.yml is frequently ignored in SCM; you can't rely on git to tell you it has been altered.
... The generate script should ask before it touches database.yml ...
=============
aslakhellesoy: The generator is using Rails' generator API, more specifically #gsub_file. This method doesn't ask the user about overwrite [Ynaqdh] (the #force_file_collision? method) although I agree it should. The right place to address this is in Rails itself. Please send them a patch or create an issue for it (if it doesn't exist already).
Comments and changes to this ticket
-
José Valim February 21st, 2010 @ 12:51 PM
- State changed from new to invalid
gsub_file should not ask for file collision. file_collision is done when you are creating a file in a place where another already exists. For instance, scaffold uses gsub_file to add routes to your config/routes.rb. If it asked every time if you want to modify it or not, it would be painful.
If Cucumber is going risky operation, they should ask their users before:
if yes?("Do you want to change your database.yml?") gsub_file ... end
Does it make sense?
-
Aslak Hellesøy February 21st, 2010 @ 01:23 PM
Thanks José - I wasn't aware of #yes? - I'll use that.
-
José Valim February 21st, 2010 @ 01:33 PM
You're welcome! Here is a list of methods that comes from Thor:
http://rdoc.info/rdoc/wycats/thor/blob/4f5da62d135aa10122e8c51f0c0d...
And here are the ones that Rails adds:
http://github.com/rails/rails/tree/master/railties/lib/rails/genera...
Regards!
-
Sam Goldstein February 21st, 2010 @ 06:24 PM
I agree that users (e.g. cucumber) should deal with the prompt/confirmation logic for
gsub_file
since this logic may vary case to case. It would be nice ifgsub_file
simply output that a change has taken place. Something like:changed config/database.yml
This would help end users of generators know that a file had been changes, especially in cases (like database.yml) where it may be excluded from SCM. It doesn't make sense for each generator to have to roll their own code to tell the user that a file has changed.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>