forked from shakacode/shakapacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_spec.rb
323 lines (251 loc) · 10.6 KB
/
generator_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
require "pathname"
require "rake"
require "json"
require "shakapacker/utils/misc"
require "shakapacker/utils/version_syntax_converter"
require "package_json"
GEM_ROOT = Pathname.new(File.expand_path("../../..", __FILE__))
SPEC_PATH = Pathname.new(File.expand_path("../", __FILE__))
BASE_RAILS_APP_PATH = SPEC_PATH.join("base-rails-app")
TEMP_RAILS_APP_PATH = SPEC_PATH.join("temp-rails-app")
describe "Generator" do
before :all do
# Don't use --skip-git because we want .gitignore file to exist in the project
sh_in_dir({}, SPEC_PATH, %(
rails new base-rails-app --skip-javascript --skip-bundle --skip-spring
rm -rf base-rails-app/.git
))
Bundler.with_unbundled_env do
sh_in_dir({}, BASE_RAILS_APP_PATH, %(
gem update bundler
bundle add shakapacker --path "#{GEM_ROOT}"
))
end
end
after :all do
Dir.chdir(SPEC_PATH)
FileUtils.rm_rf(BASE_RAILS_APP_PATH)
end
describe "shakapacker:install" do
# TODO: ideally "yarn_berry" should be here too, but it requires more complex setup
NODE_PACKAGE_MANAGERS.reject { |fm| fm == "yarn_berry" }.each do |fallback_manager|
context "when using package_json with #{fallback_manager} as the manager" do
before :all do
sh_opts = { fallback_manager: fallback_manager }
sh_in_dir(sh_opts, SPEC_PATH, "cp -r '#{BASE_RAILS_APP_PATH}' '#{TEMP_RAILS_APP_PATH}'")
Bundler.with_unbundled_env do
sh_in_dir(sh_opts, TEMP_RAILS_APP_PATH, "FORCE=true bundle exec rails shakapacker:install")
end
end
after :all do
Dir.chdir(SPEC_PATH)
FileUtils.rm_rf(TEMP_RAILS_APP_PATH)
end
it "creates `config/shakapacker.yml`" do
config_file_relative_path = "config/shakapacker.yml"
actual_content = read(path_in_the_app(config_file_relative_path))
expected_content = read(path_in_the_gem(config_file_relative_path))
expect(actual_content).to eq expected_content
end
it "replaces package.json with the template file" do
package_json = PackageJson.read(path_in_the_app)
expect(package_json.fetch("name", "")).to eq("app")
end
it "creates webpack config directory and its files" do
expected_files = [
"webpack.config.js"
]
Dir.chdir(path_in_the_app("config/webpack")) do
existing_files_in_config_webpack_dir = Dir.glob("*")
expect(existing_files_in_config_webpack_dir).to eq expected_files
end
end
it "adds binstubs" do
expected_binstubs = []
Dir.chdir(File.join(GEM_ROOT, "lib/install/bin")) do
expected_binstubs = Dir.glob("bin/*")
end
Dir.chdir(File.join(TEMP_RAILS_APP_PATH, "bin")) do
actual_binstubs = Dir.glob("*")
expect(actual_binstubs).to include(*expected_binstubs)
end
end
it "modifies .gitignore" do
actual_content = read(path_in_the_app(".gitignore"))
expect(actual_content).to match ".yarn-integrity"
end
it 'adds <%= javascript_pack_tag "application" %>' do
actual_content = read(path_in_the_app("app/views/layouts/application.html.erb"))
expect(actual_content).to match '<%= javascript_pack_tag "application" %>'
end
it "updates `bin/setup`" do
package_json = PackageJson.read(path_in_the_app)
cmd = package_json.manager.native_install_command.join(" ")
setup_file_content = read(path_in_the_app("bin/setup"))
expect(setup_file_content).to match %r(^\s*system!\(['"]#{cmd}['"]\))
end
it "adds relevant shakapacker version in package.json depending on gem version" do
npm_version = Shakapacker::Utils::VersionSyntaxConverter.new.rubygem_to_npm(Shakapacker::VERSION)
package_json = PackageJson.read(path_in_the_app)
actual_version = package_json.fetch("dependencies", {})["shakapacker"]
expect(actual_version).to eq(npm_version)
end
it "adds Shakapacker peer dependencies to package.json" do
package_json = PackageJson.read(path_in_the_app)
actual_dependencies = package_json.fetch("dependencies", {}).keys
expected_dependencies = %w(
@babel/core
@babel/plugin-transform-runtime
@babel/preset-env
@babel/runtime
babel-loader
compression-webpack-plugin
terser-webpack-plugin
webpack
webpack-assets-manifest
webpack-cli
webpack-merge
)
expect(actual_dependencies).to include(*expected_dependencies)
end
it "adds Shakapacker peer dev dependencies to package.json" do
package_json = PackageJson.read(path_in_the_app)
actual_dev_dependencies = package_json.fetch("devDependencies", {}).keys
expected_dev_dependencies = %w(
webpack-dev-server
)
expect(actual_dev_dependencies).to include(*expected_dev_dependencies)
end
context "with a basic react app setup" do
it "passes the test for rendering react component on the page" do
sh_opts = { fallback_manager: fallback_manager }
Bundler.with_unbundled_env do
sh_in_dir(sh_opts, TEMP_RAILS_APP_PATH, "./bin/rails app:template LOCATION=../e2e_template/template.rb")
expect(sh_in_dir(sh_opts, TEMP_RAILS_APP_PATH, "bundle exec rspec")).to be_truthy
end
end
end
end
end
context "when not using package_json" do
before :all do
sh_opts = { fallback_manager: nil }
sh_in_dir(sh_opts, SPEC_PATH, "cp -r '#{BASE_RAILS_APP_PATH}' '#{TEMP_RAILS_APP_PATH}'")
Bundler.with_unbundled_env do
sh_in_dir(sh_opts, TEMP_RAILS_APP_PATH, "FORCE=true bundle exec rails shakapacker:install")
end
end
after :all do
Dir.chdir(SPEC_PATH)
FileUtils.rm_rf(TEMP_RAILS_APP_PATH)
end
it "creates `config/shakapacker.yml`" do
config_file_relative_path = "config/shakapacker.yml"
actual_content = read(path_in_the_app(config_file_relative_path))
expected_content = read(path_in_the_gem(config_file_relative_path))
expect(actual_content).to eq expected_content
end
it "replaces package.json with template file" do
package_json = PackageJson.read(path_in_the_app)
expect(package_json.fetch("name", "")).to eq("app")
end
it "creates the webpack config directory and its files" do
expected_files = [
"webpack.config.js"
]
Dir.chdir(path_in_the_app("config/webpack")) do
existing_files_in_config_webpack_dir = Dir.glob("*")
expect(existing_files_in_config_webpack_dir).to eq expected_files
end
end
it "adds binstubs" do
expected_binstubs = []
Dir.chdir(File.join(GEM_ROOT, "lib/install/bin")) do
expected_binstubs = Dir.glob("bin/*")
end
Dir.chdir(File.join(TEMP_RAILS_APP_PATH, "bin")) do
actual_binstubs = Dir.glob("*")
expect(actual_binstubs).to include(*expected_binstubs)
end
end
it "modifies .gitignore" do
actual_content = read(path_in_the_app(".gitignore"))
expect(actual_content).to match ".yarn-integrity"
end
it 'adds <%= javascript_pack_tag "application" %>' do
actual_content = read(path_in_the_app("app/views/layouts/application.html.erb"))
expect(actual_content).to match '<%= javascript_pack_tag "application" %>'
end
it "updates `bin/setup`" do
setup_file_content = read(path_in_the_app("bin/setup"))
expect(setup_file_content).to match %r(^\s*system!\(['"]bin/yarn['"]\))
end
it "uses the shakapacker version in package.json depending on gem version" do
npm_version = Shakapacker::Utils::VersionSyntaxConverter.new.rubygem_to_npm(Shakapacker::VERSION)
package_json = PackageJson.read(path_in_the_app)
actual_version = package_json.fetch("dependencies", {})["shakapacker"]
expect(actual_version).to eq(npm_version)
end
it "adds Shakapacker peer dependencies to package.json" do
package_json = PackageJson.read(path_in_the_app)
actual_dependencies = package_json.fetch("dependencies", {}).keys
expected_dependencies = %w(
@babel/core
@babel/plugin-transform-runtime
@babel/preset-env
@babel/runtime
babel-loader
compression-webpack-plugin
terser-webpack-plugin
webpack
webpack-assets-manifest
webpack-cli
webpack-merge
)
expect(actual_dependencies).to include(*expected_dependencies)
end
it "adds Shakapacker peer dev dependencies to package.json" do
package_json = PackageJson.read(path_in_the_app)
actual_dev_dependencies = package_json.fetch("devDependencies", {}).keys
expected_dev_dependencies = %w(
webpack-dev-server
)
expect(actual_dev_dependencies).to include(*expected_dev_dependencies)
end
context "with a basic react app setup" do
it "passes the test for rendering react component on the page" do
sh_opts = { fallback_manager: nil }
Bundler.with_unbundled_env do
sh_in_dir(sh_opts, TEMP_RAILS_APP_PATH, "./bin/rails app:template LOCATION=../e2e_template/template.rb")
expect(sh_in_dir(sh_opts, TEMP_RAILS_APP_PATH, "bundle exec rspec")).to be_truthy
end
end
end
end
end
private
def path_in_the_app(relative_path = nil)
Pathname.new(File.join([TEMP_RAILS_APP_PATH, relative_path].compact))
end
def path_in_the_gem(relative_path = nil)
Pathname.new(File.join([GEM_ROOT, "lib/install" , relative_path].compact))
end
def read(path)
File.read(path)
end
def sort_out_package_json(opts)
ENV["PATH"] = "#{SPEC_PATH}/fake-bin:#{ENV["PATH"]}"
if opts[:fallback_manager].nil?
ENV["SHAKAPACKER_EXPECTED_PACKAGE_MANGER"] = "yarn_classic"
ENV["SHAKAPACKER_USE_PACKAGE_JSON_GEM"] = "false"
else
ENV["SHAKAPACKER_EXPECTED_PACKAGE_MANGER"] = opts[:fallback_manager]
ENV["SHAKAPACKER_USE_PACKAGE_JSON_GEM"] = "true"
ENV["PACKAGE_JSON_FALLBACK_MANAGER"] = opts[:fallback_manager]
end
end
def sh_in_dir(opts, dir, *shell_commands)
sort_out_package_json(opts)
Shakapacker::Utils::Misc.sh_in_dir(dir, *shell_commands)
end
end