spec/support/app_integration.rb in hanami-2.1.0.beta1 vs spec/support/app_integration.rb in hanami-2.1.0.beta2

- old
+ new

@@ -1,9 +1,10 @@ # frozen_string_literal: true require "hanami/devtools/integration/files" require "hanami/devtools/integration/with_tmp_directory" +require "json" require "tmpdir" require "zeitwerk" module RSpec module Support @@ -14,14 +15,56 @@ Pathname(Dir.mktmpdir).tap do |dir| (@made_tmp_dirs ||= []) << dir end end end + + module App + def self.included(base) + super + + base.class_eval do + let!(:node_modules_path) { File.join(Dir.pwd, "node_modules") } + end + end + + private + + def stub_assets(*assets) + manifest_hash = assets.each_with_object({}) { |source_path, hsh| + hsh[source_path] = {url: File.join("/assets", source_path)} + } + + write "public/assets.json", JSON.generate(manifest_hash) + end + + def compile_assets! + link_node_modules + + require "hanami/cli/command" + require "hanami/cli/commands/app/command" + require "hanami/cli/commands/app/assets/compile" + compiler = Hanami::CLI::Commands::App::Assets::Compile.new(config: Hanami.app.config.assets) + + with_directory(Hanami.app.root) do + compiler.call + end + end + + def link_node_modules + root = Hanami.app.root + + return if File.exist?(File.join(root, "node_modules", "hanami-assets", "dist", "hanami-assets.js")) + + FileUtils.ln_s(node_modules_path, root) + rescue Errno::EEXIST # rubocop:disable Lint/SuppressedException + end + end end end -RSpec.shared_context "Application integration" do +RSpec.shared_context "App integration" do let(:app_modules) { %i[TestApp Admin Main Search] } end def autoloaders_teardown! ObjectSpace.each_object(Zeitwerk::Loader) do |loader| @@ -33,10 +76,11 @@ end RSpec.configure do |config| config.include RSpec::Support::Files, :app_integration config.include RSpec::Support::WithTmpDirectory, :app_integration - config.include_context "Application integration", :app_integration + config.include RSpec::Support::App, :app_integration + config.include_context "App integration", :app_integration config.before :each, :app_integration do # Conditionally assign these in case they have been assigned earlier for specific # example groups (e.g. container/prepare_container_spec.rb) @load_paths ||= $LOAD_PATH.dup