From ea0db839b1d9b23a46bf76d07171e739c641b635 Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sun, 8 Dec 2024 18:04:51 -0300 Subject: [PATCH 1/2] Catch FileAlreadyExistsError and exit cleanly --- .github/workflows/ci.yml | 16 +------ lib/hanami/cli/commands/app/command.rb | 3 ++ .../cli/commands/app/generate/action_spec.rb | 44 +++++++++++-------- .../commands/app/generate/component_spec.rb | 41 +++++++++++------ .../commands/app/generate/migration_spec.rb | 23 +++++++--- .../commands/app/generate/operation_spec.rb | 23 +++++++--- .../cli/commands/app/generate/part_spec.rb | 27 +++++++----- .../commands/app/generate/relation_spec.rb | 26 ++++++++--- .../cli/commands/app/generate/repo_spec.rb | 23 +++++++--- .../cli/commands/app/generate/struct_spec.rb | 27 +++++++----- .../cli/commands/app/generate/view_spec.rb | 27 +++++++----- 11 files changed, 173 insertions(+), 107 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f7517aa7..6e61d9fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,21 +1,7 @@ name: ci -"on": +on: push: - paths: - - ".github/workflows/ci.yml" - - "lib/**" - - "*.gemspec" - - "spec/**" - - "Rakefile" - - "Gemfile" - - ".rubocop.yml" - pull_request: - branches: - - main - schedule: - - cron: "30 4 * * *" - create: jobs: tests: diff --git a/lib/hanami/cli/commands/app/command.rb b/lib/hanami/cli/commands/app/command.rb index f59f144b..c56c28a7 100644 --- a/lib/hanami/cli/commands/app/command.rb +++ b/lib/hanami/cli/commands/app/command.rb @@ -44,6 +44,9 @@ def call(*args, **opts) Hanami::Env.load super + rescue FileAlreadyExistsError => error + err.puts(error.message) + exit(1) end end diff --git a/spec/unit/hanami/cli/commands/app/generate/action_spec.rb b/spec/unit/hanami/cli/commands/app/generate/action_spec.rb index f9561e97..d6eade31 100644 --- a/spec/unit/hanami/cli/commands/app/generate/action_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/action_spec.rb @@ -4,9 +4,10 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::Action, :app do - subject { described_class.new(fs: fs, inflector: inflector, generator: generator) } + subject { described_class.new(fs: fs, inflector: inflector, generator: generator, err: err) } let(:out) { StringIO.new } + let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } let(:generator) { Hanami::CLI::Generators::App::Action.new(fs: fs, inflector: inflector) } @@ -20,6 +21,8 @@ def output out.rewind && out.read.chomp end + def error_output = err.string.chomp + shared_context "with existing files" do context "with existing route file" do it "generates action without error" do @@ -41,12 +44,13 @@ def output end end - it "raises error" do - expect { - within_application_directory do - generate_action - end - }.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/actions/#{controller}/#{action}.rb`") + it "exits with error message" do + expect do + within_application_directory { generate_action } + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/actions/#{controller}/#{action}.rb`" + end end end @@ -57,12 +61,13 @@ def output end end - it "raises error" do - expect { - within_application_directory do - generate_action - end - }.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/views/#{controller}/#{action}.rb`") + it "exits with error message" do + expect do + within_application_directory { generate_action } + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/views/#{controller}/#{action}.rb`" + end end end @@ -73,12 +78,13 @@ def output end end - it "raises error" do - expect { - within_application_directory do - generate_action - end - }.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/templates/#{controller}/#{action}.html.erb`") + it "exits with error message" do + expect do + within_application_directory { generate_action } + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/templates/#{controller}/#{action}.html.erb`" + end end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/component_spec.rb b/spec/unit/hanami/cli/commands/app/generate/component_spec.rb index 8f400e8d..21b0d80b 100644 --- a/spec/unit/hanami/cli/commands/app/generate/component_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/component_spec.rb @@ -4,9 +4,10 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::Component, :app do - subject { described_class.new(fs: fs, inflector: inflector, out: out) } + subject { described_class.new(fs: fs, inflector: inflector, out: out, err: err) } let(:out) { StringIO.new } + let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } let(:app) { Hanami.app.namespace } @@ -18,6 +19,8 @@ def output out.rewind && out.read.chomp end + def error_output = err.string.chomp + context "generating for app" do context "shallowly nested" do it "generates the component" do @@ -43,10 +46,13 @@ class SendWelcomeEmail fs.write("app/operations/send_welcome_email.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "operations.send_welcome_email") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/operations/send_welcome_email.rb`" + end end end end @@ -79,10 +85,13 @@ class SendWelcomeEmail fs.write("app/operations/user/mailing/send_welcome_email.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "operations.user.mailing.send_welcome_email") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/operations/user/mailing/send_welcome_email.rb`" + end end end end @@ -114,10 +123,13 @@ class WelcomeEmail fs.write("slices/main/renderers/welcome_email.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "renderers.welcome_email", slice: "main") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/renderers/welcome_email.rb`" + end end end end @@ -151,10 +163,13 @@ class WelcomeEmail fs.write("slices/main/renderers/user/mailing/welcome_email.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "renderers.user.mailing.welcome_email", slice: "main") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/renderers/user/mailing/welcome_email.rb`" + end end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb b/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb index 3707e076..ed8be246 100644 --- a/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb @@ -3,17 +3,20 @@ require "hanami" RSpec.describe Hanami::CLI::Commands::App::Generate::Migration, :app do - subject { described_class.new(fs: fs, inflector: inflector) } + subject { described_class.new(fs: fs, inflector: inflector, err: err) } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } let(:out) { StringIO.new } + let(:err) { StringIO.new } def output out.string.strip end + def error_output = err.string.chomp + let(:app) { Hanami.app.namespace } let(:migration_file_contents) { @@ -70,10 +73,13 @@ def output fs.write("config/db/migrate/20240713140600_create_posts.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "create_posts") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `config/db/migrate/20240713140600_create_posts.rb`" + end end end end @@ -95,10 +101,13 @@ def output fs.write("slices/main/config/db/migrate/20240713140600_create_posts.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "create_posts", slice: "main") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/config/db/migrate/20240713140600_create_posts.rb`" + end end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb b/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb index 46a1e992..bf5b78a9 100644 --- a/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true RSpec.describe Hanami::CLI::Commands::App::Generate::Operation, :app do - subject { described_class.new(fs: fs, inflector: inflector, out: out) } + subject { described_class.new(fs: fs, inflector: inflector, out: out, err: err) } let(:out) { StringIO.new } + let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } let(:app) { Hanami.app.namespace } @@ -12,6 +13,8 @@ def output out.string.chomp end + def error_output = err.string.chomp + context "generating for app" do it "generates an operation without a namespace, with a recommendation" do subject.call(name: "add_book") @@ -84,10 +87,13 @@ def call fs.write("app/admin/books/add.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "admin.books.add") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/admin/books/add.rb`" + end end end end @@ -145,10 +151,13 @@ def call fs.write("slices/main/admin/books/add.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "admin.books.add", slice: "main") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/admin/books/add.rb`" + end end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/part_spec.rb b/spec/unit/hanami/cli/commands/app/generate/part_spec.rb index 00bc74fc..fdb11514 100644 --- a/spec/unit/hanami/cli/commands/app/generate/part_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/part_spec.rb @@ -4,9 +4,10 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::Part, :app do - subject { described_class.new(fs: fs, inflector: inflector, generator: generator) } + subject { described_class.new(fs: fs, inflector: inflector, generator: generator, err: err) } let(:out) { StringIO.new } + let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } let(:generator) { Hanami::CLI::Generators::App::Part.new(fs: fs, inflector: inflector) } @@ -17,6 +18,8 @@ def output out.rewind && out.read.chomp end + def error_output = err.string.chomp + context "generating for app" do context "without base part" do it "generates base part and the part" do @@ -66,11 +69,12 @@ class User < Test::Views::Part end end - it "raises error" do - within_application_directory do - expect { - subject.call(name: "user") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + it "exits with error message" do + expect do + within_application_directory { subject.call(name: "user") } + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/views/parts/user.rb`" end end end @@ -233,11 +237,12 @@ class User < Main::Views::Part end end - it "raises error" do - within_application_directory do - expect { - subject.call(name: "user", slice: "main") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + it "exits with error message" do + expect do + within_application_directory { subject.call(name: "user", slice: "main") } + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/views/parts/user.rb`" end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb b/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb index 669816ec..7d36499b 100644 --- a/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb @@ -1,13 +1,17 @@ # frozen_string_literal: true RSpec.describe Hanami::CLI::Commands::App::Generate::Relation, "#call", :app_integration do - subject { described_class.new(inflector: inflector, out: out) } + subject { described_class.new(inflector: inflector, out: out, err: err) } let(:inflector) { Dry::Inflector.new } let(:out) { StringIO.new } + let(:err) { StringIO.new } + def output = out.string + def error_output = err.string.chomp + before do with_directory(@dir = make_tmp_directory) do write "config/app.rb", <<~RUBY @@ -120,9 +124,13 @@ class Books < TestApp::DB::Relation write "app/relations/books.rb", "existing content" end - it "raises error" do - expect { subject.call(name: "books") } - .to raise_error(Hanami::CLI::FileAlreadyExistsError) + it "exits with error message" do + expect do + subject.call(name: "books") + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/relations/books.rb`" + end end end end @@ -195,9 +203,13 @@ class Drafts < Main::DB::Relation write "slices/main/relations/books.rb", "existing content" end - it "raises error" do - expect { subject.call(name: "books", slice: "main") } - .to raise_error(Hanami::CLI::FileAlreadyExistsError) + it "exits with error message" do + expect do + subject.call(name: "books", slice: "main") + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/relations/books.rb`" + end end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb b/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb index a15163bd..02c8bba4 100644 --- a/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true RSpec.describe Hanami::CLI::Commands::App::Generate::Repo, :app do - subject { described_class.new(fs: fs, inflector: inflector, out: out) } + subject { described_class.new(fs: fs, inflector: inflector, out: out, err: err) } let(:out) { StringIO.new } + let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } let(:app) { Hanami.app.namespace } @@ -12,6 +13,8 @@ def output out.string end + def error_output = err.string.chomp + context "generating for app" do describe "without namespace" do it "generates a repo and singularizes name properly" do @@ -98,10 +101,13 @@ class HardcoverRepo < Test::DB::Repo fs.write("app/repos/book_repo.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "books") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/repos/book_repo.rb`" + end end end end @@ -152,10 +158,13 @@ class DraftRepo < Main::DB::Repo fs.write("slices/main/repos/book_repo.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "books", slice: "main") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/repos/book_repo.rb`" + end end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb b/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb index 490914cd..538aa8c1 100644 --- a/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb @@ -1,16 +1,17 @@ # frozen_string_literal: true RSpec.describe Hanami::CLI::Commands::App::Generate::Struct, :app do - subject { described_class.new(fs: fs, inflector: inflector, out: out) } + subject { described_class.new(fs: fs, inflector: inflector, out: out, err: err) } let(:out) { StringIO.new } + let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } let(:app) { Hanami.app.namespace } - def output - out.string.chomp - end + def output = out.string.chomp + + def error_output = err.string.chomp context "generating for app" do it "generates a struct without a namespace" do @@ -78,10 +79,13 @@ class Hardcover < Test::DB::Struct fs.write("app/structs/book/published/hardcover.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "book/published/hardcover") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/structs/book/published/hardcover.rb`" + end end end end @@ -132,10 +136,13 @@ class DraftBook < Main::DB::Struct fs.write("slices/main/structs/book/draft_book.rb", "existing content") end - it "raises error" do - expect { + it "exits with error message" do + expect do subject.call(name: "book.draft_book", slice: "main") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/structs/book/draft_book.rb`" + end end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/view_spec.rb b/spec/unit/hanami/cli/commands/app/generate/view_spec.rb index b761979f..c9f90ea3 100644 --- a/spec/unit/hanami/cli/commands/app/generate/view_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/view_spec.rb @@ -4,9 +4,10 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::View, :app do - subject { described_class.new(fs: fs, inflector: inflector, generator: generator) } + subject { described_class.new(fs: fs, inflector: inflector, generator: generator, err: err) } let(:out) { StringIO.new } + let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } let(:generator) { Hanami::CLI::Generators::App::View.new(fs: fs, inflector: inflector) } @@ -17,6 +18,8 @@ def output out.rewind && out.read.chomp end + def error_output = err.string.chomp + # it "raises error if action name doesn't respect the convention" do # expect { # subject.call(name: "foo") @@ -99,11 +102,12 @@ class Index < Test::View end end - it "raises error" do - within_application_directory do - expect { - subject.call(name: "users.index") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + it "exits with error message" do + expect do + within_application_directory { subject.call(name: "users.index") } + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `app/views/users/index.rb`" end end end @@ -152,11 +156,12 @@ class Index < Main::View end end - it "raises error" do - within_application_directory do - expect { - subject.call(name: "users.index", slice: "main") - }.to raise_error(Hanami::CLI::FileAlreadyExistsError) + it "exits with error message" do + expect do + within_application_directory { subject.call(name: "users.index", slice: "main") } + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq "Cannot overwrite existing file: `slices/main/views/users/index.rb`" end end end From 937290c7194c67d4edb4179ee7f5fbce89f4aeba Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sun, 8 Dec 2024 18:46:08 -0300 Subject: [PATCH 2/2] Change FileAlreadyExistsError message --- .github/workflows/ci.yml | 16 ++++++++++++- lib/hanami/cli/errors.rb | 8 +++++-- .../cli/commands/app/generate/action_spec.rb | 18 +++++++++----- .../commands/app/generate/component_spec.rb | 24 ++++++++++++------- .../commands/app/generate/migration_spec.rb | 12 ++++++---- .../commands/app/generate/operation_spec.rb | 12 ++++++---- .../cli/commands/app/generate/part_spec.rb | 12 ++++++---- .../commands/app/generate/relation_spec.rb | 12 ++++++---- .../cli/commands/app/generate/repo_spec.rb | 12 ++++++---- .../cli/commands/app/generate/struct_spec.rb | 12 ++++++---- .../cli/commands/app/generate/view_spec.rb | 12 ++++++---- 11 files changed, 105 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6e61d9fd..f7517aa7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,21 @@ name: ci -on: +"on": push: + paths: + - ".github/workflows/ci.yml" + - "lib/**" + - "*.gemspec" + - "spec/**" + - "Rakefile" + - "Gemfile" + - ".rubocop.yml" + pull_request: + branches: + - main + schedule: + - cron: "30 4 * * *" + create: jobs: tests: diff --git a/lib/hanami/cli/errors.rb b/lib/hanami/cli/errors.rb index 05b8718f..40be0bd9 100644 --- a/lib/hanami/cli/errors.rb +++ b/lib/hanami/cli/errors.rb @@ -46,8 +46,12 @@ def initialize(path) # @api public class FileAlreadyExistsError < Error - def initialize(path) - super("Cannot overwrite existing file: `#{path}`") + ERROR_MESSAGE = <<~ERROR.chomp + The file `%{file_path}` could not be generated because it already exists in your application. + ERROR + + def initialize(file_path) + super(ERROR_MESSAGE % {file_path:}) end end diff --git a/spec/unit/hanami/cli/commands/app/generate/action_spec.rb b/spec/unit/hanami/cli/commands/app/generate/action_spec.rb index d6eade31..3cbf192d 100644 --- a/spec/unit/hanami/cli/commands/app/generate/action_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/action_spec.rb @@ -38,9 +38,11 @@ def error_output = err.string.chomp end context "with existing action file" do + let(:file_path) { "app/actions/#{controller}/#{action}.rb" } + before do within_application_directory do - fs.write("app/actions/#{controller}/#{action}.rb", "") + fs.write(file_path, "") end end @@ -49,15 +51,17 @@ def error_output = err.string.chomp within_application_directory { generate_action } end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/actions/#{controller}/#{action}.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end context "with existing view file" do + let(:file_path) { "app/views/#{controller}/#{action}.rb" } + before do within_application_directory do - fs.write("app/views/#{controller}/#{action}.rb", "") + fs.write(file_path, "") end end @@ -66,15 +70,17 @@ def error_output = err.string.chomp within_application_directory { generate_action } end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/views/#{controller}/#{action}.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end context "with existing template file" do + let(:file_path) { "app/templates/#{controller}/#{action}.html.erb" } + before do within_application_directory do - fs.write("app/templates/#{controller}/#{action}.html.erb", "") + fs.write(file_path, "") end end @@ -83,7 +89,7 @@ def error_output = err.string.chomp within_application_directory { generate_action } end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/templates/#{controller}/#{action}.html.erb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/component_spec.rb b/spec/unit/hanami/cli/commands/app/generate/component_spec.rb index 21b0d80b..310a7044 100644 --- a/spec/unit/hanami/cli/commands/app/generate/component_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/component_spec.rb @@ -42,8 +42,10 @@ class SendWelcomeEmail end context "with existing file" do + let(:file_path) { "app/operations/send_welcome_email.rb" } + before do - fs.write("app/operations/send_welcome_email.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -51,7 +53,7 @@ class SendWelcomeEmail subject.call(name: "operations.send_welcome_email") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/operations/send_welcome_email.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -81,8 +83,10 @@ class SendWelcomeEmail end context "with existing file" do + let(:file_path) { "app/operations/user/mailing/send_welcome_email.rb" } + before do - fs.write("app/operations/user/mailing/send_welcome_email.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -90,7 +94,7 @@ class SendWelcomeEmail subject.call(name: "operations.user.mailing.send_welcome_email") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/operations/user/mailing/send_welcome_email.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -119,8 +123,10 @@ class WelcomeEmail end context "with existing file" do + let(:file_path) { "slices/main/renderers/welcome_email.rb" } + before do - fs.write("slices/main/renderers/welcome_email.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -128,7 +134,7 @@ class WelcomeEmail subject.call(name: "renderers.welcome_email", slice: "main") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/renderers/welcome_email.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -159,8 +165,10 @@ class WelcomeEmail end context "with existing file" do + let(:file_path) { "slices/main/renderers/user/mailing/welcome_email.rb" } + before do - fs.write("slices/main/renderers/user/mailing/welcome_email.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -168,7 +176,7 @@ class WelcomeEmail subject.call(name: "renderers.user.mailing.welcome_email", slice: "main") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/renderers/user/mailing/welcome_email.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb b/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb index ed8be246..3fd202ec 100644 --- a/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb @@ -69,8 +69,10 @@ def error_output = err.string.chomp end context "with existing file" do + let(:file_path) { "config/db/migrate/20240713140600_create_posts.rb" } + before do - fs.write("config/db/migrate/20240713140600_create_posts.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -78,7 +80,7 @@ def error_output = err.string.chomp subject.call(name: "create_posts") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `config/db/migrate/20240713140600_create_posts.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -96,9 +98,11 @@ def error_output = err.string.chomp end context "with existing file" do + let(:file_path) { "slices/main/config/db/migrate/20240713140600_create_posts.rb" } + context "with existing file" do before do - fs.write("slices/main/config/db/migrate/20240713140600_create_posts.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -106,7 +110,7 @@ def error_output = err.string.chomp subject.call(name: "create_posts", slice: "main") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/config/db/migrate/20240713140600_create_posts.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb b/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb index bf5b78a9..065caae9 100644 --- a/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb @@ -83,8 +83,10 @@ def call end context "with existing file" do + let(:file_path) { "app/admin/books/add.rb" } + before do - fs.write("app/admin/books/add.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -92,7 +94,7 @@ def call subject.call(name: "admin.books.add") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/admin/books/add.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -146,9 +148,11 @@ def call end context "with existing file" do + let(:file_path) { "slices/main/admin/books/add.rb" } + before do fs.mkdir("slices/main") - fs.write("slices/main/admin/books/add.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -156,7 +160,7 @@ def call subject.call(name: "admin.books.add", slice: "main") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/admin/books/add.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/part_spec.rb b/spec/unit/hanami/cli/commands/app/generate/part_spec.rb index fdb11514..44d33265 100644 --- a/spec/unit/hanami/cli/commands/app/generate/part_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/part_spec.rb @@ -63,9 +63,11 @@ class User < Test::Views::Part end context "with existing file" do + let(:file_path) { "app/views/parts/user.rb" } + before do within_application_directory do - fs.write("app/views/parts/user.rb", "existing content") + fs.write(file_path, "existing content") end end @@ -74,7 +76,7 @@ class User < Test::Views::Part within_application_directory { subject.call(name: "user") } end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/views/parts/user.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -230,10 +232,12 @@ class User < Main::Views::Part end context "with existing file" do + let(:file_path) { "slices/main/views/parts/user.rb" } + before do within_application_directory do fs.mkdir("slices/main") - fs.write("slices/main/views/parts/user.rb", "existing content") + fs.write(file_path, "existing content") end end @@ -242,7 +246,7 @@ class User < Main::Views::Part within_application_directory { subject.call(name: "user", slice: "main") } end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/views/parts/user.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb b/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb index 7d36499b..03bd4368 100644 --- a/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb @@ -120,8 +120,10 @@ class Books < TestApp::DB::Relation end context "with existing file" do + let(:file_path) { "app/relations/books.rb" } + before do - write "app/relations/books.rb", "existing content" + write file_path, "existing content" end it "exits with error message" do @@ -129,7 +131,7 @@ class Books < TestApp::DB::Relation subject.call(name: "books") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/relations/books.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -199,8 +201,10 @@ class Drafts < Main::DB::Relation end context "with existing file" do + let(:file_path) { "slices/main/relations/books.rb" } + before do - write "slices/main/relations/books.rb", "existing content" + write file_path, "existing content" end it "exits with error message" do @@ -208,7 +212,7 @@ class Drafts < Main::DB::Relation subject.call(name: "books", slice: "main") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/relations/books.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb b/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb index 02c8bba4..f42bdb3a 100644 --- a/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb @@ -97,8 +97,10 @@ class HardcoverRepo < Test::DB::Repo end context "with existing file" do + let(:file_path) { "app/repos/book_repo.rb" } + before do - fs.write("app/repos/book_repo.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -106,7 +108,7 @@ class HardcoverRepo < Test::DB::Repo subject.call(name: "books") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/repos/book_repo.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -154,8 +156,10 @@ class DraftRepo < Main::DB::Repo end context "with existing file" do + let(:file_path) { "slices/main/repos/book_repo.rb" } + before do - fs.write("slices/main/repos/book_repo.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -163,7 +167,7 @@ class DraftRepo < Main::DB::Repo subject.call(name: "books", slice: "main") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/repos/book_repo.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb b/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb index 538aa8c1..550b90bb 100644 --- a/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb @@ -75,8 +75,10 @@ class Hardcover < Test::DB::Struct end context "with existing file" do + let(:file_path) { "app/structs/book/published/hardcover.rb" } + before do - fs.write("app/structs/book/published/hardcover.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -84,7 +86,7 @@ class Hardcover < Test::DB::Struct subject.call(name: "book/published/hardcover") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/structs/book/published/hardcover.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -132,8 +134,10 @@ class DraftBook < Main::DB::Struct end context "with existing file" do + let(:file_path) { "slices/main/structs/book/draft_book.rb" } + before do - fs.write("slices/main/structs/book/draft_book.rb", "existing content") + fs.write(file_path, "existing content") end it "exits with error message" do @@ -141,7 +145,7 @@ class DraftBook < Main::DB::Struct subject.call(name: "book.draft_book", slice: "main") end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/structs/book/draft_book.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end diff --git a/spec/unit/hanami/cli/commands/app/generate/view_spec.rb b/spec/unit/hanami/cli/commands/app/generate/view_spec.rb index c9f90ea3..b246c33c 100644 --- a/spec/unit/hanami/cli/commands/app/generate/view_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/view_spec.rb @@ -96,9 +96,11 @@ class Index < Test::View end context "with existing file" do + let(:file_path) { "app/views/users/index.rb" } + before do within_application_directory do - fs.write("app/views/users/index.rb", "existing content") + fs.write(file_path, "existing content") end end @@ -107,7 +109,7 @@ class Index < Test::View within_application_directory { subject.call(name: "users.index") } end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `app/views/users/index.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end @@ -149,10 +151,12 @@ class Index < Main::View end context "with existing file" do + let(:file_path) { "slices/main/views/users/index.rb" } + before do within_application_directory do fs.mkdir("slices/main") - fs.write("slices/main/views/users/index.rb", "existing content") + fs.write(file_path, "existing content") end end @@ -161,7 +165,7 @@ class Index < Main::View within_application_directory { subject.call(name: "users.index", slice: "main") } end.to raise_error SystemExit do |exception| expect(exception.status).to eq 1 - expect(error_output).to eq "Cannot overwrite existing file: `slices/main/views/users/index.rb`" + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} end end end