summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorflu0r1ne <flu0r1ne@flu0r1ne.net>2021-08-29 22:45:25 -0500
committerflu0r1ne <flu0r1ne@flu0r1ne.net>2021-08-29 22:45:25 -0500
commite556b90e39d77c8b6a5eeb8b74d0b2c28272a932 (patch)
treed931cc27824e4317c35192847345bfa439ad7f64
parentb5decf314d7219e338fc70af0991a52b1d9faef2 (diff)
downloadgit-update-agent-e556b90e39d77c8b6a5eeb8b74d0b2c28272a932.tar.xz
git-update-agent-e556b90e39d77c8b6a5eeb8b74d0b2c28272a932.zip
Add test cases
-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" ]
+}