summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtest.bats119
1 files changed, 119 insertions, 0 deletions
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" ]
+}