From e556b90e39d77c8b6a5eeb8b74d0b2c28272a932 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Sun, 29 Aug 2021 22:45:25 -0500 Subject: Add test cases --- test.bats | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100755 test.bats (limited to 'test.bats') diff --git a/test.bats b/test.bats new file mode 100755 index 0000000..cd95eec --- /dev/null +++ b/test.bats @@ -0,0 +1,119 @@ +#!/usr/bin/env bats +# +# TESTS git-update-agent using bats +# Clones a repository, tests update behavior, and ensure remote is set accordingly + +export REMOTE=updates + +setup_parent_repo() { + export PARENT="parent-repo" + mkdir "$PARENT" + cd "$PARENT" + git init + echo 1 > a + echo 2 > b + git add a b + git commit -m "Add a and b" + cd .. +} + +clone_parent_repo() { + export CHILD="child-repo" + + git-update-agent \ + --init-with "../${PARENT}" \ + --dir "${CHILD}" +} + +setup_file() { + export TMP="$(mktemp -d)" + cd "$TMP" + + setup_parent_repo + clone_parent_repo +} + +teardown_file() { + rm -rf "$TMP" +} + + +hash_files() { + cat $(git ls-files | xargs) | sha256sum +} + +main() { + bash git-update-agent +} + +@test "Clone exists" { + [ -d "${CHILD}" ] +} + +@test "Files exists" { + + local expected_file + + expected_files() { +cat <<_EOF +a +b +_EOF + } + + cd "${CHILD}" + [ "$(git ls-files)" = "$(expected_files)" ] + [ "$(cat a)" = "1" ] + [ "$(cat b)" = "2" ] +} + +@test "Does not update files if remote is not updated" { + cd "${CHILD}" + + local initial_hash + local final_hash + + initial_hash="$(hash_files)" + + run main + + final_hash="$(hash_files)" + + echo "Testing if files have changed" + echo "${initial_hash} = ${final_hash}" + + echo "$output" + + [ "$initial_hash" = "$final_hash" ] + + echo "$output" | grep "up to date" + + [ "${status}" -eq 0 ] +} + +@test "Child updates if parent updates" { + cd "${PARENT}" + echo 3 > a + + git add a + git commit -m "Rewrite a" + + cd "${TMP}/${CHILD}" + + run main + + echo "${output}" | grep "Updated" + + [ "$(cat a)" = "3" ] + [ "${status}" -eq 0 ] +} + +@test "Remote is set properly" { + cd "${CHILD}" + [ "$(git remote)" = "$REMOTE" ] +} + +@test "Remote head is set" { + cd "${CHILD}" + [ "$(git symbolic-ref refs/remotes/${REMOTE}/HEAD)" = "refs/remotes/${REMOTE}/main" ] +} -- cgit v1.2.3