Skip to content

Commit 937290c

Browse files
committed
Change FileAlreadyExistsError message
1 parent ea0db83 commit 937290c

File tree

11 files changed

+105
-45
lines changed

11 files changed

+105
-45
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
name: ci
22

3-
on:
3+
"on":
44
push:
5+
paths:
6+
- ".github/workflows/ci.yml"
7+
- "lib/**"
8+
- "*.gemspec"
9+
- "spec/**"
10+
- "Rakefile"
11+
- "Gemfile"
12+
- ".rubocop.yml"
13+
pull_request:
14+
branches:
15+
- main
16+
schedule:
17+
- cron: "30 4 * * *"
18+
create:
519

620
jobs:
721
tests:

lib/hanami/cli/errors.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ def initialize(path)
4646

4747
# @api public
4848
class FileAlreadyExistsError < Error
49-
def initialize(path)
50-
super("Cannot overwrite existing file: `#{path}`")
49+
ERROR_MESSAGE = <<~ERROR.chomp
50+
The file `%{file_path}` could not be generated because it already exists in your application.
51+
ERROR
52+
53+
def initialize(file_path)
54+
super(ERROR_MESSAGE % {file_path:})
5155
end
5256
end
5357

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ def error_output = err.string.chomp
3838
end
3939

4040
context "with existing action file" do
41+
let(:file_path) { "app/actions/#{controller}/#{action}.rb" }
42+
4143
before do
4244
within_application_directory do
43-
fs.write("app/actions/#{controller}/#{action}.rb", "")
45+
fs.write(file_path, "")
4446
end
4547
end
4648

@@ -49,15 +51,17 @@ def error_output = err.string.chomp
4951
within_application_directory { generate_action }
5052
end.to raise_error SystemExit do |exception|
5153
expect(exception.status).to eq 1
52-
expect(error_output).to eq "Cannot overwrite existing file: `app/actions/#{controller}/#{action}.rb`"
54+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
5355
end
5456
end
5557
end
5658

5759
context "with existing view file" do
60+
let(:file_path) { "app/views/#{controller}/#{action}.rb" }
61+
5862
before do
5963
within_application_directory do
60-
fs.write("app/views/#{controller}/#{action}.rb", "")
64+
fs.write(file_path, "")
6165
end
6266
end
6367

@@ -66,15 +70,17 @@ def error_output = err.string.chomp
6670
within_application_directory { generate_action }
6771
end.to raise_error SystemExit do |exception|
6872
expect(exception.status).to eq 1
69-
expect(error_output).to eq "Cannot overwrite existing file: `app/views/#{controller}/#{action}.rb`"
73+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
7074
end
7175
end
7276
end
7377

7478
context "with existing template file" do
79+
let(:file_path) { "app/templates/#{controller}/#{action}.html.erb" }
80+
7581
before do
7682
within_application_directory do
77-
fs.write("app/templates/#{controller}/#{action}.html.erb", "")
83+
fs.write(file_path, "")
7884
end
7985
end
8086

@@ -83,7 +89,7 @@ def error_output = err.string.chomp
8389
within_application_directory { generate_action }
8490
end.to raise_error SystemExit do |exception|
8591
expect(exception.status).to eq 1
86-
expect(error_output).to eq "Cannot overwrite existing file: `app/templates/#{controller}/#{action}.html.erb`"
92+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
8793
end
8894
end
8995
end

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,18 @@ class SendWelcomeEmail
4242
end
4343

4444
context "with existing file" do
45+
let(:file_path) { "app/operations/send_welcome_email.rb" }
46+
4547
before do
46-
fs.write("app/operations/send_welcome_email.rb", "existing content")
48+
fs.write(file_path, "existing content")
4749
end
4850

4951
it "exits with error message" do
5052
expect do
5153
subject.call(name: "operations.send_welcome_email")
5254
end.to raise_error SystemExit do |exception|
5355
expect(exception.status).to eq 1
54-
expect(error_output).to eq "Cannot overwrite existing file: `app/operations/send_welcome_email.rb`"
56+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
5557
end
5658
end
5759
end
@@ -81,16 +83,18 @@ class SendWelcomeEmail
8183
end
8284

8385
context "with existing file" do
86+
let(:file_path) { "app/operations/user/mailing/send_welcome_email.rb" }
87+
8488
before do
85-
fs.write("app/operations/user/mailing/send_welcome_email.rb", "existing content")
89+
fs.write(file_path, "existing content")
8690
end
8791

