From 32a7749617896c6aa785c331b13c772e4d37c3f2 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 14:42:39 +0100 Subject: [PATCH] Basic tests for a mounted application This sets up a barebones mounted Rails engine so that tests can be performed against it. This follows a similar approach to the other rake tasks test and thus doesn't intend to be too exhaustive in what is testing and is mostly a cursory test that one of the tasks does perform differently in a mounted Rails engine context. --- .gitignore | 1 + test/engine_rake_tasks_test.rb | 39 ++++++++++ test/mounted_app/Rakefile | 4 + test/mounted_app/test/dummy/Rakefile | 3 + test/mounted_app/test/dummy/bin/rails | 3 + test/mounted_app/test/dummy/bin/rake | 3 + test/mounted_app/test/dummy/config.ru | 5 ++ .../test/dummy/config/application.rb | 10 +++ .../test/dummy/config/environment.rb | 3 + .../test/dummy/config/webpacker.yml | 75 +++++++++++++++++++ test/mounted_app/test/dummy/package.json | 7 ++ 11 files changed, 153 insertions(+) create mode 100644 test/engine_rake_tasks_test.rb create mode 100644 test/mounted_app/Rakefile create mode 100644 test/mounted_app/test/dummy/Rakefile create mode 100755 test/mounted_app/test/dummy/bin/rails create mode 100755 test/mounted_app/test/dummy/bin/rake create mode 100644 test/mounted_app/test/dummy/config.ru create mode 100644 test/mounted_app/test/dummy/config/application.rb create mode 100644 test/mounted_app/test/dummy/config/environment.rb create mode 100644 test/mounted_app/test/dummy/config/webpacker.yml create mode 100644 test/mounted_app/test/dummy/package.json diff --git a/.gitignore b/.gitignore index 62fbfaee1..07598ddfd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /.bundle /pkg +/test/mounted_app/test/dummy/log /test/test_app/log node_modules .byebug_history diff --git a/test/engine_rake_tasks_test.rb b/test/engine_rake_tasks_test.rb new file mode 100644 index 000000000..a1a288a52 --- /dev/null +++ b/test/engine_rake_tasks_test.rb @@ -0,0 +1,39 @@ +require "test_helper" + +class EngineRakeTasksTest < Minitest::Test + def setup + remove_webpack_binstubs + end + + def teardown + remove_webpack_binstubs + end + + def test_task_mounted + output = Dir.chdir(mounted_app_path) { `rake -T` } + assert_includes output, "app:webpacker" + end + + def test_binstubs + Dir.chdir(mounted_app_path) { `bundle exec rake app:webpacker:binstubs` } + webpack_binstub_paths.each { |path| assert File.exist?(path) } + end + + private + def mounted_app_path + File.expand_path("mounted_app", __dir__) + end + + def webpack_binstub_paths + [ + "#{mounted_app_path}/test/dummy/bin/webpack", + "#{mounted_app_path}/test/dummy/bin/webpack-dev-server", + ] + end + + def remove_webpack_binstubs + webpack_binstub_paths.each do |path| + File.delete(path) if File.exist?(path) + end + end +end diff --git a/test/mounted_app/Rakefile b/test/mounted_app/Rakefile new file mode 100644 index 000000000..fe4151add --- /dev/null +++ b/test/mounted_app/Rakefile @@ -0,0 +1,4 @@ +require "bundler/setup" + +APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__) +load "rails/tasks/engine.rake" diff --git a/test/mounted_app/test/dummy/Rakefile b/test/mounted_app/test/dummy/Rakefile new file mode 100644 index 000000000..d1baef069 --- /dev/null +++ b/test/mounted_app/test/dummy/Rakefile @@ -0,0 +1,3 @@ +require_relative "config/application" + +Rails.application.load_tasks diff --git a/test/mounted_app/test/dummy/bin/rails b/test/mounted_app/test/dummy/bin/rails new file mode 100755 index 000000000..fc42e555b --- /dev/null +++ b/test/mounted_app/test/dummy/bin/rails @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path("../config/application", __dir__) +require "rails/commands" diff --git a/test/mounted_app/test/dummy/bin/rake b/test/mounted_app/test/dummy/bin/rake new file mode 100755 index 000000000..580915dc8 --- /dev/null +++ b/test/mounted_app/test/dummy/bin/rake @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +require "rake" +Rake.application.run diff --git a/test/mounted_app/test/dummy/config.ru b/test/mounted_app/test/dummy/config.ru new file mode 100644 index 000000000..8987d1c00 --- /dev/null +++ b/test/mounted_app/test/dummy/config.ru @@ -0,0 +1,5 @@ +# This file allows the `Rails.root` to be correctly determined. + +require_relative "config/environment" + +run Rails.application diff --git a/test/mounted_app/test/dummy/config/application.rb b/test/mounted_app/test/dummy/config/application.rb new file mode 100644 index 000000000..24f516874 --- /dev/null +++ b/test/mounted_app/test/dummy/config/application.rb @@ -0,0 +1,10 @@ +require "action_controller/railtie" +require "action_view/railtie" +require "webpacker" + +module TestDummyApp + class Application < Rails::Application + config.secret_key_base = "abcdef" + config.eager_load = true + end +end diff --git a/test/mounted_app/test/dummy/config/environment.rb b/test/mounted_app/test/dummy/config/environment.rb new file mode 100644 index 000000000..73a3979b0 --- /dev/null +++ b/test/mounted_app/test/dummy/config/environment.rb @@ -0,0 +1,3 @@ +require_relative "application" + +Rails.application.initialize! diff --git a/test/mounted_app/test/dummy/config/webpacker.yml b/test/mounted_app/test/dummy/config/webpacker.yml new file mode 100644 index 000000000..cd7cd329e --- /dev/null +++ b/test/mounted_app/test/dummy/config/webpacker.yml @@ -0,0 +1,75 @@ +# Note: You must restart bin/webpack-dev-server for changes to take effect + +default: &default + source_path: app/javascript + source_entry_path: packs + public_output_path: packs + cache_path: tmp/cache/webpacker + + # Additional paths webpack should lookup modules + # ['app/assets', 'engine/foo/app/assets'] + resolved_paths: + - app/assets + - /etc/yarn + + # Reload manifest.json on all requests so we reload latest compiled packs + cache_manifest: false + + extensions: + - .js + - .sass + - .scss + - .css + - .module.sass + - .module.scss + - .module.css + - .png + - .svg + - .gif + - .jpeg + - .jpg + +development: + <<: *default + compile: true + + # Reference: https://webpack.js.org/configuration/dev-server/ + dev_server: + https: false + host: localhost + port: 3035 + public: localhost:3035 + hmr: false + # Inline should be set to true if using HMR + inline: true + overlay: true + disable_host_check: true + use_local_ip: false + +test: + <<: *default + compile: true + + # Compile test packs to a separate directory + public_output_path: packs-test + +production: + <<: *default + + # Production depends on precompilation of packs prior to booting for performance. + compile: false + + # Cache manifest.json for performance + cache_manifest: true + +staging: + <<: *default + + # Production depends on precompilation of packs prior to booting for performance. + compile: false + + # Cache manifest.json for performance + cache_manifest: true + + # Compile staging packs to a separate directory + public_output_path: packs-staging diff --git a/test/mounted_app/test/dummy/package.json b/test/mounted_app/test/dummy/package.json new file mode 100644 index 000000000..d32a2dc50 --- /dev/null +++ b/test/mounted_app/test/dummy/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "dependencies": { + "@rails/webpacker": "file:../../../../" + }, + "license": "MIT" +}