From 8a56587d62d08606e5ed11eb1b93e238cf5aa2fe Mon Sep 17 00:00:00 2001
From: Avdi Grimm <avdi@users.noreply.github.com>
Date: Thu, 15 Dec 2022 10:40:36 -0600
Subject: [PATCH] Improve devcontainer for running tests (#22277)

* Improve devcontainer for running tests

- Pull devcontainer post-create out into its own script
- Add asset precompilation
- Add test-mode asset precompilation (needed to run tests without error)

* Document Gemfile.lock re-checkout in devcontainer
---
 .devcontainer/devcontainer.json |  2 +-
 .devcontainer/post-create.sh    | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100755 .devcontainer/post-create.sh

diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
index 47497794f..5ac56c842 100644
--- a/.devcontainer/devcontainer.json
+++ b/.devcontainer/devcontainer.json
@@ -20,7 +20,7 @@
   "forwardPorts": [3000, 4000],
 
   // Use 'postCreateCommand' to run commands after the container is created.
-  "postCreateCommand": "bundle install --path vendor/bundle && yarn install && git checkout -- Gemfile.lock && ./bin/rails db:setup",
+  "postCreateCommand": ".devcontainer/post-create.sh",
 
   // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
   "remoteUser": "vscode"
diff --git a/.devcontainer/post-create.sh b/.devcontainer/post-create.sh
new file mode 100755
index 000000000..02f488f12
--- /dev/null
+++ b/.devcontainer/post-create.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+set -e # Fail the whole script on first error
+
+# Fetch Ruby gem dependencies
+bundle install --path vendor/bundle --with='development test'
+
+# Fetch Javascript dependencies
+yarn install
+
+# Make Gemfile.lock pristine again
+git checkout -- Gemfile.lock
+
+# [re]create, migrate, and seed the test database
+RAILS_ENV=test ./bin/rails db:setup
+
+# Precompile assets for development
+RAILS_ENV=development ./bin/rails assets:precompile
+
+# Precompile assets for test
+RAILS_ENV=test NODE_ENV=tests ./bin/rails assets:precompile