Files
myos/spec/support/run.sh
T
Yann Autissier 55fae625d6 keep the dynamism of make in pure shell
Two mechanisms, matching what the make engine actually did:

Lazy defaults. A stack setting is a function myos_default_<VAR>, called only
when the variable has no value, and called again at every reference. That is
exactly a recursive ?=: an explicit value wins, and the default follows a
DOMAIN that a .env changes later. The prefix is what makes it safe; the first
version used a bare function named after the variable, and the test suite
caught it running /usr/bin/host for a stack group called host.

Templates. myos env-update fills a .env from the .env.dist files, expanding
${VAR} against the current values and running $(command), forward references
included.

Also fixed: the project .env now wins over /etc/conf.d/myos, which is what the
documentation claimed and the code did not.

share/make/shim.mk lets a project keep make as a front end: every myos command
becomes a target that shells out to bin/myos, and the project keeps its own
targets and its stack .mk files. It sits outside make/ because the legacy
engine globs every .mk in there.
2026-09-03 20:46:28 +02:00

77 lines
3.1 KiB
Bash

#!/bin/sh
# Helpers shared by the golden recorder and the shellspec suites.
# myos_run_engine ENGINE WORKDIR ARGS... runs myos (legacy make engine or bin/myos CLI)
# in a hermetic environment and prints normalized stdout+stderr followed by "[exit N]".
MYOS_ROOT="${MYOS_ROOT:-$(cd "$(dirname "$0")/../.." 2>/dev/null && pwd)}"
SPEC_DIR="${SPEC_DIR:-$MYOS_ROOT/spec}"
# myos_sandbox FIXTURE -> prints a temp dir containing a copy of the fixture (wd/) and of the home fixture (home/)
myos_sandbox() {
_fixture=$1
_tmp=$(mktemp -d "${TMPDIR:-/tmp}/myos-spec.XXXXXX"); _tmp=$(cd "$_tmp" && pwd -P)
cp -R "$SPEC_DIR/fixtures/$_fixture" "$_tmp/wd"
cp -R "$SPEC_DIR/fixtures/home" "$_tmp/home"
printf '%s\n' "$_tmp"
}
# myos_hermetic_env SANDBOX -> prints the env(1) arguments for a hermetic run
myos_hermetic_env() {
printf '%s\n' \
"PATH=$SPEC_DIR/support/bin:/usr/bin:/bin:/usr/sbin:/sbin" \
"HOME=$1/home" \
"USER=tester" "HOSTNAME=testhost" "DOMAIN=example.test" \
"DOCKER_MACHINE=x86_64" "DOCKER_SYSTEM=Linux" "DOCKER_SOCKET_LOCATION=/var/run/docker.sock" \
"DRYRUN=true" "TERM=dumb" "LANG=C" "LC_ALL=C" \
"MYOS_DOCKER_LOG=$1/docker.log"
}
# myos_normalize SANDBOX (stdin -> stdout)
# APPS, BRANCH and VERSION depend on where the myos checkout lives and on its git state,
# so they are masked to keep the goldens reproducible across machines.
myos_normalize() {
sed -e 's/\x1b\[[0-9;]*m//g' \
-e "s#$MYOS_ROOT#@MYOS@#g" \
-e "s#$1/wd#@WD@#g" \
-e "s#$1/home#@HOME@#g" \
-e "s#$1#@TMP@#g" \
-e 's#/[^ ]*/bin/make#make#g' \
-e 's/^APPS .*/APPS @APPS@/' \
-e 's/^BRANCH .*/BRANCH @BRANCH@/' \
-e 's/^VERSION .*/VERSION @VERSION@/' \
-e 's/[[:space:]][[:space:]]*/ /g' \
-e 's/[[:space:]]*$//'
}
# myos_run_engine ENGINE SANDBOX ARGS...
# shellcheck disable=SC2046 # myos_hermetic_env output is meant to be word-split
myos_run_engine() {
_engine=$1; _sb=$2; shift 2
# "@make" as first arg = the project drives make itself: its Makefile includes
# either the legacy engine (make/include.mk) or the shim (make/shim.mk), and
# make runs from the project directory.
if [ "${1:-}" = "@make" ]; then
shift
_out=$(cd "$_sb/wd" && env -i $(myos_hermetic_env "$_sb") MYOS_CONF=/dev/null \
make -es MYOS="$MYOS_ROOT" "$@" 2>&1); _rc=$?
printf '%s\n[exit %s]\n' "$_out" "$_rc" | myos_normalize "$_sb"
return 0
fi
case $_engine in
legacy)
# what /usr/local/bin/myos does: env from system conf + MYOS=. WORKDIR=$PWD make -esC $MYOS
_out=$(cd "$_sb/wd" && env -i $(myos_hermetic_env "$_sb") \
make -esC "$MYOS_ROOT" MYOS=. WORKDIR="$_sb/wd" "$@" 2>&1); _rc=$?
;;
cli)
# MYOS_PROJECT_FORMAT pins the legacy naming so that the goldens compare
# resolution, not naming; the new default is covered by the unit tests.
_out=$(cd "$_sb/wd" && env -i $(myos_hermetic_env "$_sb") MYOS_CONF=/dev/null \
MYOS_PROJECT_FORMAT=user-app-env \
"$MYOS_ROOT/bin/myos" -C "$_sb/wd" "$@" 2>&1); _rc=$?
;;
*) echo "unknown engine $_engine" >&2; return 2 ;;
esac
printf '%s\n[exit %s]\n' "$_out" "$_rc" | myos_normalize "$_sb"
}