summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouis Burda <dev@sinitax.com>2025-10-28 22:22:15 +0100
committerLouis Burda <dev@sinitax.com>2025-10-28 22:22:15 +0100
commit64c4396bca419044bbf591077eb030df3171ebf2 (patch)
tree21d96f66f45eda175d1d64303435b02592ce9b76
parente7600accd87ddb692d534519385374c1cdef1721 (diff)
downloadsubgit-64c4396bca419044bbf591077eb030df3171ebf2.tar.gz
subgit-64c4396bca419044bbf591077eb030df3171ebf2.zip
Add testcases
-rwxr-xr-xsrc/subgit-add1
-rwxr-xr-xsrc/subgit-check2
-rwxr-xr-xsrc/subgit-save2
-rwxr-xr-xsrc/subgit-write3
-rwxr-xr-xtests/run-tests.sh75
-rwxr-xr-xtests/test-add-init.sh90
-rwxr-xr-xtests/test-branches.sh76
-rwxr-xr-xtests/test-check.sh76
-rwxr-xr-xtests/test-helpers.sh113
-rwxr-xr-xtests/test-recursive.sh73
-rwxr-xr-xtests/test-save.sh73
11 files changed, 582 insertions, 2 deletions
diff --git a/src/subgit-add b/src/subgit-add
index d1082f4..1411dc8 100755
--- a/src/subgit-add
+++ b/src/subgit-add
@@ -8,6 +8,7 @@ usage() {
or commit, as a subgit repo in the current path. Can also be used
to add an existing repos.
EOF
+ )
}
branch=
diff --git a/src/subgit-check b/src/subgit-check
index 5a27b0a..d86dc32 100755
--- a/src/subgit-check
+++ b/src/subgit-check
@@ -41,7 +41,7 @@ subgit-sub subrepo "$@" | while read -r subrepo; do
fi
branch=$(git -C "$bare" branch --show-current)
- if [ "${subgitinfo[$relpath/branch]}" != "$branch" ]; then
+ if [ ! -z "${subgitinfo[$relpath/branch]}" ] && [ "${subgitinfo[$relpath/branch]}" != "$branch" ]; then
echo "$relpath: new branch - '$branch'"
fi
diff --git a/src/subgit-save b/src/subgit-save
index 570d166..f2594e8 100755
--- a/src/subgit-save
+++ b/src/subgit-save
@@ -20,7 +20,7 @@ else
parent=$(dirname "$(realpath "$a")")
[ "$root" != "$(subgit-sub root "$parent")" ] \
&& die "Subgits do not have same parent"
- subrepos+=($(subgit-sub relpath "$a"))
+ subrepos+=($(subgit-sub relpath "$root" "$a"))
done
fi
diff --git a/src/subgit-write b/src/subgit-write
new file mode 100755
index 0000000..ef5aaae
--- /dev/null
+++ b/src/subgit-write
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+subgit-source genrc > "$root/.subgitrc"
diff --git a/tests/run-tests.sh b/tests/run-tests.sh
new file mode 100755
index 0000000..73a82a3
--- /dev/null
+++ b/tests/run-tests.sh
@@ -0,0 +1,75 @@
+#!/bin/bash
+
+cd "$(dirname "$0")"
+
+TESTS=(test-*.sh)
+PARALLEL=${PARALLEL:-1}
+
+if [ ${#TESTS[@]} -eq 0 ]; then
+ echo "No tests found"
+ exit 1
+fi
+
+run_test() {
+ local test=$1
+ local output
+
+ if output=$(bash "$test" 2>&1); then
+ echo "✓ $test"
+ return 0
+ else
+ echo "✗ $test"
+ echo "$output" | sed 's/^/ /'
+ return 1
+ fi
+}
+
+export -f run_test
+
+if [ "$PARALLEL" = "1" ]; then
+ echo "Running ${#TESTS[@]} tests in parallel..."
+ echo
+
+ failed=0
+ passed=0
+
+ for test in "${TESTS[@]}"; do
+ run_test "$test" &
+ done
+
+ for job in $(jobs -p); do
+ if wait $job; then
+ ((passed++))
+ else
+ ((failed++))
+ fi
+ done
+
+ echo
+ echo "Results: $passed passed, $failed failed"
+
+ if [ $failed -gt 0 ]; then
+ exit 1
+ fi
+else
+ echo "Running ${#TESTS[@]} tests sequentially..."
+ echo
+
+ failed=0
+ passed=0
+
+ for test in "${TESTS[@]}"; do
+ if run_test "$test"; then
+ ((passed++))
+ else
+ ((failed++))
+ fi
+ echo
+ done
+
+ echo "Results: $passed passed, $failed failed"
+
+ if [ $failed -gt 0 ]; then
+ exit 1
+ fi
+fi
diff --git a/tests/test-add-init.sh b/tests/test-add-init.sh
new file mode 100755
index 0000000..28db567
--- /dev/null
+++ b/tests/test-add-init.sh
@@ -0,0 +1,90 @@
+#!/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 add and init ==="
+
+# Step 1: Create an upstream repository to track
+mkdir -p upstream/repo1
+cd upstream/repo1
+git init
+echo "test content" > file1.txt
+git add file1.txt
+git commit -m "Initial commit"
+REPO1_COMMIT=$(git rev-parse HEAD)
+cd "$TESTDIR"
+
+# Step 2: Create a parent repository and add the upstream repo as a subrepo
+mkdir parent
+cd parent
+git init
+
+subgit add ../upstream/repo1 subrepo1
+
+# Step 3: Verify that subgit add created the necessary files and directories
+
+# Check that .subgitrc configuration file was created
+if [ ! -f .subgitrc ]; then
+ echo "FAIL: .subgitrc not created"
+ exit 1
+fi
+
+# Check that subrepo is tracked in .subgitrc
+source .subgitrc
+if [ -z "${subgit[subrepo1]}" ]; then
+ echo "FAIL: subrepo1 not in subgit array"
+ exit 1
+fi
+
+# Check that bare repository was created in .subgit/
+if [ ! -d .subgit/subrepo1 ]; then
+ echo "FAIL: bare repo not created"
+ exit 1
+fi
+
+# Check that working directory was created
+if [ ! -d subrepo1 ]; then
+ echo "FAIL: working directory not created"
+ exit 1
+fi
+
+# Check that .git is a symlink to the bare repo
+if [ ! -L subrepo1/.git ]; then
+ echo "FAIL: .git symlink not created"
+ exit 1
+fi
+
+# Check that files from upstream were checked out
+if [ ! -f subrepo1/file1.txt ]; then
+ echo "FAIL: file1.txt not present in working directory"
+ exit 1
+fi
+
+# Check that the correct commit was cloned
+ACTUAL_COMMIT=$(git -C .subgit/subrepo1 rev-parse HEAD)
+if [ "$ACTUAL_COMMIT" != "$REPO1_COMMIT" ]; then
+ echo "FAIL: commit mismatch (expected $REPO1_COMMIT, got $ACTUAL_COMMIT)"
+ exit 1
+fi
+
+# Step 4: Test that init can restore a deleted working directory
+echo "=== Test: reinit should work ==="
+rm -rf subrepo1
+subgit init -f
+
+# Verify that files were restored
+if [ ! -f subrepo1/file1.txt ]; then
+ echo "FAIL: reinit did not restore working directory"
+ exit 1
+fi
+
+echo "PASS: add and init test"
diff --git a/tests/test-branches.sh b/tests/test-branches.sh
new file mode 100755
index 0000000..ffa1667
--- /dev/null
+++ b/tests/test-branches.sh
@@ -0,0 +1,76 @@
+#!/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 with branches ==="
+
+# Step 1: Create an upstream repository with multiple branches
+mkdir -p upstream/repo1
+cd upstream/repo1
+git init
+git config user.email "test@test.com"
+git config user.name "Test User"
+
+# Create master branch with file1.txt
+echo "test content" > file1.txt
+git add file1.txt
+git commit -m "Initial commit on master"
+
+# Create develop branch with file2.txt
+git checkout -b develop
+echo "develop content" > file2.txt
+git add file2.txt
+git commit -m "Commit on develop"
+DEVELOP_COMMIT=$(git rev-parse HEAD)
+
+# Switch back to master so upstream is on a different branch
+git checkout master
+cd "$TESTDIR"
+
+# Step 2: Create parent repo and track the develop branch specifically
+mkdir parent
+cd parent
+git init
+
+echo "=== Test: add with specific branch ==="
+subgit add -b develop ../upstream/repo1 subrepo1
+
+# Step 3: Verify that the develop branch was tracked
+
+# Check that branch is recorded in .subgitrc
+source .subgitrc
+if [ "${subgitinfo[subrepo1/branch]}" != "develop" ]; then
+ echo "FAIL: branch not set to develop (got ${subgitinfo[subrepo1/branch]})"
+ exit 1
+fi
+
+# Check that the bare repo is on the develop branch
+CURRENT_BRANCH=$(git -C .subgit/subrepo1 branch --show-current)
+if [ "$CURRENT_BRANCH" != "develop" ]; then
+ echo "FAIL: bare repo not on develop branch (got $CURRENT_BRANCH)"
+ exit 1
+fi
+
+# Check that develop-specific file is present
+if [ ! -f subrepo1/file2.txt ]; then
+ echo "FAIL: file2.txt from develop branch not present"
+ exit 1
+fi
+
+# Optional check: verify we're not accidentally on master
+if [ -f subrepo1/file1.txt ]; then
+ if ! git -C subrepo1 ls-tree HEAD | grep -q file1.txt; then
+ echo "Note: file1.txt exists but not in develop tree (expected)"
+ fi
+fi
+
+echo "PASS: branches test"
diff --git a/tests/test-check.sh b/tests/test-check.sh
new file mode 100755
index 0000000..df73c00
--- /dev/null
+++ b/tests/test-check.sh
@@ -0,0 +1,76 @@
+#!/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 check ==="
+
+# Step 1: Create an upstream repository
+mkdir -p upstream/repo1
+cd upstream/repo1
+git init
+git config user.email "test@test.com"
+git config user.name "Test User"
+echo "test content" > file1.txt
+git add file1.txt
+git commit -m "Initial commit"
+cd "$TESTDIR"
+
+# Step 2: Create parent repo and add upstream as subrepo
+mkdir parent
+cd parent
+git init
+
+subgit add "$TESTDIR/upstream/repo1" subrepo1
+
+# Step 3: Test that clean repo produces no check warnings
+echo "=== Test: clean repo should have no output ==="
+OUTPUT=$(subgit check subrepo1)
+if [ ! -z "$OUTPUT" ]; then
+ echo "FAIL: clean repo should have no check output, got: $OUTPUT"
+ exit 1
+fi
+
+# Step 4: Test detection of uncommitted changes (dirty tree)
+echo "=== Test: dirty tree detection ==="
+echo "modified" > subrepo1/file1.txt
+OUTPUT=$(subgit check subrepo1)
+if ! echo "$OUTPUT" | grep -q "dirty tree"; then
+ echo "FAIL: dirty tree not detected"
+ exit 1
+fi
+
+# Restore file to clean state
+git -C subrepo1 checkout file1.txt
+
+# Step 5: Test detection of new commits in bare repo
+echo "=== Test: new commit detection ==="
+
+# Add a new commit to upstream
+cd "$TESTDIR/upstream/repo1"
+echo "new content" > file2.txt
+git add file2.txt
+git commit -m "Second commit"
+
+# Fetch and update bare repo to have the new commit
+cd "$TESTDIR/parent"
+git -C .subgit/subrepo1 fetch
+git -C .subgit/subrepo1 update-ref refs/heads/master refs/remotes/origin/master
+
+# Check should report that bare repo has a newer commit than .subgitrc
+OUTPUT=$(subgit check subrepo1)
+if ! echo "$OUTPUT" | grep -q "new commit"; then
+ echo "FAIL: new commit not detected"
+ exit 1
+fi
+
+echo "PASS: check test"
diff --git a/tests/test-helpers.sh b/tests/test-helpers.sh
new file mode 100755
index 0000000..4421dfd
--- /dev/null
+++ b/tests/test-helpers.sh
@@ -0,0 +1,113 @@
+#!/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"
diff --git a/tests/test-recursive.sh b/tests/test-recursive.sh
new file mode 100755
index 0000000..28cd9cf
--- /dev/null
+++ b/tests/test-recursive.sh
@@ -0,0 +1,73 @@
+#!/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: recursive subgit ==="
+
+# 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 repo1"
+cd ../repo2
+git init
+echo "test2" > file2.txt
+git add file2.txt
+git commit -m "Initial commit repo2"
+cd "$TESTDIR"
+
+# Step 2: Create parent repo with a subrepo that itself has a nested subrepo
+mkdir parent
+cd parent
+git init
+
+# Add first-level subrepo
+subgit add "$TESTDIR/upstream/repo1" subrepo1
+
+# Make the subrepo directory into a subgit container with its own subrepo
+cd subrepo1
+git init
+subgit add "$TESTDIR/upstream/repo2" nested-repo
+
+cd "$TESTDIR/parent"
+
+# Step 3: Test that init without -r flag does not initialize nested subrepos
+echo "=== Test: init without -r flag ==="
+rm -rf subrepo1/nested-repo
+subgit init -f subrepo1
+
+# nested-repo should not exist (not initialized recursively)
+if [ -d subrepo1/nested-repo ]; then
+ echo "FAIL: nested repo should not be initialized without -r flag"
+ exit 1
+fi
+
+# Step 4: Test that init with -r flag initializes nested subrepos
+echo "=== Test: init with -r flag ==="
+subgit init -r -f subrepo1
+
+# nested-repo should now exist with its files
+if [ ! -f subrepo1/nested-repo/file2.txt ]; then
+ echo "FAIL: nested repo not initialized with -r flag"
+ exit 1
+fi
+
+# Verify .git symlink was created for nested repo
+if [ ! -L subrepo1/nested-repo/.git ]; then
+ echo "FAIL: nested repo .git symlink not created"
+ exit 1
+fi
+
+echo "PASS: recursive test"
diff --git a/tests/test-save.sh b/tests/test-save.sh
new file mode 100755
index 0000000..54efa0d
--- /dev/null
+++ b/tests/test-save.sh
@@ -0,0 +1,73 @@
+#!/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 save ==="
+
+# Step 1: Create an upstream repository with an initial commit
+mkdir -p upstream/repo1
+cd upstream/repo1
+git init
+git config user.email "test@test.com"
+git config user.name "Test User"
+echo "test content" > file1.txt
+git add file1.txt
+git commit -m "Initial commit"
+COMMIT1=$(git rev-parse HEAD)
+cd "$TESTDIR"
+
+# Step 2: Create parent repo and add upstream, pinned to specific commit
+mkdir parent
+cd parent
+git init
+
+subgit add -c "$COMMIT1" "$TESTDIR/upstream/repo1" subrepo1
+
+# Verify the initial commit was recorded in .subgitrc
+source .subgitrc
+if [ "${subgitinfo[subrepo1/commit]}" != "$COMMIT1" ]; then
+ echo "FAIL: initial commit not recorded correctly"
+ exit 1
+fi
+
+# Step 3: Add a new commit to upstream repository
+cd "$TESTDIR/upstream/repo1"
+echo "new content" > file2.txt
+git add file2.txt
+git commit -m "Second commit"
+COMMIT2=$(git rev-parse HEAD)
+
+# Step 4: Fetch the new commit and update bare repo to point to it
+cd "$TESTDIR/parent"
+git -C .subgit/subrepo1 fetch
+git -C .subgit/subrepo1 update-ref refs/heads/master refs/remotes/origin/master
+
+# Step 5: Use 'subgit save' to update .subgitrc with the new commit
+subgit save subrepo1
+
+# Step 6: Verify that .subgitrc was updated with the new commit
+
+# Check in-memory after sourcing
+source .subgitrc
+if [ "${subgitinfo[subrepo1/commit]}" != "$COMMIT2" ]; then
+ echo "FAIL: save did not update commit (expected $COMMIT2, got ${subgitinfo[subrepo1/commit]})"
+ exit 1
+fi
+
+# Check that file was actually written with new commit
+if ! grep -q "$COMMIT2" .subgitrc; then
+ echo "FAIL: .subgitrc does not contain new commit"
+ exit 1
+fi
+
+echo "PASS: save test"