Skip to content

Commit 2eba1f6

Browse files
committed
Catch FileAlreadyExistsError and exit cleanly
1 parent 04ec18a commit 2eba1f6

File tree

10 files changed

+172
-92
lines changed

10 files changed

+172
-92
lines changed

lib/hanami/cli/commands/app/command.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def call(*args, **opts)
4444
Hanami::Env.load
4545

4646
super
47+
rescue FileAlreadyExistsError => error
48+
err.puts(error.message)
49+
exit(1)
4750
end
4851
end
4952

spec/unit/hanami/cli/commands/app/generate/action_spec.rb

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
require "ostruct"
55

66
RSpec.describe Hanami::CLI::Commands::App::Generate::Action, :app do
7-
subject { described_class.new(fs: fs, inflector: inflector, generator: generator) }
7+
subject { described_class.new(fs: fs, inflector: inflector, generator: generator, err: err) }
88

99
let(:out) { StringIO.new }
10+
let(:err) { StringIO.new }
1011
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
1112
let(:inflector) { Dry::Inflector.new }
1213
let(:generator) { Hanami::CLI::Generators::App::Action.new(fs: fs, inflector: inflector) }
@@ -20,6 +21,8 @@ def output
2021
out.rewind && out.read.chomp
2122
end
2223

24+
def error_output = err.string.chomp
25+
2326
shared_context "with existing files" do
2427
context "with existing route file" do
2528
it "generates action without error" do
@@ -41,12 +44,13 @@ def output
4144
end
4245
end
4346

