aboutsummaryrefslogtreecommitdiff
path: root/tests/test-coverage-core--command.el
blob: 274938c89b3850c9fa879833804e16fdbbe4a5bc (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
;;; test-coverage-core--command.el --- Tests for cj/coverage-report and scope helpers -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for:
;;   `cj/--coverage-scope-from-label' (pure label → symbol lookup)
;;   `cj/--coverage-label-from-scope' (pure symbol → label lookup)
;;   `cj/coverage-report' (one smoke test with stubbed git + prepared report)

;;; Code:

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

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

;;; Scope label <-> symbol (pure lookups)

(ert-deftest test-coverage-scope-from-label-known ()
  "Normal: each registered label maps to its scope symbol."
  (should (eq 'working-tree
			  (cj/--coverage-scope-from-label
			   "Working tree — all uncommitted changes")))
  (should (eq 'staged
			  (cj/--coverage-scope-from-label "Staged — about to commit")))
  (should (eq 'branch-vs-parent
			  (cj/--coverage-scope-from-label "Branch vs parent")))
  (should (eq 'branch-vs-main
			  (cj/--coverage-scope-from-label "Branch vs main"))))

(ert-deftest test-coverage-scope-from-label-unknown ()
  "Boundary: unknown label returns nil."
  (should-not (cj/--coverage-scope-from-label "bogus label"))
  (should-not (cj/--coverage-scope-from-label "")))

(ert-deftest test-coverage-label-from-scope-roundtrip ()
  "Normal: symbol → label → symbol is an identity."
  (dolist (sym '(working-tree staged branch-vs-parent branch-vs-main))
	(should (eq sym (cj/--coverage-scope-from-label
					 (cj/--coverage-label-from-scope sym))))))

;;; Smoke test for the interactive command

(ert-deftest test-coverage-report-smoke-happy-path ()
  "Smoke: cj/coverage-report with stubbed backend, scope, and git-diff
populates the *Coverage Report* buffer with the expected summary and
uncovered-line markers."
  (let* ((tmp-root (make-temp-file "test-coverage-smoke-" t))
		 (report-file (expand-file-name "simplecov.json" tmp-root))
		 (simplecov (concat "{\"run\":{\"timestamp\":1,\"coverage\":"
							"{\"modules/foo.el\":[null,1,0,1,0]}}}"))
		 (diff-output (concat "diff --git a/modules/foo.el b/modules/foo.el\n"
							  "--- a/modules/foo.el\n"
							  "+++ b/modules/foo.el\n"
							  "@@ -1,0 +2,4 @@\n"
							  "+added 1\n+added 2\n+added 3\n+added 4\n"))
		 (test-backend
		  (list :name 'test-backend
				:detect (lambda (_) t)
				:run (lambda (cb) (funcall cb report-file))
				:report-path (lambda (&rest _) report-file)))
		 (cj/coverage-backends nil))
	(unwind-protect
		(progn
		  (with-temp-file report-file (insert simplecov))
		  (cj/coverage-register-backend test-backend)
		  (cl-letf (((symbol-function 'completing-read)
					 (lambda (&rest _) "Staged — about to commit"))
					((symbol-function 'process-file)
					 (lambda (_program _infile destination _display &rest _args)
					   (with-current-buffer destination
						 (insert diff-output))
					   0))
					((symbol-function 'cj/--coverage-project-root)
					 (lambda () tmp-root))
					((symbol-function 'display-buffer) #'identity))
			(call-interactively #'cj/coverage-report))
		  (with-current-buffer "*Coverage Report*"
			(let ((text (buffer-substring-no-properties (point-min) (point-max))))
			  ;; Scope label appears
			  (should (string-match-p "Staged" text))
			  ;; Lines 2 and 4 hit, lines 3 and 5 uncovered (per simplecov)
			  ;; Diff covers lines 2-5
			  ;; So the summary should show 2 of 4 covered
			  (should (string-match-p "2 of 4" text))
			  ;; Uncovered line markers use compilation-friendly format
			  (should (string-match-p "modules/foo\\.el:[35]: uncovered" text)))))
	  (when (get-buffer "*Coverage Report*")
		(kill-buffer "*Coverage Report*"))
	  (delete-directory tmp-root t))))

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