aboutsummaryrefslogtreecommitdiff
path: root/tests/test-coverage-core--changed-lines.el
blob: 0662594b4dc94cf6ef40e5f6ac74672ab8567618 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
;;; test-coverage-core--changed-lines.el --- Tests for cj/--coverage-changed-lines -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for:
;;   `cj/--coverage-parse-diff-output' (pure parser over git-diff text)
;;   `cj/--coverage-changed-lines' (scope → hash table, invokes git by argv)
;;
;; The parser takes the output of `git diff --unified=0' and returns
;; a hash table of file → set of changed (added) line numbers in the
;; new version.  Hunk headers have the form:
;;   @@ -<old_start>[,<old_count>] +<new_start>[,<new_count>] @@
;; Changed lines are new_start through new_start + new_count - 1.

;;; Code:

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

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'coverage-core)

;;; Fixtures

(defconst test-coverage-diff--simple-single-file
  "diff --git a/foo.el b/foo.el
index abc..def 100644
--- a/foo.el
+++ b/foo.el
@@ -10,1 +10,3 @@
-old line
+new line 1
+new line 2
+new line 3
"
  "Single-file diff with one hunk adding three lines at line 10.")

(defconst test-coverage-diff--multiple-files
  "diff --git a/a.el b/a.el
--- a/a.el
+++ b/a.el
@@ -1,0 +1,2 @@
+line 1
+line 2
diff --git a/b.el b/b.el
--- a/b.el
+++ b/b.el
@@ -5,0 +6,1 @@
+new line
"
  "Two-file diff.")

(defconst test-coverage-diff--new-file
  "diff --git a/new.el b/new.el
new file mode 100644
index 0000000..abc
--- /dev/null
+++ b/new.el
@@ -0,0 +1,3 @@
+line 1
+line 2
+line 3
"
  "New file: @@ -0,0 +1,3 @@ — three lines added to a brand-new file.")

(defconst test-coverage-diff--deletion-only
  "diff --git a/gone.el b/gone.el
--- a/gone.el
+++ b/gone.el
@@ -1,3 +1,0 @@
-removed 1
-removed 2
-removed 3
"
  "Deletion-only hunk: no new lines, file should map to an empty set.")

(defconst test-coverage-diff--binary-marker
  "diff --git a/image.png b/image.png
Binary files a/image.png and b/image.png differ
"
  "Binary file marker: no parseable hunks.")

(defconst test-coverage-diff--single-line-no-count
  "diff --git a/foo.el b/foo.el
--- a/foo.el
+++ b/foo.el
@@ -5 +5 @@
-old
+new
"
  "Hunk without a count means a single line (count defaults to 1).")

;;; Normal cases — parser

(ert-deftest test-coverage-parse-diff-single-hunk-three-lines ()
  "Normal: one hunk adding three lines gives {10, 11, 12}."
  (let* ((result (cj/--coverage-parse-diff-output
                  test-coverage-diff--simple-single-file))
         (lines (gethash "foo.el" result)))
    (should (= 1 (hash-table-count result)))
    (should (= 3 (hash-table-count lines)))
    (should (gethash 10 lines))
    (should (gethash 11 lines))
    (should (gethash 12 lines))))

(ert-deftest test-coverage-parse-diff-multiple-files ()
  "Normal: two files parsed separately with their own line sets."
  (let* ((result (cj/--coverage-parse-diff-output
                  test-coverage-diff--multiple-files))
         (a-lines (gethash "a.el" result))
         (b-lines (gethash "b.el" result)))
    (should (= 2 (hash-table-count result)))
    (should (= 2 (hash-table-count a-lines)))
    (should (gethash 1 a-lines))
    (should (gethash 2 a-lines))
    (should (= 1 (hash-table-count b-lines)))
    (should (gethash 6 b-lines))))

;;; Boundary cases — parser

(ert-deftest test-coverage-parse-diff-new-file ()
  "Boundary: new file hunk @@ -0,0 +1,3 @@ yields lines 1-3."
  (let* ((result (cj/--coverage-parse-diff-output
                  test-coverage-diff--new-file))
         (lines (gethash "new.el" result)))
    (should (= 3 (hash-table-count lines)))
    (should (gethash 1 lines))
    (should (gethash 2 lines))
    (should (gethash 3 lines))))

(ert-deftest test-coverage-parse-diff-deletion-only ()
  "Boundary: deletion-only hunk (+1,0) records the file with an empty line set."
  (let* ((result (cj/--coverage-parse-diff-output
                  test-coverage-diff--deletion-only))
         (lines (gethash "gone.el" result)))
    (should (hash-table-p lines))
    (should (= 0 (hash-table-count lines)))))

(ert-deftest test-coverage-parse-diff-binary-file-ignored ()
  "Boundary: binary files have no hunks; parser doesn't crash."
  (let ((result (cj/--coverage-parse-diff-output
                 test-coverage-diff--binary-marker)))
    (should (hash-table-p result))
    (should (= 0 (hash-table-count result)))))

(ert-deftest test-coverage-parse-diff-single-line-no-count ()
  "Boundary: @@ -5 +5 @@ means one line at line 5 (count defaults to 1)."
  (let* ((result (cj/--coverage-parse-diff-output
                  test-coverage-diff--single-line-no-count))
         (lines (gethash "foo.el" result)))
    (should (= 1 (hash-table-count lines)))
    (should (gethash 5 lines))))

(ert-deftest test-coverage-parse-diff-empty-input ()
  "Boundary: empty string input returns an empty hash table, not nil."
  (let ((result (cj/--coverage-parse-diff-output "")))
    (should (hash-table-p result))
    (should (= 0 (hash-table-count result)))))

;;; Error cases — parser

(ert-deftest test-coverage-parse-diff-malformed-hunk-header-skipped ()
  "Error: a malformed @@ line is skipped; surrounding valid hunks still parse."
  (let* ((input (concat "diff --git a/foo.el b/foo.el\n"
                        "--- a/foo.el\n"
                        "+++ b/foo.el\n"
                        "@@ this is not a valid hunk header @@\n"
                        "@@ -1,0 +10,2 @@\n"
                        "+ok1\n"
                        "+ok2\n"))
         (result (cj/--coverage-parse-diff-output input))
         (lines (gethash "foo.el" result)))
    (should (= 2 (hash-table-count lines)))
    (should (gethash 10 lines))
    (should (gethash 11 lines))))

;;; Smoke tests — changed-lines (stubbed git invocation)

(ert-deftest test-coverage-changed-lines-working-tree-stubbed ()
  "Smoke: working-tree scope invokes git diff via argv and parses the result."
  (let (seen-calls)
    (cl-letf (((symbol-function 'process-file)
               (lambda (program _infile destination _display &rest args)
                 (push (cons program args) seen-calls)
                 (with-current-buffer destination
                   (insert test-coverage-diff--simple-single-file))
                 0)))
      (let* ((result (cj/--coverage-changed-lines 'working-tree))
             (lines (gethash "foo.el" result)))
        (should (equal (nreverse seen-calls)
                       '(("git" "diff" "HEAD" "--unified=0"))))
        (should (= 1 (hash-table-count result)))
        (should (= 3 (hash-table-count lines)))))))

(ert-deftest test-coverage-changed-lines-branch-vs-parent-computes-merge-base ()
  "Branch scopes should compute merge-base separately before diffing."
  (let (seen-calls)
    (cl-letf (((symbol-function 'process-file)
               (lambda (program _infile destination _display &rest args)
                 (push (cons program args) seen-calls)
                 (with-current-buffer destination
                   (insert
                    (pcase args
                      (`("merge-base" "HEAD" "feature/base") "abc123\n")
                      (`("diff" "abc123..HEAD" "--unified=0")
                       test-coverage-diff--simple-single-file)
                      (_ ""))))
                 0)))
      (let* ((result (cj/--coverage-changed-lines 'branch-vs-parent "feature/base"))
             (lines (gethash "foo.el" result)))
        (should (equal (nreverse seen-calls)
                       '(("git" "merge-base" "HEAD" "feature/base")
                         ("git" "diff" "abc123..HEAD" "--unified=0"))))
        (should (= 3 (hash-table-count lines)))))))

(ert-deftest test-coverage-changed-lines-git-failure-errors-clearly ()
  "Git failures should surface as user-error messages."
  (cl-letf (((symbol-function 'process-file)
             (lambda (_program _infile destination _display &rest _args)
               (with-current-buffer destination
                 (insert "fatal: not a git repository\n"))
               128)))
    (should-error (cj/--coverage-changed-lines 'working-tree)
                  :type 'user-error)))

(ert-deftest test-coverage-changed-lines-unknown-scope-errors ()
  "Error: an unknown scope symbol signals user-error."
  (should-error (cj/--coverage-changed-lines 'bogus-scope)
                :type 'user-error))

;;; Boundary cases — parser, /dev/null and orphan hunks

(ert-deftest test-coverage-parse-diff-dev-null-resets-current-file ()
  "Boundary: a \"+++ /dev/null\" target resets state so a following hunk is
not misattributed to the previous file."
  (let* ((input (concat "diff --git a/keep.el b/keep.el\n"
                        "--- a/keep.el\n"
                        "+++ b/keep.el\n"
                        "@@ -1,0 +1,2 @@\n"
                        "+k1\n+k2\n"
                        "diff --git a/gone.el b/gone.el\n"
                        "--- a/gone.el\n"
                        "+++ /dev/null\n"
                        "@@ -1,0 +5,2 @@\n"
                        "+orphan1\n+orphan2\n"))
         (result (cj/--coverage-parse-diff-output input))
         (keep (gethash "keep.el" result)))
    (should (= 1 (hash-table-count result)))   ; gone.el never recorded
    (should (= 2 (hash-table-count keep)))
    (should (gethash 1 keep))
    (should (gethash 2 keep))
    (should-not (gethash 5 keep))              ; not misattributed
    (should-not (gethash 6 keep))))

(ert-deftest test-coverage-parse-diff-hunk-before-any-file-marker ()
  "Boundary: a hunk header before any file marker is ignored, not crashed on."
  (let* ((input (concat "@@ -1,0 +1,2 @@\n"
                        "+orphan1\n+orphan2\n"
                        "diff --git a/real.el b/real.el\n"
                        "--- a/real.el\n"
                        "+++ b/real.el\n"
                        "@@ -1,0 +1,1 @@\n"
                        "+r1\n"))
         (result (cj/--coverage-parse-diff-output input))
         (real (gethash "real.el" result)))
    (should (= 1 (hash-table-count result)))
    (should (= 1 (hash-table-count real)))
    (should (gethash 1 real))))

;;; merge-base (stubbed git invocation)

(ert-deftest test-coverage-git-merge-base-returns-trimmed-sha ()
  "Normal: a SHA with trailing newline is trimmed and returned."
  (cl-letf (((symbol-function 'process-file)
             (lambda (_program _infile destination _display &rest _args)
               (with-current-buffer destination (insert "abc123\n"))
               0)))
    (should (equal (cj/--coverage-git-merge-base "main") "abc123"))))

(ert-deftest test-coverage-git-merge-base-empty-output-errors ()
  "Error: empty merge-base output signals user-error (no common commit)."
  (cl-letf (((symbol-function 'process-file)
             (lambda (_program _infile destination _display &rest _args)
               (with-current-buffer destination (insert ""))
               0)))
    (should-error (cj/--coverage-git-merge-base "main") :type 'user-error)))

(ert-deftest test-coverage-git-merge-base-whitespace-output-errors ()
  "Error: whitespace-only output trims to empty and signals user-error."
  (cl-letf (((symbol-function 'process-file)
             (lambda (_program _infile destination _display &rest _args)
               (with-current-buffer destination (insert "   \n"))
               0)))
    (should-error (cj/--coverage-git-merge-base "main") :type 'user-error)))

;;; changed-lines — remaining scopes (stubbed git invocation)

(ert-deftest test-coverage-changed-lines-staged-stubbed ()
  "Normal: staged scope invokes git diff --cached via argv."
  (let (seen-calls)
    (cl-letf (((symbol-function 'process-file)
               (lambda (program _infile destination _display &rest args)
                 (push (cons program args) seen-calls)
                 (with-current-buffer destination
                   (insert test-coverage-diff--simple-single-file))
                 0)))
      (let ((result (cj/--coverage-changed-lines 'staged)))
        (should (equal (nreverse seen-calls)
                       '(("git" "diff" "--cached" "--unified=0"))))
        (should (= 3 (hash-table-count (gethash "foo.el" result))))))))

(ert-deftest test-coverage-changed-lines-branch-vs-main-stubbed ()
  "Normal: branch-vs-main computes merge-base against main, then diffs."
  (let (seen-calls)
    (cl-letf (((symbol-function 'process-file)
               (lambda (program _infile destination _display &rest args)
                 (push (cons program args) seen-calls)
                 (with-current-buffer destination
                   (insert
                    (pcase args
                      (`("merge-base" "HEAD" "main") "abc123\n")
                      (`("diff" "abc123..HEAD" "--unified=0")
                       test-coverage-diff--simple-single-file)
                      (_ ""))))
                 0)))
      (let ((result (cj/--coverage-changed-lines 'branch-vs-main)))
        (should (equal (nreverse seen-calls)
                       '(("git" "merge-base" "HEAD" "main")
                         ("git" "diff" "abc123..HEAD" "--unified=0"))))
        (should (= 3 (hash-table-count (gethash "foo.el" result))))))))

(provide 'test-coverage-core--changed-lines)
;;; test-coverage-core--changed-lines.el ends here