Skip to content

Commit

Permalink
Install JavaScript packages before run test
Browse files Browse the repository at this point in the history
Some tests are running yarn install during the test.
The directory used for isolation test is not subject to yarn workspace,
and it occurs because the required package is not installed.
In order to avoid this, I fixed all necessary packages to be installed
before run test and use symlink to `node_modules`.

This is a bit complicated, as `yarn install` needs to be run in a specific
directory before running the test.
However, running `yarn install` every time run the test is expensive
when testing locally and should be avoided.
  • Loading branch information
y-yagi committed Feb 11, 2019
1 parent 9cc463e commit cd34f00
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ before_script:
# Decodes to e.g. `export VARIABLE=VALUE`
- $(base64 --decode <<< "ZXhwb3J0IFNBVUNFX0FDQ0VTU19LRVk9YTAzNTM0M2YtZTkyMi00MGIzLWFhM2MtMDZiM2VhNjM1YzQ4")
- $(base64 --decode <<< "ZXhwb3J0IFNBVUNFX1VTRVJOQU1FPXJ1YnlvbnJhaWxz")
- "[[ $GEM != 'railties' ]] || (cd actionview && yarn build)"
- "[[ $GEM != 'railties' ]] || (cd railties/test/isolation/assets && yarn install)"

script: 'ci/travis.rb'

Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
"actioncable",
"actiontext",
"activestorage",
"actionview",
"tmp/templates/app_template",
"railties/test/fixtures/tmp/bukkits/**/test/dummy",
"railties/test/fixtures/tmp/bukkits/**/spec/dummy"
"actionview"
],
"dependencies": {
"webpack": "^4.17.1"
Expand Down
1 change: 1 addition & 0 deletions railties/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/test/500.html
/test/fixtures/tmp/
/test/initializer/root/log/
/test/isolation/assets/yarn.lock
/tmp/
2 changes: 1 addition & 1 deletion railties/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace :test do

test_patterns = dirs.map { |dir| "test/#{dir}/*_test.rb" }
test_files = Dir[*test_patterns].select do |file|
!file.start_with?("test/fixtures/")
!file.start_with?("test/fixtures/") && !file.start_with?("test/isolation/assets/")
end.sort

if ENV["BUILDKITE_PARALLEL_JOB_COUNT"]
Expand Down
18 changes: 7 additions & 11 deletions railties/test/isolation/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -472,21 +472,17 @@ class ActiveSupport::TestCase
FileUtils.rm_rf(app_template_path)
FileUtils.mkdir_p(app_template_path)

Dir.chdir "#{RAILS_FRAMEWORK_ROOT}/actionview" do
`yarn build`
end

`#{Gem.ruby} #{RAILS_FRAMEWORK_ROOT}/railties/exe/rails new #{app_template_path} --skip-bundle --skip-listen --no-rc`
`#{Gem.ruby} #{RAILS_FRAMEWORK_ROOT}/railties/exe/rails new #{app_template_path} --skip-bundle --skip-listen --no-rc --skip-webpack-install`
File.open("#{app_template_path}/config/boot.rb", "w") do |f|
f.puts "require 'rails/all'"
end

Dir.chdir(app_template_path) { `yarn add https://github.com/rails/webpacker.git` } # Use the latest version.

# Manually install `webpack` as bin symlinks are not created for sub dependencies
# in workspaces. See https://github.com/yarnpkg/yarn/issues/4964
Dir.chdir(app_template_path) { `yarn add webpack@4.17.1 --tilde` }
Dir.chdir(app_template_path) { `yarn add webpack-cli` }
assets_path = "#{RAILS_FRAMEWORK_ROOT}/railties/test/isolation/assets"
FileUtils.cp("#{assets_path}/package.json", "#{app_template_path}/package.json")
FileUtils.cp("#{assets_path}/config/webpacker.yml", "#{app_template_path}/config/webpacker.yml")
FileUtils.cp_r("#{assets_path}/config/webpack", "#{app_template_path}/config/webpack")
FileUtils.ln_s("#{assets_path}/node_modules", "#{app_template_path}/node_modules")
FileUtils.chdir(app_template_path) { `bin/rails webpacker:binstubs` }

# Fake 'Bundler.require' -- we run using the repo's Gemfile, not an
# app-specific one: we don't want to require every gem that lists.
Expand Down
3 changes: 3 additions & 0 deletions railties/test/isolation/assets/config/webpack/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const { environment } = require('@rails/webpacker')
module.exports = environment.toWebpackConfig()
3 changes: 3 additions & 0 deletions railties/test/isolation/assets/config/webpack/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const { environment } = require('@rails/webpacker')
module.exports = environment.toWebpackConfig()
3 changes: 3 additions & 0 deletions railties/test/isolation/assets/config/webpack/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const { environment } = require('@rails/webpacker')
module.exports = environment.toWebpackConfig()
8 changes: 8 additions & 0 deletions railties/test/isolation/assets/config/webpacker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
default: &default
check_yarn_integrity: false
development:
<<: *default
test:
<<: *default
production:
<<: *default
9 changes: 9 additions & 0 deletions railties/test/isolation/assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "dummy",
"private": true,
"dependencies": {
"@rails/ujs": "file:../../../../actionview",
"@rails/webpacker": "https://github.com/rails/webpacker.git",
"turbolinks": "^5.2.0"
}
}

0 comments on commit cd34f00

Please sign in to comment.