From ab17a6cec5b473bc70a94bcaa03fd090cc08cb09 Mon Sep 17 00:00:00 2001 From: Andrew Novoselac Date: Mon, 8 Apr 2024 14:26:38 -0400 Subject: [PATCH] When generating an app with the --dev flag, mount the local rails repo in the devcontainer This is useful for testing changes to how the devcontainer is generated, and will be necessary for creating a CI workflow for testing the devcontainer setup. --- railties/lib/rails/generators/devcontainer.rb | 18 ++++++++++++++++++ .../.devcontainer/devcontainer.json.tt | 6 ++++++ railties/test/generators/app_generator_test.rb | 12 ++++++++++++ .../test/generators/generators_test_helper.rb | 6 ++++++ 4 files changed, 42 insertions(+) diff --git a/railties/lib/rails/generators/devcontainer.rb b/railties/lib/rails/generators/devcontainer.rb index 295b6e595b5e3..45f621ce08816 100644 --- a/railties/lib/rails/generators/devcontainer.rb +++ b/railties/lib/rails/generators/devcontainer.rb @@ -39,6 +39,16 @@ def devcontainer_volumes @devcontainer_volumes end + def devcontainer_mounts + return @devcontainer_mounts if @devcontainer_mounts + + @devcontainer_mounts = [] + + @devcontainer_mounts << local_rails_mount if options.dev? + + @devcontainer_mounts + end + def devcontainer_needs_redis? !(options.skip_action_cable? && options.skip_active_job?) end @@ -127,6 +137,14 @@ def mariadb_service def db_service_names ["mysql", "mariadb", "postgres"] end + + def local_rails_mount + { + type: "bind", + source: Rails::Generators::RAILS_DEV_PATH, + target: Rails::Generators::RAILS_DEV_PATH + } + end end end end diff --git a/railties/lib/rails/generators/rails/app/templates/.devcontainer/devcontainer.json.tt b/railties/lib/rails/generators/rails/app/templates/.devcontainer/devcontainer.json.tt index 18a9009b68b0d..988eafbc4055c 100644 --- a/railties/lib/rails/generators/rails/app/templates/.devcontainer/devcontainer.json.tt +++ b/railties/lib/rails/generators/rails/app/templates/.devcontainer/devcontainer.json.tt @@ -29,6 +29,12 @@ // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // "remoteUser": "root", +<%- if !devcontainer_mounts.empty? -%> + "mounts": [ + <%= devcontainer_mounts.map { |mount| "{\n " + mount.map { |key, value| "\"#{key}\": \"#{value}\"" }.join(",\n ") + "\n }" }.join(",\n ") %> + ], +<%- end -%> + // Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": "bin/setup" } diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 207a5948437f9..20bd5c1f18d62 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -1415,6 +1415,18 @@ def test_devcontainer_no_depends_on_when_no_dependencies end end + def test_devcontainer_dev_flag_mounts_local_rails_repo + run_generator_using_prerelease [ destination_root, "--dev" ] + + assert_devcontainer_json_file do |devcontainer_config| + rails_mount = devcontainer_config["mounts"].sole + + assert_equal "bind", rails_mount["type"] + assert_equal Rails::Generators::RAILS_DEV_PATH, rails_mount["source"] + assert_equal Rails::Generators::RAILS_DEV_PATH, rails_mount["target"] + end + end + def test_skip_devcontainer run_generator [ destination_root, "--skip-devcontainer" ] diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index 9e18c7adc3162..b125fd5726966 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -122,6 +122,12 @@ def assert_compose_file end end + def assert_devcontainer_json_file + assert_file ".devcontainer/devcontainer.json" do |content| + yield JSON.load(content) + end + end + private def gemfile_locals {