aboutsummaryrefslogtreecommitdiff
path: root/tests/test-pearl-coverage-summary.el
blob: 79de30a287cefa6ea19664a8e8673f682c3e804d (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
;;; test-pearl-coverage-summary.el --- Tests for the coverage summary script -*- lexical-binding: t; -*-

;; Copyright (C) 2026 Craig Jennings

;; Author: Craig Jennings <c@cjennings.net>

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Unit tests for scripts/coverage-summary.el — the batch helper behind
;; `make coverage-summary'.  Drives the SimpleCov parsers, the per-file
;; record builder, and the formatted text on synthetic reports written to
;; temp files (no committed fixtures).  Covers the normal percentage path,
;; boundary cases (fully covered, no executable lines, multi-key union), and
;; error cases (missing file, malformed JSON, a tracked source absent from the
;; report counting as 0%).

;;; Code:

(require 'ert)
(load (expand-file-name "../scripts/coverage-summary.el") nil t)

(defconst test-pearl-cov-root "/tmp/pearl-cov-test/"
  "Synthetic project root used to anchor source-file paths in fixtures.")

(defun test-pearl-cov--write (json)
  "Write JSON to a temp file and return its path."
  (let ((f (make-temp-file "pearl-cov" nil ".json")))
    (with-temp-file f (insert json))
    f))

(defun test-pearl-cov--report (hits-array)
  "Write a SimpleCov report mapping pearl.el under the test root to HITS-ARRAY.
HITS-ARRAY is a JSON array literal string such as \"[null, 1, 0]\"."
  (test-pearl-cov--write
   (format "{\"undercover\": {\"coverage\": {\"%spearl.el\": %s}}}"
           test-pearl-cov-root hits-array)))

(defmacro test-pearl-cov--with (json-or-path varname &rest body)
  "Bind VARNAME to a report path from JSON-OR-PATH, run BODY, then delete it."
  (declare (indent 2))
  `(let ((,varname ,json-or-path))
     (unwind-protect (progn ,@body)
       (when (file-exists-p ,varname) (delete-file ,varname)))))

;;; --- Parsers ---

(ert-deftest test-pearl-coverage-parse-counts-only-hit-lines ()
  "`parse-simplecov' records only lines whose hit count is greater than zero."
  (test-pearl-cov--with (test-pearl-cov--report "[null, 1, 0, 3, null, 0]") f
    (let ((tbl (pearl-coverage--parse-simplecov f))
          (key (expand-file-name "pearl.el" test-pearl-cov-root)))
      ;; lines 2 and 4 are hits; 3 and 6 are executable-but-unhit; 1,5 null
      (should (= 2 (pearl-coverage--line-count tbl key))))))

(ert-deftest test-pearl-coverage-executable-counts-all-numeric-lines ()
  "`executable-lines' records every numeric entry, hit or not."
  (test-pearl-cov--with (test-pearl-cov--report "[null, 1, 0, 3, null, 0]") f
    (let ((tbl (pearl-coverage--executable-lines f))
          (key (expand-file-name "pearl.el" test-pearl-cov-root)))
      ;; lines 2,3,4,6 are numeric; 1 and 5 are null
      (should (= 4 (pearl-coverage--line-count tbl key))))))

(ert-deftest test-pearl-coverage-parse-unions-across-test-keys ()
  "Coverage under multiple top-level keys is unioned (undercover :merge-report)."
  (test-pearl-cov--with
      (test-pearl-cov--write
       (format (concat "{\"a\": {\"coverage\": {\"%spearl.el\": [1, 0, null]}},"
                       " \"b\": {\"coverage\": {\"%spearl.el\": [0, 1, null]}}}")
               test-pearl-cov-root test-pearl-cov-root))
      f
    (let ((tbl (pearl-coverage--parse-simplecov f))
          (key (expand-file-name "pearl.el" test-pearl-cov-root)))
      ;; line 1 hit in a, line 2 hit in b -> union is 2 covered lines
      (should (= 2 (pearl-coverage--line-count tbl key))))))