8892
it "exits with error message" do
8993
expect do
9094
subject.call(name: "operations.user.mailing.send_welcome_email")
9195
end.to raise_error SystemExit do |exception|
9296
expect(exception.status).to eq 1
93-
expect(error_output).to eq "Cannot overwrite existing file: `app/operations/user/mailing/send_welcome_email.rb`"
97+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
9498
end
9599
end
96100
end
@@ -119,16 +123,18 @@ class WelcomeEmail
119123
end
120124

121125
context "with existing file" do
126+
let(:file_path) { "slices/main/renderers/welcome_email.rb" }
127+
122128
before do
123-
fs.write("slices/main/renderers/welcome_email.rb", "existing content")
129+
fs.write(file_path, "existing content")
124130
end
125131

126132
it "exits with error message" do
127133
expect do
128134
subject.call(name: "renderers.welcome_email", slice: "main")
129135
end.to raise_error SystemExit do |exception|
130136
expect(exception.status).to eq 1
131-
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/renderers/welcome_email.rb`"
137+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
132138
end
133139
end
134140
end
@@ -159,16 +165,18 @@ class WelcomeEmail
159165
end
160166

161167
context "with existing file" do
168+
let(:file_path) { "slices/main/renderers/user/mailing/welcome_email.rb" }
169+
162170
before do
163-
fs.write("slices/main/renderers/user/mailing/welcome_email.rb", "existing content")
171+
fs.write(file_path, "existing content")
164172
end
165173

166174
it "exits with error message" do
167175
expect do
168176
subject.call(name: "renderers.user.mailing.welcome_email", slice: "main")
169177
end.to raise_error SystemExit do |exception|
170178
expect(exception.status).to eq 1
171-
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/renderers/user/mailing/welcome_email.rb`"
179+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
172180
end
173181
end
174182
end

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ def error_output = err.string.chomp
6969
end
7070

7171
context "with existing file" do
72+
let(:file_path) { "config/db/migrate/20240713140600_create_posts.rb" }
73+
7274
before do
73-
fs.write("config/db/migrate/20240713140600_create_posts.rb", "existing content")
75+
fs.write(file_path, "existing content")
7476
end
7577

7678
it "exits with error message" do
7779
expect do
7880
subject.call(name: "create_posts")
7981
end.to raise_error SystemExit do |exception|
8082
expect(exception.status).to eq 1
81-
expect(error_output).to eq "Cannot overwrite existing file: `config/db/migrate/20240713140600_create_posts.rb`"
83+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
8284
end
8385
end
8486
end
@@ -96,17 +98,19 @@ def error_output = err.string.chomp
9698
end
9799

98100
context "with existing file" do
101+
let(:file_path) { "slices/main/config/db/migrate/20240713140600_create_posts.rb" }
102+
99103
context "with existing file" do
100104
before do
101-
fs.write("slices/main/config/db/migrate/20240713140600_create_posts.rb", "existing content")
105+
fs.write(file_path, "existing content")
102106
end
103107

104108
it "exits with error message" do
105109
expect do
106110
subject.call(name: "create_posts", slice: "main")
107111
end.to raise_error SystemExit do |exception|
108112
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`"
113+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
110114
end
111115
end
112116
end

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,18 @@ def call
8383
end
8484

8585
context "with existing file" do
86+
let(:file_path) { "app/admin/books/add.rb" }
87+
8688
before do
87-
fs.write("app/admin/books/add.rb", "existing content")
89+
fs.write(file_path, "existing content")
8890
end
8991

9092
it "exits with error message" do
9193
expect do
9294
subject.call(name: "admin.books.add")
9395
end.to raise_error SystemExit do |exception|
9496
expect(exception.status).to eq 1
95-
expect(error_output).to eq "Cannot overwrite existing file: `app/admin/books/add.rb`"
97+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
9698
end
9799
end
98100
end
@@ -146,17 +148,19 @@ def call
146148
end
147149

148150
context "with existing file" do
151+
let(:file_path) { "slices/main/admin/books/add.rb" }
152+
149153
before do
150154
fs.mkdir("slices/main")
151-
fs.write("slices/main/admin/books/add.rb", "existing content")
155+
fs.write(file_path, "existing content")
152156
end
153157

154158
it "exits with error message" do
155159
expect do
156160
subject.call(name: "admin.books.add", slice: "main")
157161
end.to raise_error SystemExit do |exception|
158162
expect(exception.status).to eq 1
159-
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/admin/books/add.rb`"
163+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
160164
end
161165
end
162166
end

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ class User < Test::Views::Part
6363
end
6464

