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

Ability to have gsub_file error if it didn't gsub anything #874

Open
G-Rath opened this issue Feb 19, 2024 · 0 comments · May be fixed by #877
Open

Ability to have gsub_file error if it didn't gsub anything #874

G-Rath opened this issue Feb 19, 2024 · 0 comments · May be fixed by #877

Comments

@G-Rath
Copy link

G-Rath commented Feb 19, 2024

I recently discovered that gsub_file does not error when it does not match; this feels like a bit of a footgun to me.

I've worked around this using this utility function to ensure the file contents has changed:

def gsub_file!(path, flag, *args, &block)
  content = File.binread(path)

  gsub_file(path, flag, *args, &block)

  raise StandardError, "the contents of #{path} did not change!" if content == File.binread(path)
end

However, I think it would be a good addition to thor itself - I'm thinking maybe gsub_file "", "", "", error_on_no_change: true and then an alias of gsub_file!?

G-Rath added a commit to ackama/rails-template that referenced this issue Mar 8, 2024
…o longer doing anything (#533)

So it turns out that `gsub_file` does not actually check if it matched
anything and so we have a few misc. changes silently not being applied
due to changes in Rails 7.1.

This addresses that by switching us to use `gsub_file!` which reads the
file into memory before it's gsub'd and then compares the results to
make sure it actually changed.

I've opened rails/thor#874 to add this to Thor
itself
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 a pull request may close this issue.

1 participant