Skip to content

Commit f0bd4fd

Browse files
committed
finish specs
1 parent bc34f40 commit f0bd4fd

File tree

10 files changed

+241
-2
lines changed

10 files changed

+241
-2
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ gem "dry-system", github: "dry-rb/dry-system", branch: "main"
2020

2121
gem "rack"
2222

23-
gem "mysql2"
23+
# gem "mysql2"
2424
gem "pg"
2525
gem "sqlite3"
2626

lib/hanami/cli/generators/app/ruby_file_writer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def call(key:, namespace:, base_path:, relative_parent_class:, extra_namespace:
3131
relative_parent_class: relative_parent_class,
3232
extra_namespace: extra_namespace,
3333
body: body,
34-
).write
34+
).create
3535
end
3636

3737
private
@@ -62,6 +62,10 @@ def initialize(
6262
@body = body
6363
end
6464

65+
def create
66+
fs.create(path, file_contents)
67+
end
68+
6569
def write
6670
fs.write(path, file_contents)
6771
end

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ class SendWelcomeEmail
3838
expect(fs.read("app/operations/send_welcome_email.rb")).to eq(component)
3939
expect(output).to include("Created app/operations/send_welcome_email.rb")
4040
end
41+
42+
context "with existing file" do
43+
before do
44+
fs.write("app/operations/send_welcome_email.rb", "existing content")
45+
end
46+
47+
it "raises error" do
48+
expect {
49+
subject.call(name: "operations.send_welcome_email")
50+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
51+
end
52+
end
4153
end
4254

4355
context "deeply nested" do
@@ -62,6 +74,18 @@ class SendWelcomeEmail
6274
expect(fs.read("app/operations/user/mailing/send_welcome_email.rb")).to eq(component)
6375
expect(output).to include("Created app/operations/user/mailing/send_welcome_email.rb")
6476
end
77+
78+
context "with existing file" do
79+
before do
80+
fs.write("app/operations/user/mailing/send_welcome_email.rb", "existing content")
81+
end
82+
83+
it "raises error" do
84+
expect {
85+
subject.call(name: "operations.user.mailing.send_welcome_email")
86+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
87+
end
88+
end
6589
end
6690
end
6791

@@ -85,6 +109,18 @@ class WelcomeEmail
85109
expect(fs.read("slices/main/renderers/welcome_email.rb")).to eq(component)
86110
expect(output).to include("Created slices/main/renderers/welcome_email.rb")
87111
end
112+
113+
context "with existing file" do
114+
before do
115+
fs.write("slices/main/renderers/welcome_email.rb", "existing content")
116+
end
117+
118+
it "raises error" do
119+
expect {
120+
subject.call(name: "renderers.welcome_email", slice: "main")
121+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
122+
end
123+
end
88124
end
89125

90126
context "deeply nested" do
@@ -110,6 +146,18 @@ class WelcomeEmail
110146
expect(fs.read("slices/main/renderers/user/mailing/welcome_email.rb")).to eq(component)
111147
expect(output).to include("Created slices/main/renderers/user/mailing/welcome_email.rb")
112148
end
149+
150+
context "with existing file" do
151+
before do
152+
fs.write("slices/main/renderers/user/mailing/welcome_email.rb", "existing content")
153+
end
154+
155+
it "raises error" do
156+
expect {
157+
subject.call(name: "renderers.user.mailing.welcome_email", slice: "main")
158+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
159+
end
160+
end
113161
end
114162

115163
context "with missing slice" do

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ def output
6464
"Name must contain only letters, numbers, and underscores."
6565
))
6666
end
67+
68+
context "with existing file" do
69+
before do
70+
fs.write("config/db/migrate/20240713140600_create_posts.rb", "existing content")
71+
end
72+
73+
it "raises error" do
74+
expect {
75+
subject.call(name: "create_posts")
76+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
77+
end
78+
end
6779
end
6880

6981
context "generating for a slice" do
@@ -76,5 +88,19 @@ def output
7688
expect(fs.read("slices/main/config/db/migrate/20240713140600_create_posts.rb")).to eq migration_file_contents
7789
expect(output).to eq("Created slices/main/config/db/migrate/20240713140600_create_posts.rb")
7890
end
91+
92+
context "with existing file" do
93+
context "with existing file" do
94+
before do
95+
fs.write("slices/main/config/db/migrate/20240713140600_create_posts.rb", "existing content")
96+
end
97+
98+
it "raises error" do
99+
expect {
100+
subject.call(name: "create_posts", slice: "main")
101+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
102+
end
103+
end
104+
end
79105
end
80106
end

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ def call
7878
expect(fs.read("app/admin/books/add.rb")).to eq(operation_file)
7979
expect(output).to include("Created app/admin/books/add.rb")
8080
end
81+
82+
context "with existing file" do
83+
before do
84+
fs.write("app/admin/books/add.rb", "existing content")
85+
end
86+
87+
it "raises error" do
88+
expect {
89+
subject.call(name: "admin.books.add")
90+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
91+
end
92+
end
8193
end
8294

8395
context "generating for a slice" do
@@ -126,5 +138,18 @@ def call
126138
expect(fs.read("slices/main/admin/books/add.rb")).to eq(operation_file)
127139
expect(output).to include("Created slices/main/admin/books/add.rb")
128140
end
141+
142+
context "with existing file" do
143+
before do
144+
fs.mkdir("slices/main")
145+
fs.write("slices/main/admin/books/add.rb", "existing content")
146+
end
147+
148+
it "raises error" do
149+
expect {
150+
subject.call(name: "admin.books.add", slice: "main")
151+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
152+
end
153+
end
129154
end
130155
end

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ class User < Test::Views::Part
5858
expect(output).to include("Created app/views/parts/user.rb")
5959
end
6060
end
61+
62+
context "with existing file" do
63+
before do
64+
within_application_directory do
65+
fs.write("app/views/parts/user.rb", "existing content")
66+
end
67+
end
68+
69+
it "raises error" do
70+
within_application_directory do
71+
expect {
72+
subject.call(name: "user")
73+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
74+
end
75+
end
76+
end
6177
end
6278

6379
context "with base part" do
@@ -209,6 +225,23 @@ class User < Main::Views::Part
209225
end
210226
end
211227

228+
context "with existing file" do
229+
before do
230+
within_application_directory do
231+
fs.mkdir("slices/main")
232+
fs.write("slices/main/views/parts/user.rb", "existing content")
233+
end
234+
end
235+
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)
241+
end
242+
end
243+
end
244+
212245
context "with missing slice" do
213246
it "raises error" do
214247
within_application_directory do

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ class Books < TestApp::DB::Relation
114114
expect(Hanami.app.root.join("app/relations/books.rb").read).to eq relation_file
115115
expect(output).to include("Created app/relations/books.rb")
116116
end
117+
118+
context "with existing file" do
119+
before do
120+
write "app/relations/books.rb", "existing content"
121+
end
122+
123+
it "raises error" do
124+
expect { subject.call(name: "books") }
125+
.to raise_error(Hanami::CLI::FileAlreadyExistsError)
126+
end
127+
end
117128
end
118129

119130
context "generating for a slice" do
@@ -178,5 +189,16 @@ class Drafts < Main::DB::Relation
178189
expect(Hanami.app.root.join("slices/main/relations/book/drafts.rb").read).to eq(relation_file)
179190
expect(output).to include("Created slices/main/relations/book/drafts.rb")
180191
end
192+
193+
context "with existing file" do
194+
before do
195+
write "slices/main/relations/books.rb", "existing content"
196+
end
197+
198+
it "raises error" do
199+
expect { subject.call(name: "books", slice: "main") }
200+
.to raise_error(Hanami::CLI::FileAlreadyExistsError)
201+
end
202+
end
181203
end
182204
end

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ class HardcoverRepo < Test::DB::Repo
9292
expect(fs.read("app/repos/books/published/hardcover_repo.rb")).to eq(repo_file)
9393
expect(output).to include("Created app/repos/books/published/hardcover_repo.rb")
9494
end
95+
96+
context "with existing file" do
97+
before do
98+
fs.write("app/repos/book_repo.rb", "existing content")
99+
end
100+
101+
it "raises error" do
102+
expect {
103+
subject.call(name: "books")
104+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
105+
end
106+
end
95107
end
96108

97109
context "generating for a slice" do
@@ -134,5 +146,17 @@ class DraftRepo < Main::DB::Repo
134146
expect(fs.read("slices/main/repos/book/draft_repo.rb")).to eq(repo_file)
135147
expect(output).to include("Created slices/main/repos/book/draft_repo.rb")
136148
end
149+
150+
context "with existing file" do
151+
before do
152+
fs.write("slices/main/repos/book_repo.rb", "existing content")
153+
end
154+
155+
it "raises error" do
156+
expect {
157+
subject.call(name: "books", slice: "main")
158+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
159+
end
160+
end
137161
end
138162
end

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ class Hardcover < Test::DB::Struct
7272
expect(fs.read("app/structs/book/published/hardcover.rb")).to eq(struct_file)
7373
expect(output).to include("Created app/structs/book/published/hardcover.rb")
7474
end
75+
76+
context "with existing file" do
77+
before do
78+
fs.write("app/structs/book/published/hardcover.rb", "existing content")
79+
end
80+
81+
it "raises error" do
82+
expect {
83+
subject.call(name: "book/published/hardcover")
84+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
85+
end
86+
end
7587
end
7688

7789
context "generating for a slice" do
@@ -114,5 +126,17 @@ class DraftBook < Main::DB::Struct
114126
expect(fs.read("slices/main/structs/book/draft_book.rb")).to eq(struct_file)
115127
expect(output).to include("Created slices/main/structs/book/draft_book.rb")
116128
end
129+
130+
context "with existing file" do
131+
before do
132+
fs.write("slices/main/structs/book/draft_book.rb", "existing content")
133+
end
134+
135+
it "raises error" do
136+
expect {
137+
subject.call(name: "book.draft_book", slice: "main")
138+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
139+
end
140+
end
117141
end
118142
end

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ class Index < Test::View
9191
expect(output).to include("Created app/templates/special/users/index.html.erb")
9292
end
9393
end
94+
95+
context "with existing file" do
96+
before do
97+
within_application_directory do
98+
fs.write("app/views/users/index.rb", "existing content")
99+
end
100+
end
101+
102+
it "raises error" do
103+
within_application_directory do
104+
expect {
105+
subject.call(name: "users.index")
106+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
107+
end
108+
end
109+
end
94110
end
95111

96112
context "generating for a slice" do
@@ -127,6 +143,23 @@ class Index < Main::View
127143
expect(output).to include("Created slices/main/templates/users/index.html.erb")
128144
end
129145
end
146+
147+
context "with existing file" do
148+
before do
149+
within_application_directory do
150+
fs.mkdir("slices/main")
151+
fs.write("slices/main/views/users/index.rb", "existing content")
152+
end
153+
end
154+
155+
it "raises error" do
156+
within_application_directory do
157+
expect {
158+
subject.call(name: "users.index", slice: "main")
159+
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
160+
end
161+
end
162+
end
130163
end
131164

132165
private

0 commit comments

Comments
 (0)