From a22d2634126595bc53eeca598dde14153cbbc52d Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Wed, 24 Sep 2025 18:48:46 -0300 Subject: [PATCH] Refactor file conflict tests --- .../cli/commands/app/generate/part_spec.rb | 21 +---- .../cli/commands/app/generate/action_spec.rb | 64 +------------- .../commands/app/generate/component_spec.rb | 73 +--------------- .../generate/file_conflict_handling_spec.rb | 28 ++++++ .../commands/app/generate/migration_spec.rb | 41 +-------- .../commands/app/generate/operation_spec.rb | 40 +-------- .../cli/commands/app/generate/part_spec.rb | 44 +--------- .../commands/app/generate/relation_spec.rb | 39 +-------- .../cli/commands/app/generate/repo_spec.rb | 39 +-------- .../cli/commands/app/generate/struct_spec.rb | 39 +-------- .../cli/commands/app/generate/view_spec.rb | 87 +------------------ 11 files changed, 38 insertions(+), 477 deletions(-) create mode 100644 spec/unit/hanami/cli/commands/app/generate/file_conflict_handling_spec.rb diff --git a/spec/integration/hanami/cli/commands/app/generate/part_spec.rb b/spec/integration/hanami/cli/commands/app/generate/part_spec.rb index b7136202..b22284cd 100644 --- a/spec/integration/hanami/cli/commands/app/generate/part_spec.rb +++ b/spec/integration/hanami/cli/commands/app/generate/part_spec.rb @@ -5,14 +5,11 @@ RSpec.describe "Hanami generate part integration", :app do let(:fs) { Hanami::CLI::Files.new(memory: false, out: out) } let(:out) { StringIO.new } - let(:err) { StringIO.new } subject(:command) do - Hanami::CLI::Commands::App::Generate::Part.new(fs: fs, out: out, err: err) + Hanami::CLI::Commands::App::Generate::Part.new(fs: fs, out: out) end - def error_output = err.string.chomp - around do |example| Dir.mktmpdir do |dir| original_dir = Dir.pwd @@ -65,20 +62,4 @@ class Routes < Hanami::Routes expect(fs.exist?("app/views/parts/book.rb")).to be(true) end end - - context "error handling" do - let(:file_path) { "app/views/parts/existing.rb" } - - it "handles file conflicts" do - fs.mkdir("app/views/parts") - fs.write(file_path, "# existing content") - - expect do - command.call(name: "existing") - end.to raise_error SystemExit do |exception| - expect(exception.status).to eq 1 - expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - 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 111c2e27..f1026627 100644 --- a/spec/unit/hanami/cli/commands/app/generate/action_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/action_spec.rb @@ -4,10 +4,9 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::Action, :app do - subject { described_class.new(fs: fs, out: out, err: err) } + subject { described_class.new(fs: fs, out: out) } 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 } @@ -20,8 +19,6 @@ def output out.rewind && out.read.chomp end - def error_output = err.string.chomp - shared_context "with existing files" do before do allow(Hanami).to receive(:bundled?).and_call_original @@ -40,65 +37,6 @@ def error_output = err.string.chomp end end end - - context "with existing action file" do - let(:file_path) { "app/actions/#{controller}/#{action}.rb" } - - before do - within_application_directory do - fs.write(file_path, "") - end - end - - 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 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(file_path, "") - end - end - - 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 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(file_path, "") - end - end - - it "exits with error message" do - expect do - within_application_directory do - generate_action - end - end.to raise_error SystemExit do |exception| - expect(exception.status).to eq 1 - expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "generate for app, without hanami view bundled" do 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 1415894d..be33f957 100644 --- a/spec/unit/hanami/cli/commands/app/generate/component_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/component_spec.rb @@ -4,10 +4,9 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::Component, :app do - subject { described_class.new(fs: fs, out: out, err: err) } + subject { described_class.new(fs: fs, out: out) } 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 } @@ -19,8 +18,6 @@ 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 @@ -40,23 +37,6 @@ class SendWelcomeEmail expect(fs.read("app/operations/send_welcome_email.rb")).to eq(component) expect(output).to include("Created app/operations/send_welcome_email.rb") end - - context "with existing file" do - let(:file_path) { "app/operations/send_welcome_email.rb" } - - before do - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "deeply nested" do @@ -81,23 +61,6 @@ class SendWelcomeEmail expect(fs.read("app/operations/user/mailing/send_welcome_email.rb")).to eq(component) expect(output).to include("Created app/operations/user/mailing/send_welcome_email.rb") end - - context "with existing file" do - let(:file_path) { "app/operations/user/mailing/send_welcome_email.rb" } - - before do - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end end @@ -121,23 +84,6 @@ class WelcomeEmail expect(fs.read("slices/main/renderers/welcome_email.rb")).to eq(component) expect(output).to include("Created slices/main/renderers/welcome_email.rb") end - - context "with existing file" do - let(:file_path) { "slices/main/renderers/welcome_email.rb" } - - before do - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "deeply nested" do @@ -163,23 +109,6 @@ class WelcomeEmail expect(fs.read("slices/main/renderers/user/mailing/welcome_email.rb")).to eq(component) expect(output).to include("Created slices/main/renderers/user/mailing/welcome_email.rb") end - - context "with existing file" do - let(:file_path) { "slices/main/renderers/user/mailing/welcome_email.rb" } - - before do - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "with missing slice" do diff --git a/spec/unit/hanami/cli/commands/app/generate/file_conflict_handling_spec.rb b/spec/unit/hanami/cli/commands/app/generate/file_conflict_handling_spec.rb new file mode 100644 index 00000000..ae43fca0 --- /dev/null +++ b/spec/unit/hanami/cli/commands/app/generate/file_conflict_handling_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require "hanami" + +RSpec.describe "file conflict handling", :app do + subject(:cmd) { Hanami::CLI::Commands::App::Generate::View.new(fs:, out:, err:) } + + let(:out) { StringIO.new } + let(:err) { StringIO.new } + let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } + + def error_output = err.string.chomp + + context "when file to be generated already exists" do + it "exits with error" do + subject.call(name: "users.new") + + file_path = "app/views/users/new.rb" + + expect do + subject.call(name: "users.new") + end.to raise_error SystemExit do |exception| + expect(exception.status).to eq 1 + expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} + 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 244bb41c..d355a376 100644 --- a/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/migration_spec.rb @@ -3,18 +3,15 @@ require "hanami" RSpec.describe Hanami::CLI::Commands::App::Generate::Migration, :app do - subject { described_class.new(fs: fs, err: err) } + subject { described_class.new(fs: fs) } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } 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) { @@ -65,23 +62,6 @@ def error_output = err.string.chomp "Name must contain only letters, numbers, and underscores." )) end - - context "with existing file" do - let(:file_path) { "config/db/migrate/20240713140600_create_posts.rb" } - - before do - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - subject.call(name: "create_posts") - end.to raise_error SystemExit do |exception| - expect(exception.status).to eq 1 - expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "generating for a slice" do @@ -94,24 +74,5 @@ def error_output = err.string.chomp expect(fs.read("slices/main/config/db/migrate/20240713140600_create_posts.rb")).to eq migration_file_contents expect(output).to eq("Created slices/main/config/db/migrate/20240713140600_create_posts.rb") 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(file_path, "existing content") - end - - it "exits with error message" do - expect do - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - 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 1c229d24..3d6e227c 100644 --- a/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/operation_spec.rb @@ -1,10 +1,9 @@ # frozen_string_literal: true RSpec.describe Hanami::CLI::Commands::App::Generate::Operation, :app do - subject { described_class.new(fs: fs, out: out, err: err) } + subject { described_class.new(fs: fs, out: out) } let(:out) { StringIO.new } - let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:app) { Hanami.app.namespace } @@ -12,8 +11,6 @@ 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") @@ -80,23 +77,6 @@ def call expect(fs.read("app/admin/books/add.rb")).to eq(operation_file) expect(output).to include("Created app/admin/books/add.rb") end - - context "with existing file" do - let(:file_path) { "app/admin/books/add.rb" } - - before do - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - subject.call(name: "admin.books.add") - end.to raise_error SystemExit do |exception| - expect(exception.status).to eq 1 - expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "generating for a slice" do @@ -145,23 +125,5 @@ def call expect(fs.read("slices/main/admin/books/add.rb")).to eq(operation_file) expect(output).to include("Created slices/main/admin/books/add.rb") end - - context "with existing file" do - let(:file_path) { "slices/main/admin/books/add.rb" } - - before do - fs.mkdir("slices/main") - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - 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 44b218f1..8ebc2392 100644 --- a/spec/unit/hanami/cli/commands/app/generate/part_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/part_spec.rb @@ -4,10 +4,9 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::Part, :app do - subject { described_class.new(fs: fs, out: out, err: err) } + subject { described_class.new(fs: fs, out: out) } 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 } @@ -17,8 +16,6 @@ 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 @@ -60,25 +57,6 @@ class User < Test::Views::Part expect(output).to include("Created app/views/parts/user.rb") end end - - context "with existing file" do - let(:file_path) { "app/views/parts/user.rb" } - - before do - within_application_directory do - fs.write(file_path, "existing content") - end - end - - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "with base part" do @@ -230,26 +208,6 @@ class User < Main::Views::Part end 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(file_path, "existing content") - end - end - - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end - context "with missing slice" do it "raises error" do within_application_directory do 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 a9e001a5..55edb7dd 100644 --- a/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/relation_spec.rb @@ -1,15 +1,12 @@ # frozen_string_literal: true RSpec.describe Hanami::CLI::Commands::App::Generate::Relation, "#call", :app_integration do - subject { described_class.new(out: out, err: err) } + subject { described_class.new(out: out) } 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 @@ -116,23 +113,6 @@ class Books < TestApp::DB::Relation expect(Hanami.app.root.join("app/relations/books.rb").read).to eq relation_file expect(output).to include("Created app/relations/books.rb") end - - context "with existing file" do - let(:file_path) { "app/relations/books.rb" } - - before do - write file_path, "existing content" - end - - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "generating for a slice" do @@ -197,22 +177,5 @@ class Drafts < Main::DB::Relation expect(Hanami.app.root.join("slices/main/relations/book/drafts.rb").read).to eq(relation_file) expect(output).to include("Created slices/main/relations/book/drafts.rb") end - - context "with existing file" do - let(:file_path) { "slices/main/relations/books.rb" } - - before do - write file_path, "existing content" - end - - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - 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 ffac4e4b..84a0076a 100644 --- a/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/repo_spec.rb @@ -1,10 +1,9 @@ # frozen_string_literal: true RSpec.describe Hanami::CLI::Commands::App::Generate::Repo, :app do - subject { described_class.new(fs: fs, out: out, err: err) } + subject { described_class.new(fs: fs, out: out) } let(:out) { StringIO.new } - let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:app) { Hanami.app.namespace } @@ -12,8 +11,6 @@ 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 @@ -94,23 +91,6 @@ class HardcoverRepo < Test::DB::Repo expect(fs.read("app/repos/books/published/hardcover_repo.rb")).to eq(repo_file) expect(output).to include("Created app/repos/books/published/hardcover_repo.rb") end - - context "with existing file" do - let(:file_path) { "app/repos/book_repo.rb" } - - before do - fs.write(file_path, "existing content") - end - - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "generating for a slice" do @@ -153,22 +133,5 @@ class DraftRepo < Main::DB::Repo expect(fs.read("slices/main/repos/book/draft_repo.rb")).to eq(repo_file) expect(output).to include("Created slices/main/repos/book/draft_repo.rb") end - - context "with existing file" do - let(:file_path) { "slices/main/repos/book_repo.rb" } - - before do - fs.write(file_path, "existing content") - end - - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - 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 a7cbf72a..240c291c 100644 --- a/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/struct_spec.rb @@ -1,17 +1,14 @@ # frozen_string_literal: true RSpec.describe Hanami::CLI::Commands::App::Generate::Struct, :app do - subject { described_class.new(fs: fs, out: out, err: err) } + subject { described_class.new(fs: fs, out: out) } let(:out) { StringIO.new } - let(:err) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:app) { Hanami.app.namespace } def output = out.string.chomp - def error_output = err.string.chomp - context "generating for app" do it "generates a struct without a namespace" do subject.call(name: "book") @@ -72,23 +69,6 @@ class Hardcover < Test::DB::Struct expect(fs.read("app/structs/book/published/hardcover.rb")).to eq(struct_file) expect(output).to include("Created app/structs/book/published/hardcover.rb") end - - context "with existing file" do - let(:file_path) { "app/structs/book/published/hardcover.rb" } - - before do - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - subject.call(name: "book/published/hardcover") - end.to raise_error SystemExit do |exception| - expect(exception.status).to eq 1 - expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end end context "generating for a slice" do @@ -131,22 +111,5 @@ class DraftBook < Main::DB::Struct expect(fs.read("slices/main/structs/book/draft_book.rb")).to eq(struct_file) expect(output).to include("Created slices/main/structs/book/draft_book.rb") end - - context "with existing file" do - let(:file_path) { "slices/main/structs/book/draft_book.rb" } - - before do - fs.write(file_path, "existing content") - end - - it "exits with error message" do - expect do - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - 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 50591123..7eed4ea7 100644 --- a/spec/unit/hanami/cli/commands/app/generate/view_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/view_spec.rb @@ -4,10 +4,9 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::View, :app do - subject { described_class.new(fs: fs, out: out, err: err) } + subject { described_class.new(fs: fs, out: out) } 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 } @@ -17,8 +16,6 @@ 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") @@ -93,46 +90,6 @@ class Index < Test::View expect(output).to include("Created app/templates/special/users/index.html.erb") end end - - context "with existing view file" do - let(:file_path) { "app/views/users/index.rb" } - - before do - within_application_directory do - fs.write(file_path, "existing content") - end - end - - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end - - context "with existing template file" do - let(:file_path) { "app/templates/users/index.html.erb" } - - before do - within_application_directory do - fs.write(file_path, "existing content") - end - end - - it "raises error" do - within_application_directory do - expect do - subject.call(name: "users.index") - end.to raise_error SystemExit do |exception| - expect(exception.status).to eq 1 - expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end - end end context "generating for a slice" do @@ -169,48 +126,6 @@ class Index < Main::View expect(output).to include("Created slices/main/templates/users/index.html.erb") end end - - context "with existing view file" do - let(:file_path) { "slices/main/views/users/index.rb" } - - before do - within_application_directory do - fs.mkdir("slices/main") - fs.write(file_path, "existing content") - end - end - - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end - - context "with existing template file" do - let(:file_path) { "slices/main/templates/users/index.html.erb" } - - before do - within_application_directory do - fs.mkdir("slices/main") - fs.write(file_path, "existing content") - end - end - - it "raises error" do - within_application_directory do - expect do - 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 Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:} - end - end - end - end end private