Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve
- Add `--no-dump` option to `hanami db rollback`, to skip dumping the database structure after rolling back. (@katafrakt in #459)
- Add `repository` as an alias for the `repo` generator, so `hanami generate repository` works. (@sandbergja in #456)
- Respect the `HANAMI_APP_PATH` env var when detecting the app's file and the hanami gem is not installed. (@sandbergja in #477)
- Add `Files#create_or_skip`, for generators (such as the test generators in hanami-rspec and hanami-minitest) to leave an existing file alone and report it as `skip`, or overwrite it when `force: true` is given. (@timriley in #481)

### Changed

Expand Down
18 changes: 18 additions & 0 deletions lib/hanami/cli/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ def create(path, *content, force: false)
write(path, *content)
end

# Writes the file, unless it already exists and `force` is false. In that case, leaves the
# file alone and reports it as skipped.
#
# Use this in place of {#create} for files that are useful but not essential to generate,
# such as tests, so that an existing file does not stop the generator.
#
# @api private
def create_or_skip(path, *content, force: false)
return skipped(path) if exist?(path) && !force

write(path, *content)
end

# @since 2.0.0
# @api private
def write(path, *content)
Expand Down Expand Up @@ -96,10 +109,15 @@ def created(path)
report(Styles::SUCCESS["create"], path)
end

def skipped(path)
report(Styles::MUTED["skip"], path)
end

# Prints a line for a change to a path, with its verb right-aligned so the paths line up:
#
# create app/actions/books/index.rb
# update config/routes.rb
# skip spec/actions/books/index_spec.rb
def report(verb, path)
path = path.to_s

Expand Down
40 changes: 40 additions & 0 deletions spec/unit/hanami/cli/files_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

RSpec.describe Hanami::CLI::Files do
subject(:fs) { described_class.new(memory: true, stdout:) }

let(:stdout) { StringIO.new }

describe "#create_or_skip" do
context "when the file does not exist" do
it "creates the file" do
fs.create_or_skip("spec/thing_spec.rb", "new content")

expect(fs.read("spec/thing_spec.rb")).to eq "new content"
expect(stdout.string).to eq " create spec/thing_spec.rb\n"
end
end

context "when the file exists" do
before do
fs.write("spec/thing_spec.rb", "existing content")
stdout.truncate(0)
stdout.rewind
end

it "leaves the file alone and reports it as skipped" do
fs.create_or_skip("spec/thing_spec.rb", "new content")

expect(fs.read("spec/thing_spec.rb")).to eq "existing content"
expect(stdout.string).to eq " skip spec/thing_spec.rb\n"
end

it "overwrites the file when force is true" do
fs.create_or_skip("spec/thing_spec.rb", "new content", force: true)

expect(fs.read("spec/thing_spec.rb")).to eq "new content"
expect(stdout.string).to eq " update spec/thing_spec.rb\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess technically it "replace" or "overwrite" ("updated" is when we add routes to routes.rb). But I guess this is a different change, outside of the scope of this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's a good callout!

end
end
end
end