diff --git a/CHANGELOG.md b/CHANGELOG.md index 27d4fa76..d8ca0c9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/hanami/cli/files.rb b/lib/hanami/cli/files.rb index 67ec2f53..9183cf16 100644 --- a/lib/hanami/cli/files.rb +++ b/lib/hanami/cli/files.rb @@ -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) @@ -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 diff --git a/spec/unit/hanami/cli/files_spec.rb b/spec/unit/hanami/cli/files_spec.rb new file mode 100644 index 00000000..722aa8c7 --- /dev/null +++ b/spec/unit/hanami/cli/files_spec.rb @@ -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" + end + end + end +end