;;; --- Records ---

(ert-deftest test-pearl-coverage-record-normal-percentage ()
  "A half-covered file reports covered/total and a 50% figure."
  (test-pearl-cov--with (test-pearl-cov--report "[null, 1, 0, 3, null, 0]") f
    (let ((r (car (pearl-coverage--records f '("pearl.el") test-pearl-cov-root))))
      (should (plist-get r :present))
      (should (= 2 (plist-get r :covered)))
      (should (= 4 (plist-get r :total)))
      (should (< 49.9 (plist-get r :percent) 50.1)))))

(ert-deftest test-pearl-coverage-record-fully-covered-is-100 ()
  "Every executable line hit reports 100%."
  (test-pearl-cov--with (test-pearl-cov--report "[1, 2, 3]") f
    (let ((r (car (pearl-coverage--records f '("pearl.el") test-pearl-cov-root))))
      (should (= 3 (plist-get r :covered)))
      (should (= 3 (plist-get r :total)))
      (should (< 99.9 (plist-get r :percent) 100.1)))))

(ert-deftest test-pearl-coverage-record-no-executable-lines-is-100 ()
  "A present file with no executable lines is 100% — nothing left uncovered."
  (test-pearl-cov--with (test-pearl-cov--report "[null, null]") f
    (let ((r (car (pearl-coverage--records f '("pearl.el") test-pearl-cov-root))))
      (should (plist-get r :present))
      (should (= 0 (plist-get r :total)))
      (should (< 99.9 (plist-get r :percent) 100.1)))))

(ert-deftest test-pearl-coverage-record-missing-source-counts-as-zero ()
  "A tracked source absent from the report is flagged missing and counts as 0%."
  (test-pearl-cov--with
      (test-pearl-cov--write
       (format "{\"u\": {\"coverage\": {\"%sother.el\": [1, 2]}}}"
               test-pearl-cov-root))
      f
    (let ((r (car (pearl-coverage--records f '("pearl.el") test-pearl-cov-root))))
      (should-not (plist-get r :present))
      (should (= 0 (plist-get r :covered)))
      (should (= 0.0 (plist-get r :percent))))))

;;; --- Error handling ---

(ert-deftest test-pearl-coverage-missing-report-errors ()
  "Parsing a nonexistent report signals a user-error."
  (should-error (pearl-coverage--parse-simplecov "/nonexistent/pearl-cov.json")
                :type 'user-error))

(ert-deftest test-pearl-coverage-malformed-json-errors ()
  "Malformed JSON signals a user-error rather than a raw json error."
  (test-pearl-cov--with (test-pearl-cov--write "{ this is not json") f
    (should-error (pearl-coverage--parse-simplecov f) :type 'user-error)))

;;; --- Formatted text ---

(ert-deftest test-pearl-coverage-summary-text-reports-both-numbers ()
  "The summary names the file, a line-coverage figure, and source coverage."
  (test-pearl-cov--with (test-pearl-cov--report "[null, 1, 0, 3, null, 0]") f
    (let ((txt (pearl-coverage-summary-text f '("pearl.el") test-pearl-cov-root)))
      (should (string-match-p "pearl\\.el" txt))
      (should (string-match-p "Line coverage" txt))
      (should (string-match-p "Source coverage" txt))
      (should (string-match-p "50\\.0%" txt)))))

(ert-deftest test-pearl-coverage-summary-text-lists-missing-source ()
  "The summary's missing section names a tracked source absent from the report."
  (test-pearl-cov--with
      (test-pearl-cov--write
       (format "{\"u\": {\"coverage\": {\"%sother.el\": [1, 2]}}}"
               test-pearl-cov-root))
      f
    (let ((txt (pearl-coverage-summary-text f '("pearl.el") test-pearl-cov-root)))
      (should (string-match-p "Not in coverage report" txt))
      (should (string-match-p "pearl\\.el" txt)))))

(provide 'test-pearl-coverage-summary)
;;; test-pearl-coverage-summary.el ends here