summaryrefslogtreecommitdiff
path: root/test.bats
blob: f4d04b4c26278f7daa6f3c634a86c07dd1e6c29d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/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" ]
}