hanami 2.0.0.alpha8 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +442 -241
  3. data/FEATURES.md +30 -9
  4. data/README.md +1 -3
  5. data/hanami.gemspec +21 -11
  6. data/lib/hanami/app.rb +141 -0
  7. data/lib/hanami/assets/application_configuration.rb +10 -4
  8. data/lib/hanami/configuration/actions/content_security_policy.rb +5 -5
  9. data/lib/hanami/configuration/actions/cookies.rb +2 -2
  10. data/lib/hanami/configuration/actions.rb +10 -4
  11. data/lib/hanami/configuration/logger.rb +4 -4
  12. data/lib/hanami/configuration/router.rb +2 -6
  13. data/lib/hanami/configuration/sessions.rb +1 -1
  14. data/lib/hanami/configuration/views.rb +9 -4
  15. data/lib/hanami/configuration.rb +118 -46
  16. data/lib/hanami/constants.rb +24 -2
  17. data/lib/hanami/errors.rb +1 -1
  18. data/lib/hanami/{application → extensions}/action/slice_configured_action.rb +9 -9
  19. data/lib/hanami/extensions/action.rb +79 -0
  20. data/lib/hanami/extensions/view/context.rb +106 -0
  21. data/lib/hanami/{application → extensions}/view/slice_configured_context.rb +10 -10
  22. data/lib/hanami/{application → extensions}/view/slice_configured_view.rb +12 -6
  23. data/lib/hanami/extensions/view.rb +33 -0
  24. data/lib/hanami/extensions.rb +10 -0
  25. data/lib/hanami/providers/inflector.rb +13 -0
  26. data/lib/hanami/providers/logger.rb +13 -0
  27. data/lib/hanami/providers/rack.rb +27 -0
  28. data/lib/hanami/providers/routes.rb +33 -0
  29. data/lib/hanami/providers/settings.rb +23 -0
  30. data/lib/hanami/rake_tasks.rb +61 -0
  31. data/lib/hanami/routes.rb +51 -0
  32. data/lib/hanami/server.rb +1 -1
  33. data/lib/hanami/settings/dotenv_store.rb +58 -0
  34. data/lib/hanami/settings.rb +90 -0
  35. data/lib/hanami/setup.rb +4 -2
  36. data/lib/hanami/{application → slice}/router.rb +18 -13
  37. data/lib/hanami/slice/routes_helper.rb +37 -0
  38. data/lib/hanami/{application → slice}/routing/middleware/stack.rb +43 -5
  39. data/lib/hanami/slice/routing/resolver.rb +97 -0
  40. data/lib/hanami/{application → slice}/view_name_inferrer.rb +3 -3
  41. data/lib/hanami/slice.rb +246 -73
  42. data/lib/hanami/slice_configurable.rb +4 -17
  43. data/lib/hanami/slice_name.rb +6 -6
  44. data/lib/hanami/slice_registrar.rb +119 -0
  45. data/lib/hanami/version.rb +1 -1
  46. data/lib/hanami/web/rack_logger.rb +1 -1
  47. data/lib/hanami.rb +34 -26
  48. data/spec/integration/application_middleware_stack_spec.rb +84 -0
  49. data/spec/integration/assets/cdn_spec.rb +48 -0
  50. data/spec/integration/assets/fingerprint_spec.rb +42 -0
  51. data/spec/integration/assets/helpers_spec.rb +50 -0
  52. data/spec/integration/assets/serve_spec.rb +70 -0
  53. data/spec/integration/assets/subresource_integrity_spec.rb +54 -0
  54. data/spec/integration/body_parsers_spec.rb +50 -0
  55. data/spec/integration/cli/assets/precompile_spec.rb +147 -0
  56. data/spec/integration/cli/assets_spec.rb +14 -0
  57. data/spec/integration/cli/console_spec.rb +105 -0
  58. data/spec/integration/cli/db/apply_spec.rb +74 -0
  59. data/spec/integration/cli/db/console_spec.rb +40 -0
  60. data/spec/integration/cli/db/create_spec.rb +50 -0
  61. data/spec/integration/cli/db/drop_spec.rb +54 -0
  62. data/spec/integration/cli/db/migrate_spec.rb +108 -0
  63. data/spec/integration/cli/db/prepare_spec.rb +36 -0
  64. data/spec/integration/cli/db/rollback_spec.rb +96 -0
  65. data/spec/integration/cli/db/version_spec.rb +38 -0
  66. data/spec/integration/cli/db_spec.rb +21 -0
  67. data/spec/integration/cli/destroy/action_spec.rb +143 -0
  68. data/spec/integration/cli/destroy/app_spec.rb +118 -0
  69. data/spec/integration/cli/destroy/mailer_spec.rb +74 -0
  70. data/spec/integration/cli/destroy/migration_spec.rb +70 -0
  71. data/spec/integration/cli/destroy/model_spec.rb +113 -0
  72. data/spec/integration/cli/destroy_spec.rb +18 -0
  73. data/spec/integration/cli/generate/action_spec.rb +469 -0
  74. data/spec/integration/cli/generate/app_spec.rb +215 -0
  75. data/spec/integration/cli/generate/mailer_spec.rb +189 -0
  76. data/spec/integration/cli/generate/migration_spec.rb +72 -0
  77. data/spec/integration/cli/generate/model_spec.rb +290 -0
  78. data/spec/integration/cli/generate/secret_spec.rb +56 -0
  79. data/spec/integration/cli/generate_spec.rb +19 -0
  80. data/spec/integration/cli/new/database_spec.rb +235 -0
  81. data/spec/integration/cli/new/hanami_head_spec.rb +27 -0
  82. data/spec/integration/cli/new/template_spec.rb +118 -0
  83. data/spec/integration/cli/new/test_spec.rb +274 -0
  84. data/spec/integration/cli/new_spec.rb +970 -0
  85. data/spec/integration/cli/plugins_spec.rb +39 -0
  86. data/spec/integration/cli/routes_spec.rb +49 -0
  87. data/spec/integration/cli/server_spec.rb +626 -0
  88. data/spec/integration/cli/version_spec.rb +85 -0
  89. data/spec/integration/early_hints_spec.rb +35 -0
  90. data/spec/integration/handle_exceptions_spec.rb +244 -0
  91. data/spec/integration/head_spec.rb +89 -0
  92. data/spec/integration/http_headers_spec.rb +29 -0
  93. data/spec/integration/mailer_spec.rb +32 -0
  94. data/spec/integration/middleware_spec.rb +81 -0
  95. data/spec/integration/mount_applications_spec.rb +88 -0
  96. data/spec/integration/project_initializers_spec.rb +40 -0
  97. data/spec/integration/rackup_spec.rb +35 -0
  98. data/spec/integration/rake/with_minitest_spec.rb +67 -0
  99. data/spec/integration/rake/with_rspec_spec.rb +69 -0
  100. data/spec/integration/routing_helpers_spec.rb +61 -0
  101. data/spec/integration/security/content_security_policy_spec.rb +46 -0
  102. data/spec/integration/security/csrf_protection_spec.rb +42 -0
  103. data/spec/integration/security/force_ssl_spec.rb +29 -0
  104. data/spec/integration/security/x_content_type_options_spec.rb +46 -0
  105. data/spec/integration/security/x_frame_options_spec.rb +46 -0
  106. data/spec/integration/security/x_xss_protection_spec.rb +46 -0
  107. data/spec/integration/send_file_spec.rb +51 -0
  108. data/spec/integration/sessions_spec.rb +247 -0
  109. data/spec/integration/static_middleware_spec.rb +21 -0
  110. data/spec/integration/streaming_spec.rb +41 -0
  111. data/spec/integration/unsafe_send_file_spec.rb +52 -0
  112. data/spec/isolation/hanami/application/already_configured_spec.rb +19 -0
  113. data/spec/isolation/hanami/application/inherit_anonymous_class_spec.rb +10 -0
  114. data/spec/isolation/hanami/application/inherit_concrete_class_spec.rb +14 -0
  115. data/spec/isolation/hanami/application/not_configured_spec.rb +9 -0
  116. data/spec/isolation/hanami/application/routes/configured_spec.rb +44 -0
  117. data/spec/isolation/hanami/application/routes/not_configured_spec.rb +16 -0
  118. data/spec/isolation/hanami/boot/success_spec.rb +50 -0
  119. data/spec/new_integration/action/configuration_spec.rb +26 -0
  120. data/spec/new_integration/action/cookies_spec.rb +58 -0
  121. data/spec/new_integration/action/csrf_protection_spec.rb +54 -0
  122. data/spec/new_integration/action/routes_spec.rb +73 -0
  123. data/spec/new_integration/action/sessions_spec.rb +50 -0
  124. data/spec/new_integration/action/view_integration_spec.rb +165 -0
  125. data/spec/new_integration/action/view_rendering/automatic_rendering_spec.rb +247 -0
  126. data/spec/new_integration/action/view_rendering/paired_view_inference_spec.rb +115 -0
  127. data/spec/new_integration/action/view_rendering_spec.rb +107 -0
  128. data/spec/new_integration/code_loading/loading_from_app_spec.rb +152 -0
  129. data/spec/new_integration/code_loading/loading_from_slice_spec.rb +165 -0
  130. data/spec/new_integration/container/application_routes_helper_spec.rb +48 -0
  131. data/spec/new_integration/container/auto_injection_spec.rb +53 -0
  132. data/spec/new_integration/container/auto_registration_spec.rb +86 -0
  133. data/spec/new_integration/container/autoloader_spec.rb +80 -0
  134. data/spec/new_integration/container/imports_spec.rb +253 -0
  135. data/spec/new_integration/container/prepare_container_spec.rb +123 -0
  136. data/spec/new_integration/container/shutdown_spec.rb +91 -0
  137. data/spec/new_integration/container/standard_bootable_components_spec.rb +124 -0
  138. data/spec/new_integration/rack_app/middleware_spec.rb +215 -0
  139. data/spec/new_integration/rack_app/non_booted_rack_app_spec.rb +105 -0
  140. data/spec/new_integration/rack_app/rack_app_spec.rb +524 -0
  141. data/spec/new_integration/settings_spec.rb +115 -0
  142. data/spec/new_integration/slices/external_slice_spec.rb +92 -0
  143. data/spec/new_integration/slices/slice_configuration_spec.rb +40 -0
  144. data/spec/new_integration/slices/slice_routing_spec.rb +226 -0
  145. data/spec/new_integration/slices/slice_settings_spec.rb +141 -0
  146. data/spec/new_integration/slices_spec.rb +101 -0
  147. data/spec/new_integration/view/configuration_spec.rb +49 -0
  148. data/spec/new_integration/view/context/assets_spec.rb +67 -0
  149. data/spec/new_integration/view/context/inflector_spec.rb +48 -0
  150. data/spec/new_integration/view/context/request_spec.rb +61 -0
  151. data/spec/new_integration/view/context/routes_spec.rb +86 -0
  152. data/spec/new_integration/view/context/settings_spec.rb +50 -0
  153. data/spec/new_integration/view/inflector_spec.rb +57 -0
  154. data/spec/new_integration/view/part_namespace_spec.rb +96 -0
  155. data/spec/new_integration/view/path_spec.rb +56 -0
  156. data/spec/new_integration/view/template_spec.rb +68 -0
  157. data/spec/new_integration/view/views_spec.rb +103 -0
  158. data/spec/spec_helper.rb +16 -0
  159. data/spec/support/app_integration.rb +91 -0
  160. data/spec/support/coverage.rb +1 -0
  161. data/spec/support/fixtures/hanami-plugin/Gemfile +8 -0
  162. data/spec/support/fixtures/hanami-plugin/README.md +35 -0
  163. data/spec/support/fixtures/hanami-plugin/Rakefile +4 -0
  164. data/spec/support/fixtures/hanami-plugin/bin/console +15 -0
  165. data/spec/support/fixtures/hanami-plugin/bin/setup +8 -0
  166. data/spec/support/fixtures/hanami-plugin/hanami-plugin.gemspec +28 -0
  167. data/spec/support/fixtures/hanami-plugin/lib/hanami/plugin/cli.rb +19 -0
  168. data/spec/support/fixtures/hanami-plugin/lib/hanami/plugin/version.rb +7 -0
  169. data/spec/support/fixtures/hanami-plugin/lib/hanami/plugin.rb +8 -0
  170. data/spec/support/rspec.rb +27 -0
  171. data/spec/support/shared_examples/cli/generate/app.rb +494 -0
  172. data/spec/support/shared_examples/cli/generate/migration.rb +32 -0
  173. data/spec/support/shared_examples/cli/generate/model.rb +81 -0
  174. data/spec/support/shared_examples/cli/new.rb +97 -0
  175. data/spec/unit/hanami/configuration/actions/content_security_policy_spec.rb +102 -0
  176. data/spec/unit/hanami/configuration/actions/cookies_spec.rb +46 -0
  177. data/spec/unit/hanami/configuration/actions/csrf_protection_spec.rb +57 -0
  178. data/spec/unit/hanami/configuration/actions/default_values_spec.rb +52 -0
  179. data/spec/unit/hanami/configuration/actions/sessions_spec.rb +50 -0
  180. data/spec/unit/hanami/configuration/actions_spec.rb +78 -0
  181. data/spec/unit/hanami/configuration/base_url_spec.rb +25 -0
  182. data/spec/unit/hanami/configuration/inflector_spec.rb +35 -0
  183. data/spec/unit/hanami/configuration/logger_spec.rb +203 -0
  184. data/spec/unit/hanami/configuration/views_spec.rb +120 -0
  185. data/spec/unit/hanami/configuration_spec.rb +43 -0
  186. data/spec/unit/hanami/env_spec.rb +54 -0
  187. data/spec/unit/hanami/routes_spec.rb +25 -0
  188. data/spec/unit/hanami/settings/dotenv_store_spec.rb +119 -0
  189. data/spec/unit/hanami/settings_spec.rb +56 -0
  190. data/spec/unit/hanami/slice_configurable_spec.rb +104 -0
  191. data/spec/unit/hanami/slice_name_spec.rb +47 -0
  192. data/spec/unit/hanami/slice_spec.rb +17 -0
  193. data/spec/unit/hanami/version_spec.rb +7 -0
  194. data/spec/unit/hanami/web/rack_logger_spec.rb +78 -0
  195. metadata +353 -57
  196. data/lib/hanami/application/action.rb +0 -72
  197. data/lib/hanami/application/container/providers/inflector.rb +0 -7
  198. data/lib/hanami/application/container/providers/logger.rb +0 -7
  199. data/lib/hanami/application/container/providers/rack_logger.rb +0 -15
  200. data/lib/hanami/application/container/providers/rack_monitor.rb +0 -12
  201. data/lib/hanami/application/container/providers/routes_helper.rb +0 -9
  202. data/lib/hanami/application/container/providers/settings.rb +0 -7
  203. data/lib/hanami/application/routes.rb +0 -55
  204. data/lib/hanami/application/routes_helper.rb +0 -34
  205. data/lib/hanami/application/routing/resolver/node.rb +0 -50
  206. data/lib/hanami/application/routing/resolver/trie.rb +0 -59
  207. data/lib/hanami/application/routing/resolver.rb +0 -87
  208. data/lib/hanami/application/routing/router.rb +0 -36
  209. data/lib/hanami/application/settings/dotenv_store.rb +0 -60
  210. data/lib/hanami/application/settings.rb +0 -93
  211. data/lib/hanami/application/slice_registrar.rb +0 -106
  212. data/lib/hanami/application/view/context.rb +0 -95
  213. data/lib/hanami/application/view.rb +0 -24
  214. data/lib/hanami/application.rb +0 -273
  215. data/lib/hanami/cli/application/cli.rb +0 -40
  216. data/lib/hanami/cli/application/command.rb +0 -47
  217. data/lib/hanami/cli/application/commands/console.rb +0 -81
  218. data/lib/hanami/cli/application/commands.rb +0 -16
  219. data/lib/hanami/cli/base_command.rb +0 -48
  220. data/lib/hanami/cli/commands/command.rb +0 -171
  221. data/lib/hanami/cli/commands/server.rb +0 -88
  222. data/lib/hanami/cli/commands.rb +0 -65
  223. data/lib/hanami/configuration/middleware.rb +0 -20
  224. data/lib/hanami/configuration/source_dirs.rb +0 -42
