|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require_relative "testing/node_runner" |
| 4 | +require_relative "plugin" |
| 5 | +require_relative "compiler" |
4 | 6 |
|
5 | 7 | module Funicular |
6 | 8 | module Testing |
7 | 9 | def self.run!(**options) |
8 | 10 | NodeRunner.new(**options).run |
9 | 11 | end |
10 | 12 |
|
| 13 | + # One-call test-environment setup: syncs plugin assets and compiles |
| 14 | + # the client bundle when it is missing or older than any source or |
| 15 | + # plugin file. Development recompiles through the middleware and |
| 16 | + # production through assets:precompile, but the test environment |
| 17 | + # serves whatever app.mrb is lying around; call this once from |
| 18 | + # test_helper and rely on the mtime check to keep it cheap. |
| 19 | + # |
| 20 | + # Funicular::Testing.ensure_compiled! # Rails.root |
| 21 | + # Funicular::Testing.ensure_compiled!(force: true) |
| 22 | + # |
| 23 | + # Returns the path of the compiled bundle. |
| 24 | + def self.ensure_compiled!(root: nil, force: false, debug_mode: true) |
| 25 | + root = Pathname.new(root || default_root) |
| 26 | + registry = Funicular::Plugin::Registry.new(root) |
| 27 | + registry.validate! |
| 28 | + registry.sync_assets |
| 29 | + |
| 30 | + source_dir = root.join("app", "funicular").to_s |
| 31 | + output_file = root.join("app", "assets", "builds", "app.mrb").to_s |
| 32 | + sources = registry.local_source_files + Funicular::Compiler.source_files(source_dir) |
| 33 | + |
| 34 | + if force || stale?(output_file, sources) |
| 35 | + Funicular::Compiler.new( |
| 36 | + source_dir: source_dir, |
| 37 | + output_file: output_file, |
| 38 | + debug_mode: debug_mode, |
| 39 | + prepend_source_files: registry.local_source_files |
| 40 | + ).compile |
| 41 | + end |
| 42 | + output_file |
| 43 | + end |
| 44 | + |
| 45 | + # A bundle is stale when it does not exist or any source file was |
| 46 | + # modified after it was built. |
| 47 | + def self.stale?(output_file, source_files) |
| 48 | + return true unless File.exist?(output_file) |
| 49 | + build_time = File.mtime(output_file) |
| 50 | + source_files.any? { |f| File.exist?(f) && File.mtime(f) > build_time } |
| 51 | + end |
| 52 | + |
| 53 | + def self.default_root |
| 54 | + raise ArgumentError, "root is required outside Rails" unless defined?(Rails) && Rails.respond_to?(:root) |
| 55 | + Rails.root |
| 56 | + end |
| 57 | + |
11 | 58 | def self.assert_picotests(test_case, result, print_summary: true) |
12 | 59 | puts result.picotest_summary if print_summary |
13 | 60 | test_case.assert result.success?, result.output |
|
0 commit comments