#!/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}" echo important > untracked_file run main echo "${output}" | grep "Updated" [ "$(cat a)" = "3" ] [ "$(cat untracked_file)" = "important" ] [ "${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" ] }