aboutsummaryrefslogtreecommitdiff
path: root/tests/test-reconcile--pull-dirty.el
blob: 2ba1f5d108711c51a4267fbbed603c2c285a2d5e (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
;;; test-reconcile--pull-dirty.el --- Tests for cj/reconcile--pull-dirty -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the dirty-repo reconciliation: stash, pull, pop, magit.

;;; Code:

(require 'ert)
(require 'testutil-reconcile-open-repos)
(require 'reconcile-open-repos)

;;; Normal Cases

(ert-deftest test-pull-dirty-normal-stash-pull-pop-success ()
  "When stash, pull, and pop all succeed, magit is still opened."
  (reconcile-test-with-temp-dirs
   ("repo/.git/")
   (let ((dir (expand-file-name "repo" test-root)))
     (reconcile-test-with-magit-mock
       (reconcile-test-with-shell-mocks
           (lambda (_cmd) 0)
           (lambda (_cmd) "")
         (cj/reconcile--pull-dirty dir))
       (should (member dir reconcile-test-magit-calls))))))

(ert-deftest test-pull-dirty-normal-stash-fails-opens-magit ()
  "When stash fails, magit is opened and warning emitted."
  (reconcile-test-with-temp-dirs
   ("repo/.git/")
   (let ((dir (expand-file-name "repo" test-root))
         (messages nil))
     (reconcile-test-with-magit-mock
       (reconcile-test-with-shell-mocks
           (lambda (cmd)
             (if (string-match-p "stash --quiet\\'" cmd) 1 0))
           (lambda (_cmd) "")
         (cl-letf (((symbol-function 'message)
                    (lambda (fmt &rest args) (push (apply #'format fmt args) messages))))
           (cj/reconcile--pull-dirty dir)))
       (should (member dir reconcile-test-magit-calls))
       (should (cl-some (lambda (m) (string-match-p "stash failed" m)) messages))))))

(ert-deftest test-pull-dirty-normal-pull-fails-warns ()
  "When stash succeeds but pull fails, warning mentions pull failure."
  (reconcile-test-with-temp-dirs
   ("repo/.git/")
   (let ((dir (expand-file-name "repo" test-root))
         (messages nil))
     (reconcile-test-with-magit-mock
       (reconcile-test-with-shell-mocks
           (lambda (cmd)
             (cond ((string-match-p "stash --quiet\\'" cmd) 0)
                   ((string-match-p "pull" cmd) 1)
                   (t 0)))
           (lambda (_cmd) "")
         (cl-letf (((symbol-function 'message)
                    (lambda (fmt &rest args) (push (apply #'format fmt args) messages))))
           (cj/reconcile--pull-dirty dir)))
       (should (cl-some (lambda (m) (string-match-p "git pull failed" m)) messages))))))

(ert-deftest test-pull-dirty-normal-stash-pop-fails-warns ()
  "When stash and pull succeed but pop fails, warning mentions stash pop."
  (reconcile-test-with-temp-dirs
   ("repo/.git/")
   (let ((dir (expand-file-name "repo" test-root))
         (messages nil))
     (reconcile-test-with-magit-mock
       (reconcile-test-with-shell-mocks
           (lambda (cmd)
             (cond ((string-match-p "stash pop" cmd) 1)
                   ((string-match-p "stash" cmd) 0)
                   (t 0)))
           (lambda (_cmd) "")
         (cl-letf (((symbol-function 'message)
                    (lambda (fmt &rest args) (push (apply #'format fmt args) messages))))
           (cj/reconcile--pull-dirty dir)))
       (should (cl-some (lambda (m) (string-match-p "stash pop failed" m)) messages))))))

;;; Boundary Cases

(ert-deftest test-pull-dirty-boundary-always-opens-magit ()
  "Magit is opened regardless of whether pull succeeds or fails."
  (reconcile-test-with-temp-dirs
   ("repo/.git/")
   (let ((dir (expand-file-name "repo" test-root)))
     ;; Test with pull failure
     (reconcile-test-with-magit-mock
       (reconcile-test-with-shell-mocks
           (lambda (cmd)
             (if (string-match-p "pull" cmd) 1 0))
           (lambda (_cmd) "")
         (cl-letf (((symbol-function 'message) (lambda (_fmt &rest _args))))
           (cj/reconcile--pull-dirty dir)))
       (should (member dir reconcile-test-magit-calls))))))

(ert-deftest test-pull-dirty-boundary-uncommitted-work-message ()
  "Always emits 'contains uncommitted work' message."
  (reconcile-test-with-temp-dirs
   ("repo/.git/")
   (let ((dir (expand-file-name "repo" test-root))
         (messages nil))
     (reconcile-test-with-magit-mock
       (reconcile-test-with-shell-mocks
           (lambda (_cmd) 0)
           (lambda (_cmd) "")
         (cl-letf (((symbol-function 'message)
                    (lambda (fmt &rest args) (push (apply #'format fmt args) messages))))
           (cj/reconcile--pull-dirty dir)))
       (should (cl-some (lambda (m) (string-match-p "uncommitted work" m)) messages))))))

(provide 'test-reconcile--pull-dirty)
;;; test-reconcile--pull-dirty.el ends here