#!/bin/bash set -e # Setup: Add src directory to PATH and create isolated test directory SRCDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../src" && pwd)" export PATH="$SRCDIR:$PATH" TESTDIR=$(mktemp -d) trap "rm -rf '$TESTDIR'" EXIT cd "$TESTDIR" echo "=== Test: subgit helper commands ===" # Step 1: Create two upstream repositories mkdir -p upstream/repo1 upstream/repo2 cd upstream/repo1 git init echo "test1" > file1.txt git add file1.txt git commit -m "Initial commit" cd ../repo2 git init echo "test2" > file2.txt git add file2.txt git commit -m "Initial commit" cd "$TESTDIR" # Step 2: Create parent repo and add both upstreams as subrepos mkdir parent cd parent git init subgit add ../upstream/repo1 subrepo1 subgit add ../upstream/repo2 subrepo2 # Step 3: Test 'subgit ls' - lists all tracked subrepos echo "=== Test: subgit ls ===" OUTPUT=$(subgit ls | sort) EXPECTED=$(printf "subrepo1\nsubrepo2" | sort) if [ "$OUTPUT" != "$EXPECTED" ]; then echo "FAIL: ls output incorrect" echo "Expected: $EXPECTED" echo "Got: $OUTPUT" exit 1 fi # Step 4: Test 'subgit root' - finds the subgit container root echo "=== Test: subgit root ===" # From current directory ROOT=$(subgit root .) if [ "$ROOT" != "$PWD" ]; then echo "FAIL: root should be $PWD, got $ROOT" exit 1 fi # From a path inside a subrepo ROOT=$(subgit root subrepo1/file1.txt) if [ "$ROOT" != "$PWD" ]; then echo "FAIL: root from subrepo path should be $PWD, got $ROOT" exit 1 fi # Step 5: Test 'subgit parent' - finds which container tracks a path echo "=== Test: subgit parent ===" cd subrepo1 PARENT=$(subgit parent .) if [ "$PARENT" != "$TESTDIR/parent" ]; then echo "FAIL: parent should be $TESTDIR/parent, got $PARENT" exit 1 fi cd .. # Step 6: Test 'subgit relpath' - finds the tracking root for a path echo "=== Test: subgit relpath ===" # Direct subrepo path RELPATH=$(subgit relpath "$PWD" "$PWD/subrepo1") if [ "$RELPATH" != "subrepo1" ]; then echo "FAIL: relpath should be subrepo1, got $RELPATH" exit 1 fi # Nested path within subrepo (should still return subrepo root) RELPATH=$(subgit relpath "$PWD" "$PWD/subrepo1/subdir") if [ "$RELPATH" != "subrepo1" ]; then echo "FAIL: relpath for nested path should be subrepo1, got $RELPATH" exit 1 fi # Step 7: Test 'subgit subrepo' - resolves paths to subrepos echo "=== Test: subgit subrepo ===" # Without arguments, lists all subrepos as absolute paths OUTPUT=$(subgit subrepo | sort) EXPECTED=$(printf "$PWD/subrepo1\n$PWD/subrepo2" | sort) if [ "$OUTPUT" != "$EXPECTED" ]; then echo "FAIL: subrepo output incorrect" echo "Expected: $EXPECTED" echo "Got: $OUTPUT" exit 1 fi # With argument, normalizes path to its subrepo root SUBREPO=$(subgit subrepo subrepo1) if [ "$SUBREPO" != "$PWD/subrepo1" ]; then echo "FAIL: subrepo with path should return $PWD/subrepo1, got $SUBREPO" exit 1 fi echo "PASS: helper commands test"