summaryrefslogtreecommitdiff
path: root/magit-annex-tests.el
blob: ee59035f853077fcf809393abab3e35d6e260d2d (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;;; magit-annex-tests.el --- Tests for Magit-annex

;; Copyright (C) 2014-2021 Kyle Meyer <kyle@kyleam.com>
;;
;; License: GPLv3

;;; Code:

(require 'cl-lib)
(require 'dash)
(require 'ert)

(require 'magit-annex)

;;; Utilities

(defun magit-annex-tests-kill-buffers (directory)
  (let ((default-directory directory))
    (dolist (buf (-remove #'buffer-base-buffer (magit-mode-get-buffers)))
      (kill-buffer buf))))

(defmacro magit-annex-tests-wait (&rest body)
  (declare (indent 0) (debug t))
  (let ((wait (cl-gensym "magit-annex-tests-wait")))
    `(prog1 ,(macroexp-progn body)
       (let ((,wait t))
         (when magit-this-process
           (set-process-sentinel
            magit-this-process
            (lambda (process event)
              (magit-process-sentinel process event)
              (when (memq (process-status process) '(exit signal))
                (setq ,wait nil))))
           (let ((start (float-time)))
             (while (and magit-this-process ,wait)
               (when (< 30 (time-to-seconds
                            (time-subtract (float-time) start)))
                 (error "Process shouldn't take this long"))
               (sleep-for 0.005))))))))

;; Modified from Magit's magit-with-test-directory.
(defmacro magit-annex-with-test-directory (&rest body)
  (declare (indent 0) (debug t))
  (let ((dir (make-symbol "dir")))
    `(let ((,dir (file-name-as-directory (make-temp-file "magit-annex-" t)))
           (process-environment process-environment))
       (push "GIT_AUTHOR_NAME=A U Thor" process-environment)
       (push "GIT_AUTHOR_EMAIL=a.u.thor@example.com" process-environment)
       (condition-case err
           (cl-letf (((symbol-function #'message) #'format))
             (let ((default-directory ,dir))
               ,@body))
         (error (message "Keeping test directory and buffers:\n  %s" ,dir)
                (signal (car err) (cdr err))))
       (magit-annex-tests-kill-buffers ,dir)
       (delete-directory ,dir t))))

(defmacro magit-annex-with-test-repo (&rest body)
  (declare (indent 0) (debug t))
  `(magit-annex-with-test-directory
     (magit-call-git "init" ".")
     (magit-call-git "annex" "init" "test-repo")
     (unwind-protect
         (progn ,@body)
       (call-process "chmod" nil nil nil "-R" "777" "."))))

(defmacro magit-annex-with-test-repo-pair (&rest body)
  (declare (indent 0) (debug t))
  `(let ((repo1 (file-name-as-directory (make-temp-file "magit-annex-" t)))
         (repo2 (file-name-as-directory (make-temp-file "magit-annex-" t)))
         (process-environment process-environment))
     (push "GIT_AUTHOR_NAME=A U Thor" process-environment)
     (push "GIT_AUTHOR_EMAIL=a.u.thor@example.com" process-environment)
     (condition-case err
         (cl-letf (((symbol-function #'message) #'format))
           (let ((default-directory repo1))
             (magit-call-git "init" ".")
             (magit-call-git "annex" "init" "repo1")
             (magit-annex-tests-modify-file "file")
             (magit-stage-file "file")
             (magit-call-git "commit" "-m" "normal commit")
             (magit-call-git "remote" "add" "repo2" repo2))
           (let ((default-directory repo2))
             (magit-call-git "clone" "-o" "repo1" repo1 ".")
             (magit-call-git "annex" "init" "repo2"))
           (let ((default-directory repo1))
             ,@body))
       (error
        (message "Keeping test directories and buffers:\n  %s\n  %s"
                 repo1 repo2)
        (signal (car err) (cdr err))))
     (magit-annex-tests-kill-buffers repo1)
     (magit-annex-tests-kill-buffers repo2)
     (call-process "chmod" nil nil nil "-R" "777" repo1)
     (call-process "chmod" nil nil nil "-R" "777" repo2)
     (delete-directory repo1 t)
     (delete-directory repo2 t)))

(defun magit-annex-tests-modify-file (filename)
  (with-temp-file (expand-file-name filename)
    (insert (symbol-name (cl-gensym "content")))))

(defun magit-annex-tests-should-have-section (type info)
  (magit-status-setup-buffer default-directory)
  (message (buffer-string))
  (should (--first (equal (oref it value) info)
                   (oref (magit-get-section `((,type) (status)))
                         children))))


;;; Annexing

(ert-deftest magit-annex-add-file-to-annex ()
  (magit-annex-with-test-repo
    (magit-annex-tests-modify-file "file")
    (should (not (file-symlink-p "file")))
    (magit-annex-add "file")
    (should (file-symlink-p "file"))
    (magit-annex-tests-should-have-section 'staged "file")))

(ert-deftest magit-annex-add-all-files-to-annex ()
  (magit-annex-with-test-repo
    (magit-annex-tests-modify-file "file1")
    (magit-annex-tests-modify-file "file2")
    (should (not (file-symlink-p "file1")))
    (let ((magit-annex-add-all-confirm nil))
      (magit-annex-add-all))
    (should (file-symlink-p "file1"))
    (should (file-symlink-p "file2"))
    (magit-annex-tests-should-have-section 'staged "file1")
    (magit-annex-tests-should-have-section 'staged "file2")))


;;; Updating

(ert-deftest magit-annex-sync ()
  (magit-annex-with-test-repo-pair
    (let ((default-directory repo2))
      (magit-annex-tests-modify-file "annex-file")
      (magit-annex-add "annex-file")
      (magit-call-git "commit" "-m" "annex commit")
      (magit-annex-tests-wait
        (magit-annex-sync-all))
      (should (magit-git-lines "diff" "repo1/master"))
      (should-not (magit-git-lines "diff" "synced/master"))
      (should (magit-annex-present-files)))
    (let ((default-directory repo1))
      (magit-annex-tests-wait
        (magit-annex-merge))
      (should-not (magit-annex-present-files)))))

(ert-deftest magit-annex-sync-content ()
  (magit-annex-with-test-repo-pair
    (let ((default-directory repo2))
      (magit-annex-tests-modify-file "annex-file")
      (magit-annex-add "annex-file")
      (magit-call-git "commit" "-m" "annex commit")
      (magit-annex-tests-wait
        (magit-annex-sync-all '("--content")))
      (should (magit-git-lines "diff" "repo1/master"))
      (should-not (magit-git-lines "diff" "synced/master"))
      (should (magit-annex-present-files)))
    (let ((default-directory repo1))
      (magit-annex-tests-wait
        (magit-annex-merge))
      (should (magit-annex-present-files)))))


;;; Managing content

(ert-deftest magit-annex-get-all-auto ()
  (magit-annex-with-test-repo-pair
    (let ((default-directory repo2))
      (magit-annex-tests-modify-file "annex-file")
      (magit-annex-add "annex-file")
      (magit-call-git "commit" "-m" "annex commit")
      (magit-annex-tests-wait
        (magit-annex-sync-all)))
    (let ((default-directory repo1))
      (magit-annex-tests-wait
        (magit-annex-merge))
      (magit-annex-tests-wait
        (magit-annex-get-all-auto))
      ;; Shouldn't be present because of --auto flag.
      (should-not (magit-annex-present-files)))))

(ert-deftest magit-annex-get-files ()
  (magit-annex-with-test-repo-pair
    (let ((default-directory repo2))
      (magit-annex-tests-modify-file "annex-file")
      (magit-annex-add "annex-file")
      (magit-call-git "commit" "-m" "annex commit")
      (magit-annex-tests-wait
        (magit-annex-sync-all)))
    (let ((default-directory repo1))
      (magit-annex-tests-wait
        (magit-annex-merge))
      (should-not (magit-annex-present-files))
      (magit-annex-tests-wait
        (magit-annex-get-files '("annex-file")))
      (should (equal (magit-annex-present-files)
                     '("annex-file"))))))

(ert-deftest magit-annex-get-files-subdir ()
  (magit-annex-with-test-repo-pair
    (let ((default-directory repo2))
      (make-directory "subdir")
      (magit-annex-tests-modify-file "subdir/annex-file")
      (magit-annex-add "subdir/annex-file")
      (magit-call-git "commit" "-m" "subdir annex commit")
      (magit-annex-tests-wait
        (magit-annex-sync-all)))
    (let ((default-directory repo1))
      (magit-annex-tests-wait
        (magit-annex-merge))
      (should-not (magit-annex-present-files))
      (let ((default-directory (concat repo1 "subdir")))
        (magit-annex-tests-wait
          (magit-annex-get-files '("annex-file"))))
      (should (equal (magit-annex-present-files)
                     '("subdir/annex-file"))))))

(ert-deftest magit-annex-drop-files ()
  (magit-annex-with-test-repo-pair
    (let ((default-directory repo2))
      (magit-annex-tests-modify-file "annex-file")
      (magit-annex-add "annex-file")
      (magit-call-git "commit" "-m" "annex commit")
      (magit-annex-tests-wait
        (magit-annex-sync-all))
      (magit-annex-tests-wait
        (magit-annex-drop-files '("annex-file") '("--force")))
      (should-not (magit-annex-present-files)))))

(ert-deftest magit-annex-move-files ()
  (magit-annex-with-test-repo-pair
    (let ((default-directory repo2))
      (magit-annex-tests-modify-file "annex-file")
      (magit-annex-add "annex-file")
      (magit-call-git "commit" "-m" "annex commit")
      (magit-annex-tests-wait
        (magit-annex-sync-all))
      (magit-annex-tests-wait
        (magit-annex-move-files '("annex-file") '("--to=repo1")))
      (should-not (magit-annex-present-files)))
    (let ((default-directory repo1))
      (magit-annex-tests-wait
        (magit-annex-merge))
      (should (equal (magit-annex-present-files)
                     '("annex-file"))))))

(ert-deftest magit-annex-copy-files ()
  (magit-annex-with-test-repo-pair
    (let ((default-directory repo2))
      (magit-annex-tests-modify-file "annex-file")
      (magit-annex-add "annex-file")
      (magit-call-git "commit" "-m" "annex commit")
      (magit-annex-tests-wait
        (magit-annex-sync-all))
      (magit-annex-tests-wait
        (magit-annex-copy-files '("annex-file") '("--to=repo1")))
      (should (equal (magit-annex-present-files)
                     '("annex-file"))))
    (let ((default-directory repo1))
      (magit-annex-tests-wait
        (magit-annex-merge))
      (should (equal (magit-annex-present-files)
                     '("annex-file"))))))

(ert-deftest magit-annex-unlock-lock-files ()
  (magit-annex-with-test-repo
    (magit-annex-tests-modify-file "annex-file")
    (magit-annex-add "annex-file")
    (magit-call-git "commit" "-m" "annex commit")
    (should-not (magit-annex-unlocked-files))
    (magit-annex-unlock-files '("annex-file"))
    (should (equal (magit-annex-unlocked-files)
                   '("annex-file")))
    (magit-annex-lock-files '("annex-file") '("--force"))
    (should-not (magit-annex-unlocked-files))))

(ert-deftest magit-annex-undo-files ()
  (magit-annex-with-test-repo
    (magit-annex-tests-modify-file "annex-file")
    (magit-annex-add "annex-file")
    (magit-call-git "commit" "-m" "annex commit")
    (let* ((key-fn (lambda (x)
                     (magit-git-str "annex" "lookupkey" x)))
           (orig-key (funcall key-fn "annex-file") ))
      (magit-annex-unlock-files '("annex-file"))
      (magit-annex-tests-modify-file "annex-file")
      (magit-annex-add "annex-file")
      (magit-call-git "commit" "-m" "update file")
      (should-not (equal orig-key (funcall key-fn "annex-file")))
      (magit-annex-undo-files '("annex-file"))
      (should (equal orig-key (funcall key-fn "annex-file"))))))