-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautobuild-test.el
122 lines (112 loc) · 4.56 KB
/
autobuild-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
(require 'autobuild)
(require 'cl-lib)
(autobuild-mode t)
(ert-deftest autobuild-test-mode-filter ()
(let (autobuild-rules-list)
(eval
'(autobuild-define-rule test-sh-mode (sh-mode)
(autobuild-nice 7)
"bash"))
(with-temp-buffer
(should (eq 1 (length autobuild-rules-list)))
(should (eq 0 (length (autobuild-applicable-rule-actions))))
(sh-mode)
(should (eq 1 (length (autobuild-applicable-rule-actions)))))))
(defun make-sh-file (identifier contents)
(with-current-buffer
(find-file-noselect
(make-temp-file (format "autobuild-test-%s-" identifier) nil ".sh"))
(insert contents)
(save-buffer)
(current-buffer)))
(ert-deftest autobuild-test-notification ()
(let* (autobuild-rules-list
finish-order
(autobuild-notification-function
(lambda (buffer state)
(add-to-list 'finish-order compile-command t)))
(autobuild-notify-threshold-secs t)
(compilation-auto-rename-buffer-fn
(lambda (&rest args)
(with-current-buffer "*compilation*"
(rename-buffer (generate-new-buffer-name "*autobuild-test-*")))))
(compilation-start-hook
(cons compilation-auto-rename-buffer-fn compilation-start-hook)))
(progn
(should (null finish-order))
(funcall autobuild-notification-function 'buffer 'state)
(should (eq 1 (length finish-order)))
(pop finish-order))
(eval
`(autobuild-define-rule test-sh-mode (sh-mode)
(autobuild-nice 7)
(format "bash %s" buffer-file-name)))
(let* ((fast (make-sh-file "fast" "sleep 1"))
(slow (make-sh-file "slow" "sleep 4"))
(first (make-sh-file "first" "true"))
(run-order (list fast slow first))
(finish-order-expected (list first fast slow)))
(cl-loop for buf in run-order do
(with-current-buffer buf
(should (eq 1 (length (autobuild-applicable-rule-actions))))
(autobuild-build nil)))
(while (< (length finish-order) (length run-order)) (sit-for 1))
(let ((expected (mapcar
(lambda (buffer)
(format "bash %s" (buffer-file-name buffer)))
finish-order-expected)))
(should (equal finish-order expected))))))
(ert-deftest autobuild-test-last-rule ()
(let (autobuild-rules-list
ran)
(eval
'(autobuild-define-rule low-nice (sh-mode)
(autobuild-nice 7)
(lambda () (setq ran 'low))))
(eval
'(autobuild-define-rule high-nice (sh-mode)
(autobuild-nice 10)
(lambda () (setq ran 'high))))
(with-temp-buffer
(sh-mode)
(should (eq 2 (length (autobuild-applicable-rule-actions))))
(should (null ran))
(should (null autobuild-last-local-invocation))
(autobuild-build nil)
(should (eq 'low ran))
(should (eq (autobuild--invocation-rule autobuild-last-local-invocation) #'low-nice))
(cl-letf (((symbol-function #'autobuild-candidate-select)
(lambda (cands _prompt _stringify-fn)
(cl-loop for action in cands
thereis (when (eq (autobuild--invocation-rule action)
#'high-nice)
action)))))
(autobuild-build t)
(should (eq 'high ran))
(should (eq (autobuild--invocation-rule autobuild-last-local-invocation) #'high-nice))
(autobuild-build nil)
(should (eq 'high ran))
(should (eq (autobuild--invocation-rule autobuild-last-local-invocation) #'high-nice))))))
(ert-deftest autobuild-test-prioritizing-rules-defined-first ()
"If rules have the same nice value, older rules should be prioritized."
(let (autobuild-rules-list rule-executed)
(eval
'(progn
(autobuild-define-rule older-rule (sh-mode)
(autobuild-nice 7)
(lambda () (setq rule-executed 'older)))
(autobuild-define-rule newer-rule (sh-mode)
(autobuild-nice 7)
(lambda () (setq rule-executed 'newer)))))
(should (eq 'newer-rule (car autobuild-rules-list)))
(with-temp-buffer
(sh-mode)
(should (eq 2 (length (autobuild-applicable-rule-actions))))
(should (eq 'older-rule
(autobuild--invocation-rule
(car (autobuild-applicable-rule-actions)))))
(autobuild-build nil)
(should (eq (autobuild--invocation-rule
autobuild-last-local-invocation)
'older-rule))
(should (eq rule-executed 'older)))))