aboutsummaryrefslogtreecommitdiff
path: root/tests/test-dirvish-config-print.el
blob: ab6d073f0c4cd47bdf6ebf895a188cba0bbd4c0c (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
;;; test-dirvish-config-print.el --- Tests for the dirvish print command -*- lexical-binding: t; -*-

;;; Commentary:
;; `cj/dirvish-print-file' (bound to `P' in `dirvish-mode-map') sends the
;; file at point to the default printer via CUPS (`lp', falling back to
;; `lpr').  It refuses directories and file types outside
;; `cj/dirvish-print-extensions'.  These tests cover the pure predicates
;; (`cj/--printable-file-p', `cj/--print-program') directly and the command
;; with `dired-get-filename', `y-or-n-p', and `call-process' mocked.

;;; Code:

(require 'ert)
(require 'package)

(setq package-user-dir (expand-file-name "elpa" user-emacs-directory))
(package-initialize)
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(add-to-list 'load-path (expand-file-name "elpa/dirvish-2.3.0/extensions"
                                          user-emacs-directory))
(require 'user-constants)
(require 'keybindings)
(require 'dirvish-config)

;;; --------------------------- cj/--printable-file-p --------------------------

(ert-deftest test-dirvish-print-printable-file-p-known-extensions ()
  "Normal: PDF, text, and org files are printable."
  (should (cj/--printable-file-p "/tmp/report.pdf"))
  (should (cj/--printable-file-p "/tmp/notes.txt"))
  (should (cj/--printable-file-p "/tmp/agenda.org")))

(ert-deftest test-dirvish-print-printable-file-p-case-insensitive ()
  "Boundary: extension matching ignores case."
  (should (cj/--printable-file-p "/tmp/REPORT.PDF"))
  (should (cj/--printable-file-p "/tmp/Notes.Txt")))

(ert-deftest test-dirvish-print-printable-file-p-no-extension ()
  "Boundary: a file with no extension is not printable."
  (should-not (cj/--printable-file-p "/tmp/README"))
  (should-not (cj/--printable-file-p "/home/cj/.bashrc")))

(ert-deftest test-dirvish-print-printable-file-p-non-printable-extension ()
  "Error: a video or archive is not printable."
  (should-not (cj/--printable-file-p "/tmp/clip.mp4"))
  (should-not (cj/--printable-file-p "/tmp/archive.tar.gz")))

;;; ----------------------------- cj/--print-program ---------------------------

(ert-deftest test-dirvish-print-program-prefers-lp ()
  "Normal: `lp' is used when available."
  (cl-letf (((symbol-function 'executable-find)
             (lambda (cmd) (when (equal cmd "lp") "/usr/bin/lp"))))
    (should (equal (cj/--print-program) "/usr/bin/lp"))))

(ert-deftest test-dirvish-print-program-falls-back-to-lpr ()
  "Boundary: `lpr' is used when `lp' is missing."
  (cl-letf (((symbol-function 'executable-find)
             (lambda (cmd) (when (equal cmd "lpr") "/usr/bin/lpr"))))
    (should (equal (cj/--print-program) "/usr/bin/lpr"))))

(ert-deftest test-dirvish-print-program-none-available ()
  "Error: nil when neither `lp' nor `lpr' is on PATH."
  (cl-letf (((symbol-function 'executable-find) (lambda (_cmd) nil)))
    (should-not (cj/--print-program))))

;;; ---------------------------- cj/dirvish-print-file -------------------------

(defmacro test-dirvish-print--with-file-at-point (file &rest body)
  "Run BODY with `dired-get-filename' returning FILE."
  (declare (indent 1))
  `(cl-letf (((symbol-function 'dired-get-filename) (lambda (&rest _) ,file)))
     ,@body))

(ert-deftest test-dirvish-print-file-sends-printable-file-to-printer ()
  "Normal: confirming the prompt runs the print program on the file."
  (let (called)
    (test-dirvish-print--with-file-at-point "/tmp/report.pdf"
      (cl-letf (((symbol-function 'cj/--print-program) (lambda () "/usr/bin/lp"))
                ((symbol-function 'y-or-n-p) (lambda (&rest _) t))
                ((symbol-function 'call-process)
                 (lambda (program _infile _dest _display &rest args)
                   (setq called (cons program args))
                   0))
                ((symbol-function 'message) (lambda (&rest _) nil)))
        (cj/dirvish-print-file)
        (should (equal called '("/usr/bin/lp" "/tmp/report.pdf")))))))

(ert-deftest test-dirvish-print-file-declining-the-prompt-prints-nothing ()
  "Boundary: answering no to the prompt does not invoke the print program."
  (let (called)
    (test-dirvish-print--with-file-at-point "/tmp/report.pdf"
      (cl-letf (((symbol-function 'cj/--print-program) (lambda () "/usr/bin/lp"))
                ((symbol-function 'y-or-n-p) (lambda (&rest _) nil))
                ((symbol-function 'call-process)
                 (lambda (&rest _) (setq called t) 0)))
        (cj/dirvish-print-file)
        (should-not called)))))

(ert-deftest test-dirvish-print-file-errors-without-file ()
  "Error: no file at point fails clearly."
  (test-dirvish-print--with-file-at-point nil
    (should-error (cj/dirvish-print-file) :type 'user-error)))

(ert-deftest test-dirvish-print-file-errors-on-directory ()
  "Error: a directory is not printable."
  (test-dirvish-print--with-file-at-point temporary-file-directory
    (should-error (cj/dirvish-print-file) :type 'user-error)))

(ert-deftest test-dirvish-print-file-errors-on-non-printable-type ()
  "Error: a non-printable file type is refused before any prompt."
  (test-dirvish-print--with-file-at-point "/tmp/clip.mp4"
    (should-error (cj/dirvish-print-file) :type 'user-error)))

(ert-deftest test-dirvish-print-file-errors-without-print-program ()
  "Error: no `lp'/`lpr' on PATH fails clearly."
  (test-dirvish-print--with-file-at-point "/tmp/report.pdf"
    (cl-letf (((symbol-function 'cj/--print-program) (lambda () nil)))
      (should-error (cj/dirvish-print-file) :type 'user-error))))

(ert-deftest test-dirvish-print-file-errors-when-print-command-fails ()
  "Error: a non-zero print exit code surfaces as a `user-error'."
  (test-dirvish-print--with-file-at-point "/tmp/report.pdf"
    (cl-letf (((symbol-function 'cj/--print-program) (lambda () "/usr/bin/lp"))
              ((symbol-function 'y-or-n-p) (lambda (&rest _) t))
              ((symbol-function 'call-process) (lambda (&rest _) 1)))
      (should-error (cj/dirvish-print-file) :type 'user-error))))

(ert-deftest test-dirvish-print-file-bound-to-uppercase-p ()
  "Normal: `P' in `dirvish-mode-map' runs the print command."
  (should (eq (keymap-lookup dirvish-mode-map "P") #'cj/dirvish-print-file)))

(provide 'test-dirvish-config-print)
;;; test-dirvish-config-print.el ends here