Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

In generators #gsub_file should not silently alter files.

#3978

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).

Reported by Sam Goldstein · February 17th, 2010 @ 04:04 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. José Valim
    José Valim
    • 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?

    February 21st, 2010 @ 12:51 PM

  2. Aslak Hellesøy
    Aslak Hellesøy

    Thanks José - I wasn't aware of #yes? - I'll use that.

    February 21st, 2010 @ 01:23 PM

  3. José Valim
  4. Sam Goldstein
    Sam Goldstein

    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 if gsub_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.

    February 21st, 2010 @ 06:24 PM

  5. José Valim
    José Valim

    gsub_file does output. Unless you ask not to.

    February 21st, 2010 @ 07:23 PM