aboutsummaryrefslogtreecommitdiff
path: root/tests/test-calendar-sync-no-config-startup.el
blob: 94a53009683cc823229bb9df9eeb9a0416446e98 (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
;;; test-calendar-sync-no-config-startup.el --- No-config startup tests for calendar-sync  -*- lexical-binding: t; -*-

;;; Commentary:
;; Regression tests for loading calendar-sync without configured calendars.

;;; Code:

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

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

(defvar calendar-sync-private-config-file)
(defvar calendar-sync-calendars)
(defvar calendar-sync-auto-start)

(ert-deftest test-calendar-sync-no-config-startup-does-not-start-sync ()
  "Loading calendar-sync without calendars should not start timers or fetches."
  (let ((calendar-sync-private-config-file
         (expand-file-name "missing-calendar-sync.local.el" temporary-file-directory))
        (calendar-sync-calendars nil)
        (calendar-sync-auto-start t)
        (timer-called nil)
        (process-called nil)
        messages)
    (cl-letf (((symbol-function 'run-at-time)
               (lambda (&rest _args)
                 (setq timer-called t)
                 'test-calendar-sync-timer))
              ((symbol-function 'make-process)
               (lambda (&rest _args)
                 (setq process-called t)
                 nil))
              ((symbol-function 'message)
               (lambda (format-string &rest args)
                 (push (apply #'format format-string args) messages))))
      (load (expand-file-name "modules/calendar-sync.el" user-emacs-directory)
            nil t))
    (should (boundp 'calendar-sync-calendars))
    (should-not calendar-sync-calendars)
    (should-not timer-called)
    (should-not process-called)
    (should-not (cl-some (lambda (msg)
                           (string-match-p "calendar-sync: Syncing" msg))
                         messages))))

(ert-deftest test-calendar-sync-no-config-status-reports-missing-config ()
  "Status should report missing calendar config without erroring."
  (let ((calendar-sync-private-config-file
         (expand-file-name "missing-calendar-sync.local.el" temporary-file-directory))
        (calendar-sync-calendars nil)
        (calendar-sync-auto-start t)
        messages)
    (cl-letf (((symbol-function 'run-at-time) (lambda (&rest _args) nil))
              ((symbol-function 'make-process) (lambda (&rest _args) nil))
              ((symbol-function 'message)
               (lambda (format-string &rest args)
                 (push (apply #'format format-string args) messages))))
      (load (expand-file-name "modules/calendar-sync.el" user-emacs-directory)
            nil t)
      (calendar-sync-status))
    (should (member "calendar-sync: No calendars configured (set calendar-sync-calendars)"
                    messages))))

(ert-deftest test-calendar-sync-no-config-loads-private-config-when-present ()
  "Loading calendar-sync should read an ignored private config file when present."
  (let ((config-file (make-temp-file "calendar-sync-local-" nil ".el"))
        (calendar-sync-calendars nil)
        (calendar-sync-auto-start t)
        (timer-called nil)
        (process-called nil))
    (unwind-protect
        (progn
          (with-temp-file config-file
            (insert "(setq calendar-sync-auto-start nil)\n")
            (insert "(setq calendar-sync-calendars\n")
            (insert "      '((:name \"local\" :url \"https://example.test/calendar.ics\" :file \"/tmp/local.org\")))\n"))
          (let ((calendar-sync-private-config-file config-file))
            (cl-letf (((symbol-function 'run-at-time)
                       (lambda (&rest _args)
                         (setq timer-called t)
                         'test-calendar-sync-timer))
                      ((symbol-function 'make-process)
                       (lambda (&rest _args)
                         (setq process-called t)
                         nil))
                      ((symbol-function 'message)
                       (lambda (&rest _args) nil)))
              (load (expand-file-name "modules/calendar-sync.el" user-emacs-directory)
                    nil t))
            (should (equal (calendar-sync--calendar-names) '("local")))
            (should-not calendar-sync-auto-start)
            (should-not timer-called)
            (should-not process-called)))
      (delete-file config-file))))

(ert-deftest test-calendar-sync-no-config-private-config-does-not-auto-start-in-batch ()
  "Private config should not auto-start sync while Emacs is noninteractive."
  (let ((config-file (make-temp-file "calendar-sync-local-" nil ".el"))
        (calendar-sync-calendars nil)
        (calendar-sync-auto-start t)
        (timer-called nil)
        (process-called nil))
    (unwind-protect
        (progn
          (with-temp-file config-file
            (insert "(setq calendar-sync-calendars\n")
            (insert "      '((:name \"local\" :url \"https://example.test/calendar.ics\" :file \"/tmp/local.org\")))\n"))
          (let ((calendar-sync-private-config-file config-file))
            (cl-letf (((symbol-function 'run-at-time)
                       (lambda (&rest _args)
                         (setq timer-called t)
                         'test-calendar-sync-timer))
                      ((symbol-function 'make-process)
                       (lambda (&rest _args)
                         (setq process-called t)
                         nil))
                      ((symbol-function 'message)
                       (lambda (&rest _args) nil)))
              (load (expand-file-name "modules/calendar-sync.el" user-emacs-directory)
                    nil t))
            (should (equal (calendar-sync--calendar-names) '("local")))
            (should calendar-sync-auto-start)
            (should noninteractive)
            (should-not timer-called)
            (should-not process-called)))
      (delete-file config-file))))

(provide 'test-calendar-sync-no-config-startup)
;;; test-calendar-sync-no-config-startup.el ends here