@@ -0,0 +1,469 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe "hanami generate", type: :integration do
4
+ describe "action" do
5
+ it "generates action" do
6
+ with_project("bookshelf_generate_action") do
7
+ output = [
8
+ "create spec/web/controllers/authors/index_spec.rb",
9
+ "create apps/web/controllers/authors/index.rb",
10
+ "create apps/web/views/authors/index.rb",
11
+ "create apps/web/templates/authors/index.html.erb",
12
+ "create spec/web/views/authors/index_spec.rb",
13
+ "insert apps/web/config/routes.rb"
14
+ ]
15
+
16
+ run_cmd "hanami generate action web authors#index", output
17
+
18
+ #
19
+ # apps/web/controllers/authors/index.rb
20
+ #
21
+ expect("apps/web/controllers/authors/index.rb").to have_file_content <<~END
22
+ module Web
23
+ module Controllers
24
+ module Authors
25
+ class Index
26
+ include Web::Action
27
+
28
+ def call(params)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ END
35
+
36
+ #
37
+ # apps/web/views/authors/index.rb
38
+ #
39
+ expect("apps/web/views/authors/index.rb").to have_file_content <<~END
40
+ module Web
41
+ module Views
42
+ module Authors
43
+ class Index
44
+ include Web::View
45
+ end
46
+ end
47
+ end
48
+ end
49
+ END
50
+
51
+ #
52
+ # apps/web/config/routes.rb
53
+ #
54
+ expect("apps/web/config/routes.rb").to have_file_content(%r{get '/authors', to: 'authors#index'})
55
+ end
56
+ end
57
+
58
+ it "generates namespaced action" do
59
+ with_project("bookshelf_generate_action") do
60
+ output = [
61
+ "create spec/web/controllers/api/authors/index_spec.rb",
62
+ "create apps/web/controllers/api/authors/index.rb",
63
+ "create apps/web/views/api/authors/index.rb",
64
+ "create apps/web/templates/api/authors/index.html.erb",
65
+ "create spec/web/views/api/authors/index_spec.rb",
66
+ "insert apps/web/config/routes.rb"
67
+ ]
68
+
69
+ run_cmd "hanami generate action web api/authors#index", output
70
+
71
+ #
72
+ # apps/web/controllers/api/authors/index.rb
73
+ #
74
+ expect("apps/web/controllers/api/authors/index.rb").to have_file_content <<~END
75
+ module Web
76
+ module Controllers
77
+ module Api
78
+ module Authors
79
+ class Index
80
+ include Web::Action
81
+
82
+ def call(params)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ END
90
+
91
+ #
92
+ # apps/web/views/api/authors/index.rb
93
+ #
94
+ expect("apps/web/views/api/authors/index.rb").to have_file_content <<~END
95
+ module Web
96
+ module Views
97
+ module Api
98
+ module Authors
99
+ class Index
100
+ include Web::View
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ END
107
+
108
+ #
109
+ # apps/web/config/routes.rb
110
+ #
111
+ expect("apps/web/config/routes.rb").to have_file_content(%r{get '/api/authors', to: 'api/authors#index'})
112
+ end
113
+ end
114
+
115
+ it "generates non-RESTful actions" do
116
+ with_project do
117
+ run_cmd "hanami generate action web sessions#sign_out"
118
+
119
+ #
120
+ # apps/web/config/routes.rb
121
+ #
122
+ expect("apps/web/config/routes.rb").to have_file_content(%r{get '/sessions/sign_out', to: 'sessions#sign_out'})
123
+ end
124
+ end
125
+
126
+ it "fails with missing arguments" do
127
+ with_project("bookshelf_generate_action_without_args") do
128
+ output = <<~OUT
129
+ ERROR: "hanami generate action" was called with no arguments
130
+ Usage: "hanami generate action APP ACTION"
131
+ OUT
132
+ run_cmd "hanami generate action", output, exit_status: 1
133
+ end
134
+ end
135
+
136
+ it "fails with missing app" do
137
+ with_project("bookshelf_generate_action_without_app") do
138
+ output = <<~OUT
139
+ ERROR: "hanami generate action" was called with arguments ["home#index"]
140
+ Usage: "hanami generate action APP ACTION"
141
+ OUT
142
+
143
+ run_cmd "hanami generate action home#index", output, exit_status: 1
144
+ end
145
+ end
146
+
147
+ it "fails with unknown app" do
148
+ with_project("bookshelf_generate_action_with_unknown_app") do
149
+ output = "`foo' is not a valid APP. Please specify one of: `web'"
150
+
151
+ run_cmd "hanami generate action foo home#index", output, exit_status: 1
152
+ end
153
+ end
154
+
155
+ context "--url" do
156
+ it "generates action" do
157
+ with_project("bookshelf_generate_action_url") do
158
+ output = [
159
+ "insert apps/web/config/routes.rb"
160
+ ]
161
+
162
+ run_cmd "hanami generate action web home#index --url=/", output
163
+
164
+ #
165
+ # apps/web/config/routes.rb
166
+ #
167
+ expect("apps/web/config/routes.rb").to have_file_content(%r{get '/', to: 'home#index'})
168
+ end
169
+ end
170
+
171
+ it "fails with missing argument" do
172
+ with_project("bookshelf_generate_action_missing_url") do
173
+ output = "`' is not a valid URL"
174
+ run_cmd "hanami generate action web books#create --url=", output, exit_status: 1
175
+ end
176
+ end
177
+ end
178
+
179
+ context "--skip-view" do
180
+ it "generates action" do
181
+ with_project("bookshelf_generate_action_skip_view") do
182
+ output = [
183
+ "create apps/web/controllers/status/check.rb",
184
+ "create spec/web/controllers/status/check_spec.rb",
185
+ "insert apps/web/config/routes.rb"
186
+ ]
187
+ run_cmd "hanami generate action web status#check --skip-view", output
188
+
189
+ #
190
+ # apps/web/controllers/status/check.rb
191
+ #
192
+ expect("apps/web/controllers/status/check.rb").to have_file_content <<~END
193
+ module Web
194
+ module Controllers
195
+ module Status
196
+ class Check
197
+ include Web::Action
198
+
199
+ def call(params)
200
+ self.body = 'OK'
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
206
+ END
207
+ end
208
+ end
209
+
210
+ it "generates namespaced action" do
211
+ with_project("bookshelf_generate_action_skip_view") do
212
+ output = [
213
+ "create apps/web/controllers/api/authors/index.rb",
214
+ "create spec/web/controllers/api/authors/index_spec.rb",
215
+ "insert apps/web/config/routes.rb"
216
+ ]
217
+ run_cmd "hanami generate action web api/authors#index --skip-view", output
218
+
219
+ #
220
+ # apps/web/controllers/status/check.rb
221
+ #
222
+ expect("apps/web/controllers/api/authors/index.rb").to have_file_content <<~END
223
+ module Web
224
+ module Controllers
225
+ module Api
226
+ module Authors
227
+ class Index
228
+ include Web::Action
229
+
230
+ def call(params)
231
+ self.body = 'OK'
232
+ end
233
+ end
234
+ end
235
+ end
236
+ end
237
+ end
238
+ END
239
+ end
240
+ end
241
+ end
242
+
243
+ context "--method" do
244
+ it "generates action" do
245
+ with_project("bookshelf_generate_action_method") do
246
+ output = [
247
+ "insert apps/web/config/routes.rb"
248
+ ]
249
+
250
+ run_cmd "hanami generate action web books#create --method=POST", output
251
+
252
+ #
253
+ # apps/web/config/routes.rb
254
+ #
255
+ expect("apps/web/config/routes.rb").to have_file_content(%r{post '/books', to: 'books#create'})
256
+ end
257
+ end
258
+
259
+ it "fails with missing argument" do
260
+ with_project("bookshelf_generate_action_missing_method") do
261
+ output = "`' is not a valid HTTP method. Please use one of: `GET' `POST' `PUT' `DELETE' `HEAD' `OPTIONS' `TRACE' `PATCH' `OPTIONS' `LINK' `UNLINK'"
262
+ run_cmd "hanami generate action web books#create --method=", output, exit_status: 1
263
+ end
264
+ end
265
+
266
+ it "fails with unknown argument" do
267
+ with_project('bookshelf_generate_action_unknown_method') do
268
+ output = "`FOO' is not a valid HTTP method. Please use one of: `GET' `POST' `PUT' `DELETE' `HEAD' `OPTIONS' `TRACE' `PATCH' `OPTIONS' `LINK' `UNLINK'"
269
+ run_cmd "hanami generate action web books#create --method=FOO", output, exit_status: 1
270
+ end
271
+ end
272
+ end
273
+
274
+ context "erb" do
275
+ it "generates action" do
276
+ with_project("bookshelf_generate_action_erb", template: "erb") do
277
+ output = [
278
+ "create apps/web/templates/books/index.html.erb"
279
+ ]
280
+
281
+ run_cmd "hanami generate action web books#index", output
282
+
283
+ #
284
+ # apps/web/templates/books/index.html.erb
285
+ #
286
+ expect("apps/web/templates/books/index.html.erb").to have_file_content <<~END
287
+ END
288
+
289
+ #
290
+ # spec/web/views/books/index_spec.rb
291
+ #
292
+ expect("spec/web/views/books/index_spec.rb").to have_file_content %r{'apps/web/templates/books/index.html.erb'}
293
+ end
294
+ end
295
+ end # erb
296
+
297
+ context "haml" do
298
+ it "generates action" do
299
+ with_project("bookshelf_generate_action_haml", template: "haml") do
300
+ output = [
301
+ "create apps/web/templates/books/index.html.haml"
302
+ ]
303
+
304
+ run_cmd "hanami generate action web books#index", output
305
+
306
+ #
307
+ # apps/web/templates/books/index.html.haml
308
+ #
309
+ expect("apps/web/templates/books/index.html.haml").to have_file_content <<~END
310
+ END
311
+
312
+ #
313
+ # spec/web/views/books/index_spec.rb
314
+ #
315
+ expect("spec/web/views/books/index_spec.rb").to have_file_content(%r{'apps/web/templates/books/index.html.haml'})
316
+ end
317
+ end
318
+ end # haml
319
+
320
+ context "slim" do
321
+ it "generates action" do
322
+ with_project("bookshelf_generate_action_slim", template: "slim") do
323
+ output = [
324
+ "create apps/web/templates/books/index.html.slim"
325
+ ]
326
+
327
+ run_cmd "hanami generate action web books#index", output
328
+
329
+ #
330
+ # apps/web/templates/books/index.html.slim
331
+ #
332
+ expect("apps/web/templates/books/index.html.slim").to have_file_content <<~END
333
+ END
334
+
335
+ #
336
+ # spec/web/views/books/index_spec.rb
337
+ #
338
+ expect("spec/web/views/books/index_spec.rb").to have_file_content %r{'apps/web/templates/books/index.html.slim'}
339
+ end
340
+ end
341
+ end # slim
342
+
343
+ context "minitest" do
344
+ it "generates action" do
345
+ with_project("bookshelf_generate_action_minitest", test: "minitest") do
346
+ output = [
347
+ "create spec/web/controllers/books/index_spec.rb",
348
+ "create spec/web/views/books/index_spec.rb"
349
+ ]
350
+
351
+ run_cmd "hanami generate action web books#index", output
352
+
353
+ #
354
+ # spec/web/controllers/books/index_spec.rb
355
+ #
356
+ expect("spec/web/controllers/books/index_spec.rb").to have_file_content <<~END
357
+ require_relative '../../../spec_helper'
358
+
359
+ describe Web::Controllers::Books::Index do
360
+ let(:action) { Web::Controllers::Books::Index.new }
361
+ let(:params) { Hash[] }
362
+
363
+ it 'is successful' do
364
+ response = action.call(params)
365
+ _(response[0]).must_equal 200
366
+ end
367
+ end
368
+ END
369
+
370
+ #
371
+ # spec/web/views/books/index_spec.rb
372
+ #
373
+ expect("spec/web/views/books/index_spec.rb").to have_file_content <<~END
374
+ require_relative '../../../spec_helper'
375
+
376
+ describe Web::Views::Books::Index do
377
+ let(:exposures) { Hash[format: :html] }
378
+ let(:template) { Hanami::View::Template.new('apps/web/templates/books/index.html.erb') }
379
+ let(:view) { Web::Views::Books::Index.new(template, exposures) }
380
+ let(:rendered) { view.render }
381
+
382
+ it 'exposes #format' do
383
+ _(view.format).must_equal exposures.fetch(:format)
384
+ end
385
+ end
386
+ END
387
+ end
388
+ end
389
+ end # minitest
390
+
391
+ context "rspec" do
392
+ it "generates action" do
393
+ with_project("bookshelf_generate_action_rspec", test: "rspec") do
394
+ output = [
395
+ "create spec/web/controllers/books/index_spec.rb",
396
+ "create spec/web/views/books/index_spec.rb"
397
+ ]
398
+
399
+ run_cmd "hanami generate action web books#index", output
400
+
401
+ #
402
+ # spec/web/controllers/books/index_spec.rb
403
+ #
404
+ expect("spec/web/controllers/books/index_spec.rb").to have_file_content <<~END
405
+ RSpec.describe Web::Controllers::Books::Index, type: :action do
406
+ let(:action) { described_class.new }
407
+ let(:params) { Hash[] }
408
+
409
+ it 'is successful' do
410
+ response = action.call(params)
411
+ expect(response[0]).to eq 200
412
+ end
413
+ end
414
+ END
415
+
416
+ #
417
+ # spec/web/views/books/index_spec.rb
418
+ #
419
+ expect("spec/web/views/books/index_spec.rb").to have_file_content <<~END
420
+ RSpec.describe Web::Views::Books::Index, type: :view do
421
+ let(:exposures) { Hash[format: :html] }
422
+ let(:template) { Hanami::View::Template.new('apps/web/templates/books/index.html.erb') }
423
+ let(:view) { described_class.new(template, exposures) }
424
+ let(:rendered) { view.render }
425
+
426
+ it 'exposes #format' do
427
+ expect(view.format).to eq exposures.fetch(:format)
428
+ end
429
+ end
430
+ END
431
+ end
432
+ end
433
+ end # rspec
434
+
435
+ it "prints help message" do
436
+ with_project do
437
+ output = <<~OUT
438
+ Command:
439
+ hanami generate action
440
+
441
+ Usage:
442
+ hanami generate action APP ACTION
443
+
444
+ Description:
445
+ Generate an action for app
446
+
447
+ Arguments:
448
+ APP # REQUIRED The app name (eg. `web`)
449
+ ACTION # REQUIRED The action name (eg. `home#index`)
450
+
451
+ Options:
452
+ --url=VALUE # The action URL
453
+ --method=VALUE # The action HTTP method
454
+ --[no-]skip-view # Skip view and template, default: false
455
+ --help, -h # Print this help
456
+
457
+ Examples:
458
+ hanami generate action web home#index # Basic usage
459
+ hanami generate action admin home#index # Generate for `admin` app
460
+ hanami generate action web home#index --url=/ # Specify URL
461
+ hanami generate action web sessions#destroy --method=GET # Specify HTTP method
462
+ hanami generate action web books#create --skip-view # Skip view and template
463
+ OUT
464
+
465
+ run_cmd 'hanami generate action --help', output
466
+ end
467
+ end
468
+ end # action
469
+ end
@@ -0,0 +1,215 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/utils/string"
4
+
5
+ RSpec.describe "hanami generate", type: :integration do
6
+ describe "app" do
7
+ context "with app name" do
8
+ it_behaves_like "a new app" do
9
+ let(:input) { "admin" }
10
+ end
11
+ end
12
+
13
+ context "with underscored app name" do
14
+ it_behaves_like "a new app" do
15
+ let(:input) { "cool_app" }
16
+ end
17
+ end
18
+
19
+ context "with dashed app name" do
20
+ it_behaves_like "a new app" do
21
+ let(:input) { "awesome-app" }
22
+ end
23
+ end
24
+
25
+ context "with camel case app name" do
26
+ it_behaves_like "a new app" do
27
+ let(:input) { "CaMElAPp" }
28
+ end
29
+ end
30
+
31
+ context "without require_relative" do
32
+ it "generates app" do
33
+ with_project("bookshelf_generate_app_without_require_relative") do
34
+ app = "no_req_relative"
35
+ app_name = Hanami::Utils::String.new(app).classify
36
+ output = [
37
+ "insert config/environment.rb"
38
+ ]
39
+
40
+ File.write(
41
+ "config/environment.rb",
42
+ File
43
+ .read("config/environment.rb")
44
+ .lines
45
+ .reject { |l| l[/^require_relative '.*'\n$/] }
46
+ .reject { |l| l[/^ mount Web::App, at: '\/'\n$/] }
47
+ .join("")
48
+ )
49
+
50
+ run_cmd "hanami generate app #{app}", output
51
+
52
+ #
53
+ # config/environment.rb
54
+ #
55
+ expect("config/environment.rb").to have_file_content(%r{require_relative '../apps/#{app}/app'})
56
+ expect("config/environment.rb").to have_file_content(%r{mount #{app_name}::App, at: '/no_req_relative'})
57
+ end
58
+ end
59
+ end
60
+
61
+ context "--app-base-url" do
62
+ it "generates app" do
63
+ with_project("bookshelf_generate_app_app_base_url") do
64
+ app = "api"
65
+ app_name = Hanami::Utils::String.new(app).classify
66
+ output = [
67
+ "insert config/environment.rb"
68
+ ]
69
+
70
+ run_cmd "hanami generate app #{app} --app-base-url=/api/v1", output
71
+
72
+ #
73
+ # config/environment.rb
74
+ #
75
+ expect("config/environment.rb").to have_file_content(%r{require_relative '../apps/#{app}/app'})
76
+ expect("config/environment.rb").to have_file_content(%r{mount #{app_name}::App, at: '/api/v1'})
77
+ end
78
+ end
79
+
80
+ it "fails with missing argument" do
81
+ with_project("bookshelf_generate_app_missing_app_base_url") do
82
+ output = "`' is not a valid URL"
83
+ run_cmd "hanami generate app foo --app-base-url=", output, exit_status: 1
84
+ end
85
+ end
86
+ end
87
+
88
+ context "erb" do
89
+ it "generates app" do
90
+ with_project("bookshelf_generate_app_erb", template: :erb) do
91
+ app = "admin"
92
+ app_name = Hanami::Utils::String.new(app).classify
93
+ output = [
94
+ "create apps/#{app}/templates/app.html.erb"
95
+ ]
96
+
97
+ run_cmd "hanami generate app #{app}", output
98
+
99
+ #
100
+ # apps/admin/templates/app.html.erb
101
+ #
102
+ expect("apps/admin/templates/app.html.erb").to have_file_content <<~END
103
+ <!DOCTYPE html>
104
+ <html>
105
+ <head>
106
+ <title>#{app_name}</title>
107
+ <%= favicon %>
108
+ </head>
109
+ <body>
110
+ <%= yield %>
111
+ </body>
112
+ </html>
113
+ END
114
+ #
115
+ # spec/admin/views/app_layout_spec.rb
116
+ #
117
+ expect("spec/admin/views/app_layout_spec.rb").to have_file_content(%r{Admin::Views::AppLayout})
118
+ end
119
+ end
120
+ end # erb
121
+
122
+ context "haml" do
123
+ it "generates app" do
124
+ with_project("bookshelf_generate_app_haml", template: :haml) do
125
+ app = "admin"
126
+ app_name = Hanami::Utils::String.new(app).classify
127
+ output = [
128
+ "create apps/#{app}/templates/app.html.haml"
129
+ ]
130
+
131
+ run_cmd "hanami generate app #{app}", output
132
+
133
+ #
134
+ # apps/admin/templates/app.html.haml
135
+ #
136
+ expect("apps/admin/templates/app.html.haml").to have_file_content <<~END
137
+ !!!
138
+ %html
139
+ %head
140
+ %title #{app_name}
141
+ = favicon
142
+ %body
143
+ = yield
144
+ END
145
+
146
+ #
147
+ # spec/admin/views/app_layout_spec.rb
148
+ #
149
+ expect("spec/admin/views/app_layout_spec.rb").to have_file_content(%r{Admin::Views::AppLayout})
150
+ end
151
+ end
152
+ end # haml
153
+
154
+ context "slim" do
155
+ it "generates app" do
156
+ with_project("bookshelf_generate_app_slim", template: :slim) do
157
+ app = "admin"
158
+ app_name = Hanami::Utils::String.new(app).classify
159
+ output = [
160
+ "create apps/#{app}/templates/app.html.slim"
161
+ ]
162
+
163
+ run_cmd "hanami generate app #{app}", output
164
+
165
+ #
166
+ # apps/admin/templates/app.html.slim
167
+ #
168
+ expect("apps/admin/templates/app.html.slim").to have_file_content <<~END
169
+ doctype html
170
+ html
171
+ head
172
+ title
173
+ | #{app_name}
174
+ = favicon
175
+ body
176
+ = yield
177
+ END
178
+
179
+ #
180
+ # spec/admin/views/app_layout_spec.rb
181
+ #
182
+ expect("spec/admin/views/app_layout_spec.rb").to have_file_content(%r{Admin::Views::AppLayout})
183
+ end
184
+ end
185
+ end # slim
186
+
187
+ it "prints help message" do
188
+ with_project do
189
+ output = <<~OUT
190
+ Command:
191
+ hanami generate app
192
+
193
+ Usage:
194
+ hanami generate app APP
195
+
196
+ Description:
197
+ Generate an app
198
+
199
+ Arguments:
200
+ APP # REQUIRED The app name (eg. `web`)
201
+
202
+ Options:
203
+ --app-base-url=VALUE # The app base URL (eg. `/api/v1`)
204
+ --help, -h # Print this help
205
+
206
+ Examples:
207
+ hanami generate app admin # Generate `admin` app
208
+ hanami generate app api --app-base-url=/api/v1 # Generate `api` app and mount at `/api/v1`
209
+ OUT
210
+
211
+ run_cmd 'hanami generate app --help', output
212
+ end
213
+ end
214
+ end # app
215
+ end