|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require 'English' |
| 4 | +require 'open3' |
| 5 | +require 'tmpdir' |
4 | 6 | module GeneratorTestHelpers |
5 | 7 | def self.cleanup_test_app(app_path) |
6 | 8 | FileUtils.rm_rf(app_path) |
7 | 9 | end |
8 | 10 |
|
9 | 11 | def self.create_test_app(name, template:, template_path:) |
10 | 12 | template_file = File.join(template_path, template) |
| 13 | + ruby_llm_path = File.expand_path('../..', __dir__) |
| 14 | + root_bundle_gemfile = ENV['BUNDLE_GEMFILE'] || Bundler.default_gemfile.to_s |
11 | 15 |
|
12 | | - Bundler.with_unbundled_env do |
13 | | - Dir.chdir(Dir.tmpdir) do |
14 | | - ENV['RUBYLLM_PATH'] = File.expand_path('../..', __dir__) |
15 | | - `rails new #{name} --skip-bootsnap -m #{template_file} 2>&1` |
16 | | - success = $CHILD_STATUS.success? |
17 | | - raise "Failed to create test app #{name}" unless success |
18 | | - end |
19 | | - end |
| 16 | + root_env = { |
| 17 | + 'RUBYLLM_PATH' => ruby_llm_path, |
| 18 | + 'BUNDLE_GEMFILE' => root_bundle_gemfile |
| 19 | + } |
| 20 | + root_env['BUNDLE_PATH'] = ENV['BUNDLE_PATH'] if ENV.key?('BUNDLE_PATH') |
| 21 | + root_env['BUNDLE_DISABLE_SHARED_GEMS'] = ENV['BUNDLE_DISABLE_SHARED_GEMS'] if ENV.key?('BUNDLE_DISABLE_SHARED_GEMS') |
| 22 | + |
| 23 | + app_path = File.join(Dir.tmpdir, name) |
| 24 | + |
| 25 | + create_command = [ |
| 26 | + 'bundle', 'exec', 'rails', 'new', name, |
| 27 | + '--skip-bootsnap', '--skip-bundle', '--skip-kamal', '--skip-thruster' |
| 28 | + ] |
| 29 | + output, status = run_command(root_env, create_command, chdir: Dir.tmpdir) |
| 30 | + raise_command_error(name, create_command, status, output) unless status.success? |
| 31 | + |
| 32 | + app_env = root_env.merge('BUNDLE_GEMFILE' => File.join(app_path, 'Gemfile')) |
| 33 | + |
| 34 | + install_command = ['bundle', 'install', '--quiet'] |
| 35 | + output, status = run_command(app_env, install_command, chdir: app_path) |
| 36 | + raise_command_error(name, install_command, status, output) unless status.success? |
| 37 | + |
| 38 | + template_command = ['bundle', 'exec', 'rails', 'app:template', "LOCATION=#{template_file}"] |
| 39 | + output, status = run_command(app_env, template_command, chdir: app_path) |
| 40 | + raise_command_error(name, template_command, status, output) unless status.success? |
| 41 | + end |
| 42 | + |
| 43 | + def self.run_command(env, command, chdir:) |
| 44 | + stdout, stderr, process_status = Open3.capture3(env, *command, chdir:) |
| 45 | + ["#{stdout}#{stderr}", process_status] |
| 46 | + end |
| 47 | + |
| 48 | + def self.raise_command_error(name, command, status, output) |
| 49 | + raise <<~ERROR |
| 50 | + Failed to create test app #{name} |
| 51 | + Command: #{command.join(' ')} |
| 52 | + Exit status: #{status.exitstatus} |
| 53 | + Output: |
| 54 | + #{output} |
| 55 | + ERROR |
20 | 56 | end |
21 | 57 |
|
22 | 58 | def within_test_app(app_path, &) |
|
0 commit comments