-
Notifications
You must be signed in to change notification settings - Fork 12
/
daemons-test.el
147 lines (126 loc) · 5.13 KB
/
daemons-test.el
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
(require 'ert)
(load-file "./daemons.el")
(ert-deftest daemons-run-test ()
(let* (;; mock the shell command function to just echo into buffer
(daemons--shell-command-fun (lambda (cmd ins) (insert cmd)))
(expected "COMMAND=foo"))
;; mock up a submodule
(daemons-define-submodule daemons-run-test
"Test: run submodule"
:test t
:commands '((status . (lambda (name) (format "COMMAND=%s" name))))
:headers [("Daemon name" 100 t)]
:list '("foo" "bar"))
;; add the submodule to the list
(add-to-list 'daemons-init-system-submodules 'daemons-run-test)
(should (equal expected
(with-temp-buffer
(daemons--run 'status "foo")
(buffer-string))))))
(ert-deftest daemons-define-submodule-test ()
"A correct definition should populate `daemons--init-system-submodules-alist'."
(let ((expected (list :docstring "Test: happy path submodule"
:test (lambda () t)
:commands nil
:list (lambda () nil)
:headers (lambda () []))))
(daemons-define-submodule daemons-happy
"Test: happy path submodule"
:test t
:commands nil
:headers []
:list nil)
(should (equal expected
(alist-get 'daemons-happy
daemons--init-system-submodules-alist)))))
(ert-deftest daemons--test-submodule-test ()
(daemons-define-submodule daemons-positive-test
"Test: positive test submodule"
:test t
:commands nil
:headers []
:list nil)
(daemons-define-submodule daemons-negative-test
"Test: negative test submodule"
:test nil
:commands nil
:headers []
:list nil)
(should (daemons--test-submodule 'daemons-positive-test))
(should (not (daemons--test-submodule 'daemons-negative-test))))
(ert-deftest daemons--commands-alist-test ()
(let ((expected '((stop . (lambda () "plz stop"))
(start . (lambda () "let's go already")))))
(daemons-define-submodule daemons-commands-test
"Test: commands submodule"
:test t
:commands expected
:headers []
:list nil)
(should (equal expected (daemons--commands-alist 'daemons-commands-test)))))
(ert-deftest daemons--command-test ()
(let ((expected (lambda () "let's go already")))
(daemons-define-submodule daemons-command-test
"Test: commands submodule"
:test t
:commands '((stop . (lambda () "plz stop"))
(start . (lambda () "let's go already")))
:headers []
:list nil)
(should (equal expected (daemons--command 'start 'daemons-command-test)))))
(ert-deftest daemons--list-headers-test ()
(let ((expected [("Daemon name" 80 t)
("Status" 20 t)]))
(daemons-define-submodule daemons-list-headers-test
"Test: list headers submodule"
:test t
:commands nil
:headers expected
:list nil)
(should (equal expected (daemons--list-headers 'daemons-list-headers-test)))))
(ert-deftest daemons--list-test ()
(let ((expected '("foo" "bar" "baz")))
(daemons-define-submodule daemons-list-test
"Test: list submodule"
:test t
:commands nil
:headers []
:list expected)
(should (equal expected (daemons--list 'daemons-list-test)))))
;; system-specific tests
(dolist (test-suite (directory-files "." t "daemons-.*-test\.el$"))
(load-file test-suite))
;; helper function tests
(ert-deftest daemons--split-lines-test ()
(should (equal '("this" "is" "a" "test") (daemons--split-lines "this
is
a
test
"))))
(ert-deftest daemons--get-user-and-hostname-test-local ()
(should (equal (format "%s@%s" (user-login-name) (system-name))
(daemons--get-user-and-hostname "/tmp/file"))))
(ert-deftest daemons--get-user-and-hostname-test-sudo ()
"Case where user is using tramp to access local system as root."
(should (equal (format "root@%s" (system-name))
(daemons--get-user-and-hostname "/sudo::"))))
(ert-deftest daemons--get-user-and-hostname-test-remote ()
"Case where user is using tramp to access a remote system."
(should (equal "me@example.com"
(daemons--get-user-and-hostname "/ssh:me@example.com:/etc/issue"))))
(ert-deftest daemons--get-user-and-hostname-test-remote-no-user ()
"Case where user can omit username because it's in their ssh config is same as current."
(should (equal "example.com"
(daemons--get-user-and-hostname "/ssh:example.com:/etc/issue"))))
(ert-deftest daemons--get-user-and-hostname-test-remote-no-host ()
"Case where user is ssh-ing into own system as different user."
(should (equal (format "me@%s" (system-name))
(daemons--get-user-and-hostname "/ssh:me@:/etc/issue"))))
(ert-deftest daemons--get-user-and-hostname-test-remote-no-user-or-host ()
"Case where user is ssh-ing into own system."
(should (equal (system-name)
(daemons--get-user-and-hostname "/ssh::/etc/issue"))))
(ert t)
;; Test teardown - we added a dummy submodule
(setq daemons-init-system-submodules
(remove 'daemons-run-test daemons-init-system-submodules))