#!/usr/bin/env bats # Cross Events' hooks that don't need a server: which shared packages a checkout builds first # (contracts and seo from 2.4.0), and that an older checkout (2.3.x) builds without them. # Run with `npm run test:scripts`. setup() { # shellcheck source=../products/events.sh source "$BATS_TEST_DIRNAME/../products/events.sh" TMP="$BATS_TEST_TMPDIR" CALLS="$TMP/calls" : > "$CALLS" # Record what would run, as the app user, instead of running it. run_as() { local user="$1" dir="$2"; shift 2; echo "$user|${dir#"$TMP"/}|$*" >> "$CALLS"; } } checkout() { local dir="$TMP/$1"; shift mkdir -p "$dir/apps/events/backend" "$dir/apps/events/frontend" "$dir/packages/contracts" "$dir/packages/seo" echo '{"name":"@crosscode/contracts"}' > "$dir/packages/contracts/package.json" echo '{"name":"@crosscode/seo"}' > "$dir/packages/seo/package.json" printf '{"dependencies":{%s}}' "$1" > "$dir/apps/events/backend/package.json" printf '{"dependencies":{%s}}' "$2" > "$dir/apps/events/frontend/package.json" } @test "2.4.0: contracts and seo are installed and built before Events" { checkout new '"@crosscode/contracts":"*"' '"@crosscode/contracts":"*","@crosscode/seo":"*"' run events_shared_packages "$TMP/new" [ "$output" = $'packages/contracts\npackages/seo' ] events_install_deps "$TMP/new" hope grep -qx 'hope|new|npm ci --include=dev --no-audit --no-fund -w packages/contracts -w packages/seo --include-workspace-root -w apps/events/backend -w apps/events/frontend' "$CALLS" : > "$CALLS" events_build "$TMP/new" hope [ "$(cut -d'|' -f2 "$CALLS" | tr '\n' ' ')" = "new/packages/contracts new/packages/seo new/apps/events/backend new/apps/events/frontend " ] } @test "2.3.x (no shared packages used): installed and built as before" { checkout old '' '' run events_shared_packages "$TMP/old" [ -z "$output" ] events_install_deps "$TMP/old" hope grep -qx 'hope|old|npm ci --include=dev --no-audit --no-fund -w apps/events/backend -w apps/events/frontend' "$CALLS" : > "$CALLS" events_build "$TMP/old" hope [ "$(cut -d'|' -f2 "$CALLS" | tr '\n' ' ')" = "old/apps/events/backend old/apps/events/frontend " ] } @test "a failing package build stops before Events is built" { checkout new '"@crosscode/contracts":"*"' '' run_as() { echo "${2#"$TMP"/}" >> "$CALLS"; [[ "$2" != */packages/contracts ]]; } run events_build "$TMP/new" hope [ "$status" -ne 0 ] [ "$(cat "$CALLS")" = "new/packages/contracts" ] }