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,189 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe "hanami generate", type: :integration do
4
+ describe "mailer" do
5
+ context "generates a new mailer" do
6
+ let(:output) do
7
+ ["create lib/bookshelf_generate_mailer/mailers/welcome.rb",
8
+ "create spec/bookshelf_generate_mailer/mailers/welcome_spec.rb",
9
+ "create lib/bookshelf_generate_mailer/mailers/templates/welcome.txt.erb",
10
+ "create lib/bookshelf_generate_mailer/mailers/templates/welcome.html.erb"]
11
+ end
12
+
13
+ it 'generate the mailer files' do
14
+ with_project('bookshelf_generate_mailer', test: 'rspec') do
15
+ run_cmd "hanami generate mailer welcome", output
16
+ #
17
+ # lib/bookshelf_generate_mailer/mailers/welcome.rb
18
+ #
19
+ expect("lib/bookshelf_generate_mailer/mailers/welcome.rb").to have_file_content <<~END
20
+ module Mailers
21
+ class Welcome
22
+ include Hanami::Mailer
23
+
24
+ from '<from>'
25
+ to '<to>'
26
+ subject 'Hello'
27
+ end
28
+ end
29
+ END
30
+
31
+ expect("lib/bookshelf_generate_mailer/mailers/templates/welcome.txt.erb").to have_file_content ""
32
+ expect("lib/bookshelf_generate_mailer/mailers/templates/welcome.html.erb").to have_file_content ""
33
+ end
34
+ end
35
+
36
+ it 'generates a proper minitest file' do
37
+ with_project('bookshelf_generate_mailer', test: 'minitest') do
38
+ run_cmd "hanami generate mailer welcome", output
39
+ #
40
+ # spec/bookshelf_generate_mailer/mailers/welcome_spec.rb
41
+ #
42
+ expect("spec/bookshelf_generate_mailer/mailers/welcome_spec.rb").to have_file_content <<~END
43
+ require_relative '../../spec_helper'
44
+
45
+ describe Mailers::Welcome do
46
+ it 'delivers email' do
47
+ mail = Mailers::Welcome.deliver
48
+ end
49
+ end
50
+ END
51
+ end
52
+ end
53
+
54
+ it 'generates a proper RSpec file' do
55
+ with_project('bookshelf_generate_mailer', test: 'rspec') do
56
+ run_cmd "hanami generate mailer welcome", output
57
+ #
58
+ # spec/bookshelf_generate_mailer/mailers/welcome_spec.rb
59
+ #
60
+ expect("spec/bookshelf_generate_mailer/mailers/welcome_spec.rb").to have_file_content <<~END
61
+ RSpec.describe Mailers::Welcome, type: :mailer do
62
+ it 'delivers email' do
63
+ mail = Mailers::Welcome.deliver
64
+ end
65
+ end
66
+ END
67
+ end
68
+ end
69
+ end
70
+
71
+ it "generates mailer with options from, to and subject with single quotes" do
72
+ with_project("bookshelf_generate_mailer_with_options") do
73
+ output = [
74
+ "create spec/bookshelf_generate_mailer_with_options/mailers/welcome_spec.rb",
75
+ "create lib/bookshelf_generate_mailer_with_options/mailers/welcome.rb",
76
+ "create lib/bookshelf_generate_mailer_with_options/mailers/templates/welcome.txt.erb",
77
+ "create lib/bookshelf_generate_mailer_with_options/mailers/templates/welcome.html.erb"
78
+ ]
79
+
80
+ run_cmd "hanami generate mailer welcome --from=\"'mail@example.com'\" --to=\"'user@example.com'\" --subject=\"'Let\'s start'\"", output
81
+
82
+ expect("lib/bookshelf_generate_mailer_with_options/mailers/welcome.rb").to have_file_content <<~END
83
+ module Mailers
84
+ class Welcome
85
+ include Hanami::Mailer
86
+
87
+ from 'mail@example.com'
88
+ to 'user@example.com'
89
+ subject 'Let\'s start'
90
+ end
91
+ end
92
+ END
93
+ end
94
+ end
95
+
96
+ it "generates mailer with options from, to and subject with double quotes" do
97
+ with_project("bookshelf_generate_mailer_with_options") do
98
+ output = [
99
+ "create spec/bookshelf_generate_mailer_with_options/mailers/welcome_spec.rb",
100
+ "create lib/bookshelf_generate_mailer_with_options/mailers/welcome.rb",
101
+ "create lib/bookshelf_generate_mailer_with_options/mailers/templates/welcome.txt.erb",
102
+ "create lib/bookshelf_generate_mailer_with_options/mailers/templates/welcome.html.erb"
103
+ ]
104
+
105
+ run_cmd "hanami generate mailer welcome --from='\"mail@example.com\"' --to='\"user@example.com\"' --subject='\"Come on \"Folks\"\"'", output
106
+
107
+ expect("lib/bookshelf_generate_mailer_with_options/mailers/welcome.rb").to have_file_content <<~END
108
+ module Mailers
109
+ class Welcome
110
+ include Hanami::Mailer
111
+
112
+ from 'mail@example.com'
113
+ to 'user@example.com'
114
+ subject 'Come on "Folks"'
115
+ end
116
+ end
117
+ END
118
+ end
119
+ end
120
+
121
+ it "generates mailer with options from, to and subject without quotes" do
122
+ with_project("bookshelf_generate_mailer_with_options") do
123
+ output = [
124
+ "create spec/bookshelf_generate_mailer_with_options/mailers/welcome_spec.rb",
125
+ "create lib/bookshelf_generate_mailer_with_options/mailers/welcome.rb",
126
+ "create lib/bookshelf_generate_mailer_with_options/mailers/templates/welcome.txt.erb",
127
+ "create lib/bookshelf_generate_mailer_with_options/mailers/templates/welcome.html.erb"
128
+ ]
129
+
130
+ run_cmd "hanami generate mailer welcome --from=mail@example.com --to=user@example.com --subject=Welcome", output
131
+
132
+ expect("lib/bookshelf_generate_mailer_with_options/mailers/welcome.rb").to have_file_content <<~END
133
+ module Mailers
134
+ class Welcome
135
+ include Hanami::Mailer
136
+
137
+ from 'mail@example.com'
138
+ to 'user@example.com'
139
+ subject 'Welcome'
140
+ end
141
+ end
142
+ END
143
+ end
144
+ end
145
+
146
+ it "fails with missing arguments" do
147
+ with_project("bookshelf_generate_mailer_without_args") do
148
+ output = <<~OUT
149
+ ERROR: "hanami generate mailer" was called with no arguments
150
+ Usage: "hanami generate mailer MAILER"
151
+ OUT
152
+
153
+ run_cmd "hanami generate mailer", output, exit_status: 1
154
+ end
155
+ end
156
+
157
+ it "prints help message" do
158
+ with_project do
159
+ output = <<~OUT
160
+ Command:
161
+ hanami generate mailer
162
+
163
+ Usage:
164
+ hanami generate mailer MAILER
165
+
166
+ Description:
167
+ Generate a mailer
168
+
169
+ Arguments:
170
+ MAILER # REQUIRED The mailer name (eg. `welcome`)
171
+
172
+ Options:
173
+ --from=VALUE # The default `from` field of the mail
174
+ --to=VALUE # The default `to` field of the mail
175
+ --subject=VALUE # The mail subject
176
+ --help, -h # Print this help
177
+
178
+ Examples:
179
+ hanami generate mailer welcome # Basic usage
180
+ hanami generate mailer welcome --from="noreply@example.com" # Generate with default `from` value
181
+ hanami generate mailer announcement --to="users@example.com" # Generate with default `to` value
182
+ hanami generate mailer forgot_password --subject="Your password reset" # Generate with default `subject`
183
+ OUT
184
+
185
+ run_cmd 'hanami generate mailer --help', output
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe "hanami generate", type: :integration do
4
+ describe "migration" do
5
+ context "with migration name" do
6
+ it_behaves_like "a new migration" do
7
+ let(:input) { "users" }
8
+ end
9
+ end
10
+
11
+ context "with underscored name" do
12
+ it_behaves_like "a new migration" do
13
+ let(:input) { "create_users" }
14
+ end
15
+ end
16
+
17
+ context "with dashed name" do
18
+ it_behaves_like "a new migration" do
19
+ let(:input) { "add-verified-at-to-users" }
20
+ end
21
+ end
22
+
23
+ context "with camel case app name" do
24
+ it_behaves_like "a new migration" do
25
+ let(:input) { "AddUniqueIndexUsersEmail" }
26
+ end
27
+ end
28
+
29
+ context "with missing argument" do
30
+ it "fails" do
31
+ with_project('bookshelf_generate_migration_missing_arguments') do
32
+ output = <<-END
33
+ ERROR: "hanami generate migration" was called with no arguments
34
+ Usage: "hanami generate migration MIGRATION"
35
+ END
36
+
37
+ run_cmd "hanami generate migration", output, exit_status: 1
38
+ end
39
+ end
40
+ end
41
+
42
+ it "prints help message" do
43
+ with_project do
44
+ banner = <<~OUT
45
+ Command:
46
+ hanami generate migration
47
+
48
+ Usage:
49
+ hanami generate migration MIGRATION
50
+
51
+ Description:
52
+ Generate a migration
53
+
54
+ Arguments:
55
+ MIGRATION # REQUIRED The migration name (eg. `create_users`)
56
+
57
+ Options:
58
+ --help, -h # Print this help
59
+
60
+ Examples:
61
+ OUT
62
+
63
+ output = [
64
+ banner,
65
+ %r{ hanami generate migration create_users # Generate `db/migrations/[\d]{14}_create_users.rb`},
66
+ ]
67
+
68
+ run_cmd 'hanami generate migration --help', output
69
+ end
70
+ end
71
+ end # migration
72
+ end
@@ -0,0 +1,290 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/utils/string"
4
+
5
+ RSpec.describe "hanami generate", type: :integration do
6
+ describe "model" do
7
+ context "with model name" do
8
+ it_behaves_like "a new model" do
9
+ let(:input) { "user" }
10
+ end
11
+ end
12
+
13
+ context "with underscored name" do
14
+ it_behaves_like "a new model" do
15
+ let(:input) { "discounted_book" }
16
+ end
17
+ end
18
+
19
+ context "with dashed name" do
20
+ it_behaves_like "a new model" do
21
+ let(:input) { "user-event" }
22
+ end
23
+ end
24
+
25
+ context "with camel case name" do
26
+ it_behaves_like "a new model" do
27
+ let(:input) { "VerifiedUser" }
28
+ end
29
+ end
30
+
31
+ context "with missing argument" do
32
+ it "fails" do
33
+ with_project('bookshelf_generate_model_missing_arguments') do
34
+ output = <<-END
35
+ ERROR: "hanami generate model" was called with no arguments
36
+ Usage: "hanami generate model MODEL"
37
+ END
38
+
39
+ run_cmd "hanami generate model", output, exit_status: 1
40
+ end
41
+ end
42
+ end
43
+
44
+ context "with missing migrations directory" do
45
+ it "will create directory and migration" do
46
+ with_project do
47
+ model_name = "book"
48
+ directory = Pathname.new("db").join("migrations")
49
+ FileUtils.rm_rf(directory)
50
+
51
+ run_cmd "hanami generate model #{model_name}"
52
+ expect(directory).to be_directory
53
+
54
+ migration = directory.children.find do |m|
55
+ m.to_s.include?(model_name)
56
+ end
57
+
58
+ expect(migration).to_not be(nil)
59
+ end
60
+ end
61
+ end
62
+
63
+ context "with skip-migration" do
64
+ it "doesn't create a migration file" do
65
+ model_name = "user"
66
+ table_name = "users"
67
+ project = "bookshelf_generate_model_skip_migration"
68
+ with_project(project) do
69
+ run_cmd "hanami generate model #{model_name} --skip-migration"
70
+ #
71
+ # db/migrations/<timestamp>_create_<models>.rb
72
+ #
73
+ migrations = Pathname.new("db").join("migrations").children
74
+ file = migrations.find do |child|
75
+ child.to_s.include?("create_#{table_name}")
76
+ end
77
+
78
+ expect(file).to be_nil, "Expected to not find a migration matching: create_#{table_name}. Found #{file&.to_s}"
79
+ end
80
+ end
81
+
82
+ it "doesn't create a migration file when --relation is used" do
83
+ model_name = "user"
84
+ table_name = "accounts"
85
+ project = "bookshelf_generate_model_skip_migration"
86
+ with_project(project) do
87
+ run_cmd "hanami generate model #{model_name} --skip-migration --relation=#{table_name}"
88
+ #
89
+ # db/migrations/<timestamp>_create_<models>.rb
90
+ #
91
+ migrations = Pathname.new("db").join("migrations").children
92
+ file = migrations.find do |child|
93
+ child.to_s.include?("create_#{table_name}")
94
+ end
95
+
96
+ expect(file).to be_nil, "Expected to not find a migration matching: create_#{table_name}. Found #{file&.to_s}"
97
+ end
98
+ end
99
+ end
100
+
101
+ context "with relation option" do
102
+ let(:project) { "generate_model_with_relation_name" }
103
+ let(:model_name) { "stimulus" }
104
+ let(:class_name) { "Stimulus" }
105
+ let(:relation_name) { "stimuli" }
106
+
107
+ it "creates correct entity, repository, and migration" do
108
+ with_project(project) do
109
+ output = [
110
+ "create lib/#{project}/entities/#{model_name}.rb",
111
+ "create lib/#{project}/repositories/#{model_name}_repository.rb",
112
+ /create db\/migrations\/(\d+)_create_#{relation_name}.rb/
113
+ ]
114
+
115
+ run_cmd "hanami generate model #{model_name} --relation=#{relation_name}", output
116
+
117
+ expect("lib/#{project}/repositories/#{model_name}_repository.rb").to have_file_content <<~END
118
+ class #{class_name}Repository < Hanami::Repository
119
+ self.relation = :#{relation_name}
120
+ end
121
+ END
122
+
123
+ migration = Pathname.new("db").join("migrations").children.find do |child|
124
+ child.to_s.include?("create_#{relation_name}")
125
+ end
126
+
127
+ expect(migration.to_s).to have_file_content <<~END
128
+ Hanami::Model.migration do
129
+ change do
130
+ create_table :#{relation_name} do
131
+ primary_key :id
132
+
133
+ column :created_at, DateTime, null: false
134
+ column :updated_at, DateTime, null: false
135
+ end
136
+ end
137
+ end
138
+ END
139
+ end
140
+ end
141
+
142
+ it "handles CamelCase arguments" do
143
+ with_project(project) do
144
+ model = "sheep"
145
+ relation_name = "black_sheeps"
146
+ output = [
147
+ "create lib/#{project}/entities/#{model}.rb",
148
+ "create lib/#{project}/repositories/#{model}_repository.rb",
149
+ /create db\/migrations\/(\d+)_create_#{relation_name}.rb/
150
+ ]
151
+
152
+ run_cmd "hanami generate model #{model} --relation=BlackSheeps", output
153
+
154
+ expect("lib/#{project}/repositories/sheep_repository.rb").to have_file_content <<~END
155
+ class SheepRepository < Hanami::Repository
156
+ self.relation = :#{relation_name}
157
+ end
158
+ END
159
+
160
+ migration = Pathname.new("db").join("migrations").children.find do |child|
161
+ child.to_s.include?("create_#{relation_name}")
162
+ end
163
+
164
+ expect(migration.to_s).to have_file_content <<~END
165
+ Hanami::Model.migration do
166
+ change do
167
+ create_table :#{relation_name} do
168
+ primary_key :id
169
+
170
+ column :created_at, DateTime, null: false
171
+ column :updated_at, DateTime, null: false
172
+ end
173
+ end
174
+ end
175
+ END
176
+ end
177
+ end
178
+
179
+ it "returns error for blank option" do
180
+ with_project(project) do
181
+ run_cmd "hanami generate model #{model_name} --relation=", "`' is not a valid relation name", exit_status: 1
182
+ end
183
+ end
184
+ end
185
+
186
+ context "minitest" do
187
+ it "generates model" do
188
+ project = "bookshelf_generate_model_minitest"
189
+
190
+ with_project(project, test: :minitest) do
191
+ model = "book"
192
+ class_name = Hanami::Utils::String.new(model).classify
193
+ output = [
194
+ "create spec/#{project}/entities/#{model}_spec.rb",
195
+ "create spec/#{project}/repositories/#{model}_repository_spec.rb"
196
+ ]
197
+
198
+ run_cmd "hanami generate model #{model}", output
199
+
200
+ #
201
+ # spec/<project>/entities/<model>_spec.rb
202
+ #
203
+ expect("spec/#{project}/entities/#{model}_spec.rb").to have_file_content <<~END
204
+ require_relative '../../spec_helper'
205
+
206
+ describe #{class_name} do
207
+ # place your tests here
208
+ end
209
+ END
210
+
211
+ #
212
+ # spec/<project>/repositories/<model>_repository_spec.rb
213
+ #
214
+ expect("spec/#{project}/repositories/#{model}_repository_spec.rb").to have_file_content <<~END
215
+ require_relative '../../spec_helper'
216
+
217
+ describe #{class_name}Repository do
218
+ # place your tests here
219
+ end
220
+ END
221
+ end
222
+ end
223
+ end # minitest
224
+
225
+ context "rspec" do
226
+ it "generates model" do
227
+ project = "bookshelf_generate_model_rspec"
228
+
229
+ with_project(project, test: :rspec) do
230
+ model = "book"
231
+ class_name = Hanami::Utils::String.new(model).classify
232
+ output = [
233
+ "create spec/#{project}/entities/#{model}_spec.rb",
234
+ "create spec/#{project}/repositories/#{model}_repository_spec.rb"
235
+ ]
236
+
237
+ run_cmd "hanami generate model #{model}", output
238
+
239
+ #
240
+ # spec/<project>/entities/<model>_spec.rb
241
+ #
242
+ expect("spec/#{project}/entities/#{model}_spec.rb").to have_file_content <<~END
243
+ RSpec.describe #{class_name}, type: :entity do
244
+ # place your tests here
245
+ end
246
+ END
247
+
248
+ #
249
+ # spec/<project>/repositories/<model>_repository_spec.rb
250
+ #
251
+ expect("spec/#{project}/repositories/#{model}_repository_spec.rb").to have_file_content <<~END
252
+ RSpec.describe BookRepository, type: :repository do
253
+ # place your tests here
254
+ end
255
+ END
256
+ end
257
+ end
258
+ end # rspec
259
+
260
+ it "prints help message" do
261
+ with_project do
262
+ output = <<~OUT
263
+ Command:
264
+ hanami generate model
265
+
266
+ Usage:
267
+ hanami generate model MODEL
268
+
269
+ Description:
270
+ Generate a model
271
+
272
+ Arguments:
273
+ MODEL # REQUIRED Model name (eg. `user`)
274
+
275
+ Options:
276
+ --[no-]skip-migration # Skip migration, default: false
277
+ --relation=VALUE # Name of the database relation, default: pluralized model name
278
+ --help, -h # Print this help
279
+
280
+ Examples:
281
+ hanami generate model user # Generate `User` entity, `UserRepository` repository, and the migration
282
+ hanami generate model user --skip-migration # Generate `User` entity and `UserRepository` repository
283
+ hanami generate model user --relation=accounts # Generate `User` entity, `UserRepository` and migration to create `accounts` table
284
+ OUT
285
+
286
+ run_cmd 'hanami generate model --help', output
287
+ end
288
+ end
289
+ end # model
290
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe "hanami generate", type: :integration do
4
+ describe "secret" do
5
+ context "without app name" do
6
+ it "prints secret" do
7
+ with_project do
8
+ generate "secret"
9
+
10
+ expect(out).to match(/[\w]{64}/)
11
+ end
12
+ end
13
+ end
14
+
15
+ context "with app name" do
16
+ it "prints secret" do
17
+ with_project do
18
+ generate "secret web"
19
+
20
+ expect(out).to match(%r{WEB_SESSIONS_SECRET="[\w]{64}"})
21
+ end
22
+ end
23
+ end
24
+
25
+ it 'prints help message' do
26
+ with_project do
27
+ banner = <<~OUT
28
+ Command:
29
+ hanami generate secret
30
+
31
+ Usage:
32
+ hanami generate secret [APP]
33
+
34
+ Description:
35
+ Generate session secret
36
+
37
+ Arguments:
38
+ APP # The app name (eg. `web`)
39
+
40
+ Options:
41
+ --help, -h # Print this help
42
+
43
+ Examples:
44
+ OUT
45
+
46
+ output = [
47
+ banner,
48
+ # %r{ hanami generate secret # Prints secret (eg. `[\w]{64}`)},
49
+ # %r{ hanami generate secret web # Prints session secret (eg. `WEB_SESSIONS_SECRET=[\w]{64}`)}
50
+ ]
51
+
52
+ run_cmd 'hanami generate secret --help', output
53
+ end
54
+ end
55
+ end # secret
56
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe "hanami generate", type: :integration do
4
+ it "prints subcommands" do
5
+ with_project do
6
+ output = <<~OUT
7
+ Commands:
8
+ hanami generate action APP ACTION # Generate an action for app
9
+ hanami generate app APP # Generate an app
10
+ hanami generate mailer MAILER # Generate a mailer
11
+ hanami generate migration MIGRATION # Generate a migration
12
+ hanami generate model MODEL # Generate a model
13
+ hanami generate secret [APP] # Generate session secret
14
+ OUT
15
+
16
+ run_cmd "hanami generate", output, exit_status: 1
17
+ end
18
+ end
19
+ end