6565
context "with existing file" do
66+
let(:file_path) { "app/views/parts/user.rb" }
67+
6668
before do
6769
within_application_directory do
68-
fs.write("app/views/parts/user.rb", "existing content")
70+
fs.write(file_path, "existing content")
6971
end
7072
end
7173

@@ -74,7 +76,7 @@ class User < Test::Views::Part
7476
within_application_directory { subject.call(name: "user") }
7577
end.to raise_error SystemExit do |exception|
7678
expect(exception.status).to eq 1
77-
expect(error_output).to eq "Cannot overwrite existing file: `app/views/parts/user.rb`"
79+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
7880
end
7981
end
8082
end
@@ -230,10 +232,12 @@ class User < Main::Views::Part
230232
end
231233

232234
context "with existing file" do
235+
let(:file_path) { "slices/main/views/parts/user.rb" }
236+
233237
before do
234238
within_application_directory do
235239
fs.mkdir("slices/main")
236-
fs.write("slices/main/views/parts/user.rb", "existing content")
240+
fs.write(file_path, "existing content")
237241
end
238242
end
239243

@@ -242,7 +246,7 @@ class User < Main::Views::Part
242246
within_application_directory { subject.call(name: "user", slice: "main") }
243247
end.to raise_error SystemExit do |exception|
244248
expect(exception.status).to eq 1
245-
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/views/parts/user.rb`"
249+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
246250
end
247251
end
248252
end

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ class Books < TestApp::DB::Relation
120120
end
121121

122122
context "with existing file" do
123+
let(:file_path) { "app/relations/books.rb" }
124+
123125
before do
124-
write "app/relations/books.rb", "existing content"
126+
write file_path, "existing content"
125127
end
126128

127129
it "exits with error message" do
128130
expect do
129131
subject.call(name: "books")
130132
end.to raise_error SystemExit do |exception|
131133
expect(exception.status).to eq 1
132-
expect(error_output).to eq "Cannot overwrite existing file: `app/relations/books.rb`"
134+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
133135
end
134136
end
135137
end
@@ -199,16 +201,18 @@ class Drafts < Main::DB::Relation
199201
end
200202

201203
context "with existing file" do
204+
let(:file_path) { "slices/main/relations/books.rb" }
205+
202206
before do
203-
write "slices/main/relations/books.rb", "existing content"
207+
write file_path, "existing content"
204208
end
205209

206210
it "exits with error message" do
207211
expect do
208212
subject.call(name: "books", slice: "main")
209213
end.to raise_error SystemExit do |exception|
210214
expect(exception.status).to eq 1
211-
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/relations/books.rb`"
215+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
212216
end
213217
end
214218
end

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,18 @@ class HardcoverRepo < Test::DB::Repo
9797
end
9898

9999
context "with existing file" do
100+
let(:file_path) { "app/repos/book_repo.rb" }
101+
100102
before do
101-
fs.write("app/repos/book_repo.rb", "existing content")
103+
fs.write(file_path, "existing content")
102104
end
103105

104106
it "exits with error message" do
105107
expect do
106108
subject.call(name: "books")
107109
end.to raise_error SystemExit do |exception|
108110
expect(exception.status).to eq 1
109-
expect(error_output).to eq "Cannot overwrite existing file: `app/repos/book_repo.rb`"
111+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
110112
end
111113
end
112114
end
@@ -154,16 +156,18 @@ class DraftRepo < Main::DB::Repo
154156
end
155157

156158
context "with existing file" do
159+
let(:file_path) { "slices/main/repos/book_repo.rb" }
160+
157161
before do
158-
fs.write("slices/main/repos/book_repo.rb", "existing content")
162+
fs.write(file_path, "existing content")
159163
end
160164

161165
it "exits with error message" do
162166
expect do
163167
subject.call(name: "books", slice: "main")
164168
end.to raise_error SystemExit do |exception|
165169
expect(exception.status).to eq 1
166-
expect(error_output).to eq "Cannot overwrite existing file: `slices/main/repos/book_repo.rb`"
170+
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
167171
end
168172
end
169173
end

0 commit comments

Comments
 (0)