44-
it "raises error" do
45-
expect {
46-
within_application_directory do
47-
generate_action
48-
end
49-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/actions/#{controller}/#{action}.rb`")
47+
it "exits with error message" do
48+
expect do
49+
within_application_directory { generate_action }
50+
end.to raise_error SystemExit do |exception|
51+
expect(exception.status).to eq 1
52+
expect(error_output).to eq "Cannot overwrite existing file: `app/actions/#{controller}/#{action}.rb`"
53+
end
5054
end
5155
end
5256

@@ -57,12 +61,13 @@ def output
5761
end
5862
end
5963

60-
it "raises error" do
61-
expect {
62-
within_application_directory do
63-
generate_action
64-
end
65-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/views/#{controller}/#{action}.rb`")
64+
it "exits with error message" do
65+
expect do
66+
within_application_directory { generate_action }
67+
end.to raise_error SystemExit do |exception|
68+
expect(exception.status).to eq 1
69+
expect(error_output).to eq "Cannot overwrite existing file: `app/views/#{controller}/#{action}.rb`"
70+
end
6671
end
6772
end
6873

@@ -73,12 +78,13 @@ def output
7378
end
7479
end
7580

76-
it "raises error" do
77-
expect {
78-
within_application_directory do
79-
generate_action
80-
end
81-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/templates/#{controller}/#{action}.html.erb`")
81+
it "exits with error message" do
82+
expect do
83+
within_application_directory { generate_action }
84+
end.to raise_error SystemExit do |exception|
85+
expect(exception.status).to eq 1
86+
expect(error_output).to eq "Cannot overwrite existing file: `app/templates/#{controller}/#{action}.html.erb`"
87+
end
8288
end
8389
end
8490
end

spec/unit/hanami/cli/commands/app/generate/component_spec.rb

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
require "ostruct"
55

66
RSpec.describe Hanami::CLI::Commands::App::Generate::Component, :app do
7-
subject { described_class.new(fs: fs, inflector: inflector, out: out) }
7+
subject { described_class.new(fs: fs, inflector: inflector, out: out, err: err) }
88

99
let(:out) { StringIO.new }
10+
let(:err) { StringIO.new }
1011
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
1112
let(:inflector) { Dry::Inflector.new }
1213
let(:app) { Hanami.app.namespace }
@@ -18,6 +19,8 @@ def output
1819
out.rewind && out.read.chomp
1920
end
2021

22+
def error_output = err.string.chomp
23+
2124
context "generating for app" do
2225
context "shallowly nested" do
2326
it "generates the component" do
@@ -43,10 +46,13 @@ class SendWelcomeEmail
4346
fs.write("app/operations/send_welcome_email.rb", "existing content")
4447
end
4548

46-
it "raises error" do
47-
expect {
49+
it "exits with error message" do
50+
expect do
4851
subject.call(name: "operations.send_welcome_email")
49-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
52+
end.to raise_error SystemExit do |exception|
53+
expect(exception.status).to eq 1
54+
expect(error_output).to eq "Cannot overwrite existing file: `app/operations/send_welcome_email.rb`"
55+
end
5056
end
5157
end
5258
end
@@ -79,10 +85,13 @@ class SendWelcomeEmail
7985
fs.write("app/operations/user/mailing/send_welcome_email.rb", "existing content")
8086
end
8187

82-
it "raises error" do
83-
expect {
88+
it "exits with error message" do
89+
expect do
8490
subject.call(name: "operations.user.mailing.send_welcome_email")
85-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
91+
end.to raise_error SystemExit do |exception|
92+
expect(exception.status).to eq 1
93+
expect(error_output).to eq "Cannot overwrite existing file: `app/operations/user/mailing/send_welcome_email.rb`"
94+
end
8695
end
8796
end
8897
end
@@ -114,10 +123,13 @@ class WelcomeEmail
114123
fs.write("slices/main/renderers/welcome_email.rb", "existing content")
115124
end
116125

117-
it "raises error" do
118-
expect {
126+
it "exits with error message" do
127+
expect do
119128
subject.call(name: "renderers.welcome_email", slice: "main")
120-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
129+
end.to raise_error SystemExit do |exception|
130+
expect(exception.status).to eq 1
131+
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/renderers/welcome_email.rb`"
132+
end
121133
end
122134
end
123135
end
@@ -151,10 +163,13 @@ class WelcomeEmail
151163
fs.write("slices/main/renderers/user/mailing/welcome_email.rb", "existing content")
152164
end
153165

154-
it "raises error" do
155-
expect {
166+
it "exits with error message" do
167+
expect do
156168
subject.call(name: "renderers.user.mailing.welcome_email", slice: "main")
157-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
169+
end.to raise_error SystemExit do |exception|
170+
expect(exception.status).to eq 1
171+
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/renderers/user/mailing/welcome_email.rb`"
172+
end
158173
end
159174
end
160175
end

spec/unit/hanami/cli/commands/app/generate/migration_spec.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
require "hanami"
44

55
RSpec.describe Hanami::CLI::Commands::App::Generate::Migration, :app do
6-
subject { described_class.new(fs: fs, inflector: inflector) }
6+
subject { described_class.new(fs: fs, inflector: inflector, err: err) }
77

88
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
99
let(:inflector) { Dry::Inflector.new }
1010

1111
let(:out) { StringIO.new }
12+
let(:err) { StringIO.new }
1213

1314
def output
1415
out.string.strip
1516
end
1617

18+
def error_output = err.string.chomp
19+
1720
let(:app) { Hanami.app.namespace }
1821

1922
let(:migration_file_contents) {
@@ -70,10 +73,13 @@ def output
7073
fs.write("config/db/migrate/20240713140600_create_posts.rb", "existing content")
7174
end
7275

73-
it "raises error" do
74-
expect {
76+
it "exits with error message" do
77+
expect do
7578
subject.call(name: "create_posts")
76-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
79+
end.to raise_error SystemExit do |exception|
80+
expect(exception.status).to eq 1
81+
expect(error_output).to eq "Cannot overwrite existing file: `config/db/migrate/20240713140600_create_posts.rb`"
82+
end
7783
end
7884
end
7985
end
@@ -95,10 +101,13 @@ def output
95101
fs.write("slices/main/config/db/migrate/20240713140600_create_posts.rb", "existing content")
96102
end
97103

98-
it "raises error" do
99-
expect {
104+
it "exits with error message" do
105+
expect do
100106
subject.call(name: "create_posts", slice: "main")
101-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
107+
end.to raise_error SystemExit do |exception|
108+
expect(exception.status).to eq 1
109+
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/config/db/migrate/20240713140600_create_posts.rb`"
110+
end
102111
end
103112
end
104113
end

spec/unit/hanami/cli/commands/app/generate/operation_spec.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Hanami::CLI::Commands::App::Generate::Operation, :app do
4-
subject { described_class.new(fs: fs, inflector: inflector, out: out) }
4+
subject { described_class.new(fs: fs, inflector: inflector, out: out, err: err) }
55

66
let(:out) { StringIO.new }
7+
let(:err) { StringIO.new }
78
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
89
let(:inflector) { Dry::Inflector.new }
910
let(:app) { Hanami.app.namespace }
@@ -12,6 +13,8 @@ def output
1213
out.string.chomp
1314
end
1415

16+
def error_output = err.string.chomp
17+
1518
context "generating for app" do
1619
it "generates an operation without a namespace, with a recommendation" do
1720
subject.call(name: "add_book")
@@ -84,10 +87,13 @@ def call
8487
fs.write("app/admin/books/add.rb", "existing content")
8588
end
8689

87-
it "raises error" do
88-
expect {
90+
it "exits with error message" do
91+
expect do
8992
subject.call(name: "admin.books.add")
90-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
93+
end.to raise_error SystemExit do |exception|
94+
expect(exception.status).to eq 1
95+
expect(error_output).to eq "Cannot overwrite existing file: `app/admin/books/add.rb`"
96+
end
9197
end
9298
end
9399
end
@@ -145,10 +151,13 @@ def call
145151
fs.write("slices/main/admin/books/add.rb", "existing content")
146152
end
147153

148-
it "raises error" do
149-
expect {
154+
it "exits with error message" do
155+
expect do
150156
subject.call(name: "admin.books.add", slice: "main")
151-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
157+
end.to raise_error SystemExit do |exception|
158+
expect(exception.status).to eq 1
159+
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/admin/books/add.rb`"
160+
end
152161
end
153162
end
154163
end

spec/unit/hanami/cli/commands/app/generate/part_spec.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
require "ostruct"
55

66
RSpec.describe Hanami::CLI::Commands::App::Generate::Part, :app do
7-
subject { described_class.new(fs: fs, inflector: inflector, generator: generator) }
7+
subject { described_class.new(fs: fs, inflector: inflector, generator: generator, err: err) }
88

99
let(:out) { StringIO.new }
10+
let(:err) { StringIO.new }
1011
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
1112
let(:inflector) { Dry::Inflector.new }
1213
let(:generator) { Hanami::CLI::Generators::App::Part.new(fs: fs, inflector: inflector) }
@@ -17,6 +18,8 @@ def output
1718
out.rewind && out.read.chomp
1819
end
1920

21+
def error_output = err.string.chomp
22+
2023
context "generating for app" do
2124
context "without base part" do
2225
it "generates base part and the part" do
@@ -66,11 +69,12 @@ class User < Test::Views::Part
6669
end
6770
end
6871

69-
it "raises error" do
70-
within_application_directory do
71-
expect {
72-
subject.call(name: "user")
73-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
72+
it "exits with error message" do
73+
expect do
74+
within_application_directory { subject.call(name: "user") }
75+
end.to raise_error SystemExit do |exception|
76+
expect(exception.status).to eq 1
77+
expect(error_output).to eq "Cannot overwrite existing file: `app/views/parts/user.rb`"
7478
end
7579
end
7680
end
@@ -233,11 +237,12 @@ class User < Main::Views::Part
233237
end
234238
end
235239

236-
it "raises error" do
237-
within_application_directory do
238-
expect {
239-
subject.call(name: "user", slice: "main")
240-
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
240+
it "exits with error message" do
241+
expect do
242+
within_application_directory { subject.call(name: "user", slice: "main") }
243+
end.to raise_error SystemExit do |exception|
244+
expect(exception.status).to eq 1
245+
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/views/parts/user.rb`"
241246
end
242247
end
243248
end

0 commit comments

Comments
 (0)