aboutsummaryrefslogtreecommitdiff
path: root/scripts/tests/roam-sync.bats
blob: 017a92c4b7fa5777315b2583569b2585ce072b58 (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
#!/usr/bin/env bats
# Tests for scripts/roam-sync.sh — the git auto-sync loop the roam KB timer runs.
# Each test builds a throwaway clone + bare remote pair under $BATS_TEST_TMPDIR.

setup() {
  SCRIPT="$BATS_TEST_DIRNAME/../roam-sync.sh"
  export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null
  export GIT_AUTHOR_NAME=test GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=test GIT_COMMITTER_EMAIL=t@t
  REMOTE="$BATS_TEST_TMPDIR/remote.git"
  REPO="$BATS_TEST_TMPDIR/repo"
  OTHER="$BATS_TEST_TMPDIR/other"
  git init -q --bare -b main "$REMOTE"
  git init -q -b main "$REPO"
  (cd "$REPO" && echo seed > seed.org && git add -A && git commit -qm seed \
    && git remote add origin "$REMOTE" && git push -qu origin main)
}

clone_other() {
  git clone -q "$REMOTE" "$OTHER" 2>/dev/null
}

@test "clean tree, current remote: exits 0 with no new commit" {
  before=$(git -C "$REPO" rev-parse HEAD)
  run "$SCRIPT" "$REPO"
  [ "$status" -eq 0 ]
  [ "$(git -C "$REPO" rev-parse HEAD)" = "$before" ]
}

@test "dirty tree: commits and pushes to the remote" {
  echo "new fact" > "$REPO/fact.org"
  run "$SCRIPT" "$REPO"
  [ "$status" -eq 0 ]
  [ -z "$(git -C "$REPO" status --porcelain)" ]
  git -C "$REMOTE" log --format=%s main | grep -q "auto-sync"
}

@test "remote ahead: pulls the remote commit" {
  clone_other
  (cd "$OTHER" && echo upstream > up.org && git add -A && git commit -qm upstream && git push -q)
  run "$SCRIPT" "$REPO"
  [ "$status" -eq 0 ]
  [ -f "$REPO/up.org" ]
}

@test "local dirty and remote ahead, disjoint files: both land" {
  clone_other
  (cd "$OTHER" && echo upstream > up.org && git add -A && git commit -qm upstream && git push -q)
  echo "local fact" > "$REPO/local.org"
  run "$SCRIPT" "$REPO"
  [ "$status" -eq 0 ]
  [ -f "$REPO/up.org" ]
  git -C "$REMOTE" log --format=%s main | grep -q "auto-sync"
}

@test "conflicting rebase: exits nonzero and leaves no rebase in progress" {
  clone_other
  (cd "$OTHER" && echo theirs > seed.org && git add -A && git commit -qm theirs && git push -q)
  echo mine > "$REPO/seed.org"
  run "$SCRIPT" "$REPO"
  [ "$status" -ne 0 ]
  [ ! -d "$REPO/.git/rebase-merge" ]
  [ ! -d "$REPO/.git/rebase-apply" ]
  # the local change survives as a commit for the human to resolve later
  git -C "$REPO" log --format=%s | grep -q "auto-sync"
}

@test "missing repo path: exits nonzero with a message" {
  run "$SCRIPT" "$BATS_TEST_TMPDIR/nonexistent"
  [ "$status" -ne 0 ]
  [[ "$output" == *"not a git repo"* ]]
}