-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.el
1483 lines (1416 loc) · 69.9 KB
/
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; test.el --- unit tests for bazel.el -*- lexical-binding: t; -*-
;; Copyright 2020, 2021, 2022, 2023 Google LLC
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; https://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; Unit tests for bazel.el.
;;; Code:
(require 'bazel)
(require 'add-log)
(require 'cl-lib)
(require 'compile)
(require 'eieio)
(require 'ert)
(require 'ert-x)
(require 'faces)
(require 'ffap)
(require 'flymake)
(require 'font-lock)
(require 'imenu)
(require 'org)
(require 'ob-shell)
(require 'project)
(require 'rx)
(require 'seq)
(require 'speedbar)
(require 'subr-x)
(require 'syntax)
(require 'url)
(require 'warnings)
(require 'which-func)
(require 'xref)
(defconst bazel-test--directory
;; https://bazel.build/reference/test-encyclopedia#initial-conditions
(substitute-in-file-name "$TEST_SRCDIR/$TEST_WORKSPACE/")
"Directory with data dependencies for this package.")
;;;; Helper macros
(eval-when-compile
(defmacro bazel-test--with-temp-directory (name org-file &rest body)
"Create a new temporary directory.
Bind the name of the directory to NAME and execute BODY while the
directory exists. Remove the directory and all its contents once
BODY finishes successfully. NAME will be a directory name, not a
directory file name; see Info node ‘(elisp) Directory Names’. If
ORG-FILE is non-nil, tangle code blocks in ORG-FILE into the
directory first using ‘bazel-test--tangle’."
(declare (indent 2) (debug (symbolp form body)))
(cl-check-type name symbol)
(let ((directory (make-symbol "directory"))
(file (make-symbol "file")))
`(let ((,directory (make-temp-file "bazel-mode-test-" :dir-flag)))
(ert-info (,directory :prefix "Temporary directory: ")
(prog2 (when-let ((,file ,org-file))
(bazel-test--tangle ,directory ,file))
(let ((,name (file-name-as-directory ,directory)))
,@body)
(delete-directory ,directory :recursive))))))
(defmacro bazel-test--with-file-buffer (filename &rest body)
"Visit FILENAME in a temporary buffer.
Execute BODY with the buffer that visits FILENAME current. Kill
that buffer once BODY finishes."
(declare (indent 1) (debug t))
(let ((previous-buffers (make-symbol "previous-buffers"))
(buffer (make-symbol "buffer")))
(macroexp-let2 nil filename filename
`(ert-info (,filename :prefix "Visited test file: ")
(let ((,previous-buffers (buffer-list)))
(access-file ,filename "Visiting test file")
(save-current-buffer
(find-file-existing ,filename)
(let ((,buffer (current-buffer)))
(unwind-protect
,(macroexp-progn body)
;; Kill the buffer only if ‘find-file-existing’ has generated
;; a new one.
(unless (memq ,buffer ,previous-buffers)
(kill-buffer ,buffer))))))))))
(defmacro bazel-test--with-buffers (&rest body)
"Evaluate BODY and kill all buffers that it created."
(declare (indent 0) (debug t))
(let ((buffers (make-symbol "buffers")))
`(let ((,buffers (buffer-list)))
(unwind-protect
,(macroexp-progn body)
(dolist (buffer (buffer-list))
(unless (memq buffer ,buffers) (kill-buffer buffer)))))))
(defmacro bazel-test--with-suppressed-warnings (warnings &rest body)
"Suppress WARNINGS in BODY.
This is the same as ‘with-suppressed-warnigns’ if available.
Otherwise, just evaluate BODY."
(declare (indent 1) (debug (sexp body)))
(if (macrop 'with-suppressed-warnings)
`(with-suppressed-warnings ,warnings ,@body)
(macroexp-progn body)))
(defmacro bazel-test--wait-for (message condition)
"Busy wait for CONDITION to become non-nil.
MESSAGE is a message for ‘ert-info’."
(declare (indent 1) (debug t))
`(ert-info (,message)
(ert-info (,(prin1-to-string condition) :prefix "Condition: ")
(with-timeout (10 (ert-fail "Timed out waiting for condition"))
(while (not ,condition)
(sleep-for 0.1)))))))
;;;; Unit tests
(ert-deftest bazel-starlark-mode/indent-after-colon ()
(with-temp-buffer
(bazel-starlark-mode)
(insert "def foo():")
(newline-and-indent)
(should (= (current-column) 4))))
(ert-deftest bazel-mode/indent-region ()
(bazel-test--with-temp-directory dir "indent.org"
(bazel-test--with-file-buffer (expand-file-name "BUILD" dir)
(should (equal (buffer-string) (ert-buffer-string-reindented))))))
(ert-deftest bazel-mode-flymake ()
"Unit test for the ‘bazel-mode-flymake’ Flymake backend."
(bazel-test--with-temp-directory dir "flymake.org"
(bazel-test--with-file-buffer (expand-file-name "buildifier.bzl" dir)
(let ((bazel-buildifier-command (expand-file-name "buildifier" dir))
(flymake-diagnostic-functions '(bazel-mode-flymake))
(warning-minimum-log-level :debug)
(diagnostics ()))
(skip-unless (file-executable-p bazel-buildifier-command))
;; Make the fake Buildifier report immediately.
(write-region "" nil (expand-file-name "signal" dir) nil nil nil 'excl)
(flymake-mode)
(should (flymake-is-running))
(should (equal (flymake-running-backends) '(bazel-mode-flymake)))
;; Wait for the backend to start reporting.
(bazel-test--wait-for "Waiting for backend to start reporting"
(memq #'bazel-mode-flymake (flymake-reporting-backends)))
;; Give the backend some time to report.
(bazel-test--wait-for "Waiting for diagnostics to arrive"
(flymake-diagnostics))
(dolist (diag (flymake-diagnostics))
(ert-info ((prin1-to-string diag))
(should (eq (flymake-diagnostic-buffer diag) (current-buffer)))
(should (eq (flymake-diagnostic-type diag) :warning))
(push (list (buffer-substring-no-properties
(flymake-diagnostic-beg diag)
(flymake-diagnostic-end diag))
(flymake-diagnostic-text diag))
diagnostics)))
(should (equal (seq-sort-by #'car #'string-lessp diagnostics)
`(("\"\"\" \"\"\""
,(concat "The docstring for the function \"foo\" "
"should start with a one-line summary. "
"[function-docstring-header] "
"(https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#function-docstring-header)"))
("1 / 2"
,(concat "The \"/\" operator for integer division "
"is deprecated in favor of \"//\". "
"[integer-division] "
"(https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#integer-division)"))
("def foo(bar):"
,(concat "The file has no module docstring. "
"[module-docstring] "
"(https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#module-docstring)")))))))))
(ert-deftest bazel-mode-flymake/source-buffer-killed ()
"Unit test for the ‘bazel-mode-flymake’ Flymake backend.
Check that the Flymake backend behaves well if the source buffer
gets killed early."
(ert-with-message-capture messages
(bazel-test--with-temp-directory dir "flymake.org"
(let ((bazel-buildifier-command (expand-file-name "buildifier" dir))
(warning-minimum-log-level :debug))
(skip-unless (file-executable-p bazel-buildifier-command))
(bazel-test--with-file-buffer (expand-file-name "buildifier.bzl" dir)
(let ((flymake-diagnostic-functions '(bazel-mode-flymake)))
(flymake-mode)
(should (flymake-is-running))
(should (equal (flymake-running-backends) '(bazel-mode-flymake))))
;; Wait for a Buildifier process to start before killing the buffer.
(bazel-test--wait-for "Waiting for Buildifier process to start"
(bazel-test--buildifier-running-p)))
;; Now allow the fake Buildifier to proceed.
(write-region "" nil (expand-file-name "signal" dir) nil nil nil 'excl)
;; Wait for fake Buildifier to finish.
(bazel-test--wait-for "Waiting for Buildifier process to finish"
(not (bazel-test--buildifier-running-p)))))
;; This shouldn’t have caused any warnings or errors.
(should-not (string-match-p (rx bol (or "Emergency" "Error" "Warning"))
messages))))
(ert-deftest bazel-mode/xref ()
"Unit test for XRef support."
(let ((definitions ()))
(bazel-test--with-temp-directory dir "xref.org"
(bazel-test--with-file-buffer (expand-file-name "root/BUILD" dir)
(let ((case-fold-search nil)
(search-spaces-regexp nil))
(forward-comment (point-max))
;; Search for all sources and dependencies. These are strings that
;; stand on their own in a line.
(while (re-search-forward (rx bol (* blank) ?\") nil t)
(let ((backend (xref-find-backend))
(root (expand-file-name "root" dir)))
(should (eq backend 'bazel-mode))
(ert-info ((format "line %d, %s" (line-number-at-pos)
(buffer-substring-no-properties
(1- (point)) (line-end-position))))
(let ((identifier (xref-backend-identifier-at-point backend)))
(should (stringp identifier))
(should (equal (expand-file-name
(get-text-property 0 'bazel-mode-workspace
identifier))
(file-name-as-directory root)))
(let* ((defs (xref-backend-definitions backend identifier))
(def (car-safe defs)))
(should (consp defs))
;; Check that ‘xref-backend-definitions’ still works if the
;; magic text properties aren’t present. This allows users
;; to invoke ‘xref-find-definitions’ and enter a target
;; manually.
(should
(equal defs
(xref-backend-definitions
backend (substring-no-properties identifier))))
;; We only expect one definition for now.
(should-not (cdr defs))
(should (xref-item-p def))
(should (equal (xref-item-summary def) identifier))
(let ((ref-file (buffer-file-name
(marker-buffer
(xref-location-marker
(xref-item-location def))))))
;; Work around https://bugs.gnu.org/46219.
(cl-callf file-name-unquote root)
(cl-callf file-name-unquote ref-file)
(push (list (substring-no-properties identifier)
(file-relative-name ref-file root))
definitions))))))))
;; Test completions.
(let ((table (xref-backend-identifier-completion-table 'bazel-mode)))
(should (equal (try-completion ":" table) ":"))
(should (equal (all-completions ":" table) '("lib" "bin" "aaa.cc")))
(should (equal (completion-boundaries ":" table nil "") '(1 . 0))))
;; Test ‘bazel-show-consuming-rule’.
(let* ((build-buffer (current-buffer))
(jumps 0)
(xref-after-jump-hook (list (lambda () (cl-incf jumps)))))
(bazel-test--with-file-buffer (expand-file-name "root/aaa.cc" dir)
(bazel-show-consuming-rule))
(should (eql jumps 1))
(should (eq (current-buffer) build-buffer))
(should (looking-at-p (rx "lib"))))))
(should (equal (nreverse definitions)
'(("//:aaa.cc" "aaa.cc")
("//:dir/bbb.cc" "dir/bbb.cc")
("//:aaa.cc" "aaa.cc")
("//:aaa.cc" "aaa.cc")
("//pkg:ccc.cc" "pkg/ccc.cc")
("@ws//pkg:ddd.cc" "bazel-root/external/ws/pkg/ddd.cc")
("//:lib" "BUILD")
("//:lib" "BUILD")
("//pkg:pkg" "pkg/BUILD")
("//pkg:lib" "pkg/BUILD"))))))
(ert-deftest bazel-mode/ffap ()
"Unit test for ‘find-file-at-point’ support."
(bazel-test--with-temp-directory dir "find-file-at-point.org"
(bazel-test--with-file-buffer (expand-file-name "root/pkg/aaa.c" dir)
(let ((case-fold-search nil)
(search-spaces-regexp nil))
(search-forward "\"" (line-end-position))
(should (equal (ffap-file-at-point)
(file-name-unquote (expand-file-name "root/aaa.h" dir))))
(forward-line)
(search-forward "\"" (line-end-position))
(forward-comment (point-max))
(should (equal (ffap-file-at-point)
(file-name-unquote
(expand-file-name "root/bazel-root/external/ws/bbb.h"
dir))))))))
(ert-deftest bazel-build-mode/fill ()
"Check that magic comments are left alone."
(with-temp-buffer
(insert-file-contents
(expand-file-name "testdata/fill.BUILD" bazel-test--directory))
(bazel-build-mode)
(let ((before (buffer-string)))
(while (search-forward "# Test paragraph" nil t)
(ert-info ((format "fill-paragraph on line %d" (line-number-at-pos)))
(fill-paragraph)
(should (equal (buffer-string) before)))))))
(ert-deftest bazel-build-mode/beginning-of-defun ()
"Check that ‘beginning-of-defun’ moves to the beginning of the rule."
(bazel-test--with-temp-directory dir "defun-navigation.org"
(bazel-test--with-file-buffer (expand-file-name "BUILD" dir)
(search-forward "bazel.el")
(beginning-of-defun)
(should (looking-at-p (rx bol "elisp_library(" ?\n
" name = \"bazel\","))))))
(ert-deftest bazel-build-mode/end-of-defun ()
"Check that ‘end-of-defun’ moves to the end of the rule."
(bazel-test--with-temp-directory dir "defun-navigation.org"
(bazel-test--with-file-buffer (expand-file-name "BUILD" dir)
(search-forward "bazel.el")
(end-of-defun)
(should (looking-back (rx "\n)\n") nil)))))
(ert-deftest bazel-mode/compile ()
"Check that \\[next-error] jumps to the correct places."
(bazel-test--with-temp-directory dir "compile.org"
(with-temp-buffer
(let* ((file nil) (line nil)
(next-error-move-function
(lambda (_from to)
(goto-char to)
(setq file buffer-file-name
line (buffer-substring-no-properties
(line-beginning-position) (line-end-position)))))
(case-fold-search nil)
(search-spaces-regexp nil)
(compilation-skip-to-next-location nil))
(insert-file-contents (expand-file-name "bazel.out" dir))
(compilation-minor-mode)
(ert-info ("Deprecation warning")
(compilation-next-error 1)
(should (looking-at-p
(rx bol "WARNING: " (+ nonl) "/package/BUILD:1:11: target "
"'//package:test' is deprecated: Deprecated!" eol)))
(save-current-buffer (compile-goto-error))
(should (equal file (file-name-unquote
(expand-file-name "package/BUILD" dir))))
(should (equal line "cc_library(")))
(ert-info ("Target failure")
(compilation-next-error 1)
(should (looking-at-p
(rx bol "ERROR: " (+ nonl) "/package/BUILD:1:11: "
"Compiling package/test.cc failed: ")))
(save-current-buffer (compile-goto-error))
(should (equal file (file-name-unquote
(expand-file-name "package/BUILD" dir))))
(should (equal line "cc_library(")))
(ert-info ("Compiler error")
(compilation-next-error 1)
(save-current-buffer (compile-goto-error))
(should (equal file (file-name-unquote
(expand-file-name "package/test.cc" dir))))
(should (equal line "UnknownType foo;")))
(ert-info ("No more errors")
(should-error (compilation-next-error 1)))))))
(ert-deftest bazel-build-mode/imenu ()
"Check that ‘imenu’ finds BUILD rules."
(bazel-test--with-temp-directory dir "xref.org"
(with-temp-buffer
(insert-file-contents (expand-file-name "root/BUILD" dir))
(bazel-build-mode)
(let ((imenu-use-markers nil))
(should (equal (funcall imenu-create-index-function)
'(("lib" . 1) ("bin" . 185)))))
(let ((imenu-use-markers t))
(should (equal (funcall imenu-create-index-function)
`(("lib" . ,(copy-marker 1))
("bin" . ,(copy-marker 185)))))))))
(ert-deftest bazel-mode/speedbar ()
"Check that \\[speedbar] detects BUILD files."
(bazel-test--with-temp-directory dir "speedbar.org"
(with-temp-buffer
(speedbar-default-directory-list dir 0)
(goto-char (point-min))
(let ((case-fold-search nil)
(search-spaces-regexp nil))
(search-forward "BUILD")))))
(ert-deftest bazel-mode/triple-quoted-strings ()
"Check that triple-quoted strings work as expected."
;; See https://bazel.build/rules/lib/string.
(with-temp-buffer
(bazel-build-mode)
(insert "\"\"\"\n\"foo\"\n\"\"\"\n")
(font-lock-flush)
(font-lock-ensure)
(goto-char (point-min))
(while (not (eobp))
(ert-info ((format "at position %d; rest of buffer is %S"
(point)
(buffer-substring-no-properties (point) (point-max))))
(cl-destructuring-bind
(depth _ _ in-string-p in-comment-p _ _ _ string-start . rest)
(syntax-ppss)
(should (eq depth 0))
(when (< 4 (point) (- (point-max) 5)) (should in-string-p))
(should-not in-comment-p)
;; The syntactic start of a triple-quoted string could be on the first
;; (Emacs 27) or the last (Emacs 28) quote,
;; cf. https://bugs.gnu.org/49518.
(should (eq (not in-string-p) (not string-start))))
(should (eq (face-at-point)
(and (< (point) (1- (point-max))) 'font-lock-string-face)))
(forward-char)))))
(ert-deftest bazel/project ()
"Test project support for Bazel workspaces."
(bazel-test--with-temp-directory dir "project.org"
(let ((project (project-current nil dir)))
(should project)
(should (bazel-workspace-p project))
(should (bazel-workspace-root project))
(should (directory-name-p (bazel-workspace-root project)))
(should (file-directory-p (bazel-workspace-root project)))
(should (file-equal-p (bazel-workspace-root project) dir))
(should (file-equal-p (project-root project) dir))
(bazel-test--with-suppressed-warnings ((obsolete project-roots))
(should (consp (project-roots project)))
(should-not (cdr (project-roots project)))
(should (file-equal-p (car (project-roots project)) dir)))
(should-not (project-external-roots project)))))
(ert-deftest bazel/project-files ()
"Test ‘project-files’ support for Bazel workspaces."
;; Try to work around https://bugs.gnu.org/48471 by picking GNU find on macOS.
(let ((find-program (if (eq system-type 'darwin) "gfind" find-program)))
(skip-unless (executable-find find-program))
(bazel-test--with-temp-directory dir "project.org"
;; Unquote to work around https://bugs.gnu.org/47799.
(let* ((dir (file-name-unquote dir))
(project (project-current nil dir))
(files (project-files project)))
(should project)
(should (bazel-workspace-p project))
(should files)
(should (equal (sort (cl-loop for file in files
collect (file-relative-name file dir))
#'string-lessp)
'(".bazelignore" "WORKSPACE" "package/BUILD")))))))
(ert-deftest bazel-test/coverage ()
"Test coverage parsing and display."
;; Set up a fake workspace and execution root. We use DIR for both.
(bazel-test--with-temp-directory dir "coverage.org"
(let* ((package-dir (expand-file-name "src/main/java/example" dir))
(library (expand-file-name "Example.java" package-dir)))
(bazel-test--with-file-buffer library
(let ((case-fold-search nil)
(search-spaces-regexp nil))
;; The coverage overlays don’t logically change the buffer contents.
;; Ensure that the code works even if the buffer is read-only.
(setq buffer-read-only t)
(with-temp-buffer
(let ((default-directory dir)
(bazel-display-coverage 'local))
(insert-file-contents (expand-file-name "bazel.out" dir))
;; Simulate successful exit of the Bazel process.
(compilation-handle-exit 'exit 0 "finished\n")))
(ert-info ("Comment line")
;; Package declaration isn’t covered at all.
(should (looking-at-p (rx bol "package ")))
(should-not (face-at-point)))
(ert-info ("Line with branching statement")
(search-forward "if (arg)")
(should (eq (face-at-point) 'bazel-covered-line))
(should (> left-margin-width 0))
(let ((before-string (get-char-property (point) 'before-string)))
(should (stringp before-string))
(pcase (get-text-property 0 'display before-string)
(`((margin left-margin) ,(and (pred stringp) margin-string))
;; FIXME: Once we drop support for Emacs 28, switch to
;; ‘equal-including-properties’.
(with-suppressed-warnings
((obsolete ert-equal-including-properties))
(should (ert-equal-including-properties
margin-string
(ert-propertized-string
'(face bazel-covered-line) "+"
'(face bazel-uncovered-line) "−")))))
(otherwise
(ert-fail (list "Unexpected ‘display’ property" otherwise))))))
(ert-info ("Covered line")
(search-forward "return 137;")
(backward-char) ; overlay doesn’t extend beyond the end of the line
(should (eq (face-at-point) 'bazel-covered-line)))
(ert-info ("Uncovered line")
(search-forward "return 42;")
(backward-char) ; overlay doesn’t extend beyond the end of the line
(should (eq (face-at-point) 'bazel-uncovered-line)))
(ert-info ("Removing coverage")
(bazel-remove-coverage-display)
;; Now there shouldn’t be any faces or margins left in the buffer.
(should (eql (next-single-char-property-change (point-min) 'face)
(point-max)))
(should (eq left-margin-width 0))))))))
(ert-deftest bazel--target-completion-table/root-package ()
"Test target completion in the root package."
(bazel-test--with-temp-directory dir "target-completion-root.org"
(make-symbolic-link dir (expand-file-name "bazel-out" dir))
;; The test cases are of the form (STRING PATTERN TRY ALL TEST BOUND).
;; STRING is the input string. PATTERN specifies whether to complete target
;; patterns; if PATTERNS is ‘*’, try both with and without pattern
;; completion. TRY, ALL, and TEST are the expected results of
;; ‘try-completion’, ‘all-completions’, and ‘test-completion’, respectively.
;; BOUND is the expected prefix completion bound returned by
;; ‘completion-bounds’.
(pcase-dolist
(`(,string ,pattern ,try ,all ,test ,bound)
'(("" nil ""
("test" "foo_test" "foo_test.py" "test.cc" "//" "@") nil 0)
("" t ""
("test" "foo_test" ":all" ":all-targets" ":*" "..." "//" "@") nil 0)
("te" nil "test" ("test" "test.cc") nil 0)
("te" t "test" ("test") nil 0)
("test" nil "test" ("test" "test.cc") t 0)
("test" t t ("test") t 0)
("test.c" nil "test.cc" ("test.cc") nil 0)
("test.c" t nil () nil 0)
("a" * nil () nil 0)
(":" t ":" ("test" "foo_test" "all" "all-targets" "*") nil 1)
(":" nil ":" ("test" "foo_test" "foo_test.py" "test.cc") nil 1)
(":te" nil ":test" ("test" "test.cc") nil 1)
(":te" t ":test" ("test") nil 1)
(":test" nil ":test" ("test" "test.cc") t 1)
(":test" t t ("test") t 1)
(":a" nil nil () nil 1)
(":a" t ":all" ("all" "all-targets") nil 1)
(":all" nil nil () nil 1)
(":all" t ":all" ("all" "all-targets") t 1)
(":all-targets" nil nil () nil 1)
(":all-targets" t t ("all-targets") t 1)
(":foo/bar" * nil () nil 1)
("package/" * nil () nil 0)
("package:" nil nil () nil 0)
("package:" t nil () nil 8)
("@" * "@//" ("//") nil 1)
("@w" * nil () nil 1)
("/" * "//" ("//") nil 0)
("//" nil "//:" (":") nil 2)
("//" t "//" (":" "...") nil 2)
("//:" nil "//:" ("test" "foo_test" "foo_test.py" "test.cc") nil 3)
("//:" t "//:" ("test" "foo_test" "all" "all-targets" "*") nil 3)
("//:te" nil "//:test" ("test" "test.cc") nil 3)
("//:te" t "//:test" ("test") nil 3)
("//:test" nil "//:test" ("test" "test.cc") t 3)
("//:test" t t ("test") t 3)
("//:a" nil nil () nil 3)
("//:a" t "//:all" ("all" "all-targets") nil 3)
("//:all" nil nil () nil 3)
("//:all" t "//:all" ("all" "all-targets") t 3)
("//:all-targets" nil nil () nil 3)
("//:all-targets" t t ("all-targets") t 3)
("//:*" nil nil () nil 3)
("//:*" t t ("*") t 3)
("//pack" * nil () nil 2)
("//package/" * nil () nil 10)
("//package:" * nil () nil 10)
("//." nil nil () nil 0)
("//." t "//..." ("...") nil 2)
("//..." nil nil () nil 0)
("//..." t "//..." ("..." "...:") t 2)
("//...:" nil nil () nil 0)
("//...:" t "//...:" ("all" "all-targets" "*") nil 6)))
(dolist (pattern (if (eq pattern '*) '(nil t) (list pattern)))
(ert-info ((if pattern "yes" "no") :prefix "Pattern completion: ")
(let* ((default-directory dir)
(table (bazel--target-completion-table pattern nil)))
(ert-info ((prin1-to-string string) :prefix "Input: ")
(should (equal (try-completion string table) try))
(should (equal (all-completions string table) all))
(should (eq (test-completion string table) test))
(should (equal (completion-boundaries string table nil "suffix")
(cons bound 6))))))))
;; The test cases are of the form (STRING ONLY-TESTS TRY ALL TEST BOUND).
;; STRING is the input string. ONLY-TESTS specifies whether to restrict the
;; search to test rules; if TEST-ONLY is ‘*’, try both with and without this
;; restriction. TRY, ALL, and TEST are the expected results of
;; ‘try-completion’, ‘all-completions’, and ‘test-completion’, respectively.
;; BOUND is the expected prefix completion bound returned by
;; ‘completion-bounds’.
(pcase-dolist
(`(,string ,only-tests ,try ,all ,test ,bound)
'(("" nil ""
("test" "foo_test" ":all" ":all-targets" ":*" "..." "//" "@") nil 0)
("" t ""
("foo_test" ":all" ":all-targets" ":*" "..." "//" "@") nil 0)
("te" nil "test" ("test") nil 0)
("te" t nil () nil 0)
("fo" * "foo_test" ("foo_test") nil 0)
("a" * nil () nil 0)
(":" nil ":" ("test" "foo_test" "all" "all-targets" "*") nil 1)
(":" t ":" ("foo_test" "all" "all-targets" "*") nil 1)
(":f" * ":foo_test" ("foo_test") nil 1)
(":te" nil ":test" ("test") nil 1)
(":te" t nil () nil 1)
(":test" nil t ("test") t 1)
(":test" t nil () nil 1)
(":a" * ":all" ("all" "all-targets") nil 1)))
(dolist (only-tests (if (eq only-tests '*) '(nil t) (list only-tests)))
(ert-info ((if only-tests "yes" "no") :prefix "Only tests: ")
(let* ((default-directory dir)
(table (bazel--target-completion-table :pattern only-tests)))
(ert-info ((prin1-to-string string) :prefix "Input: ")
(should (equal (try-completion string table) try))
(should (equal (all-completions string table) all))
(should (eq (test-completion string table) test))
(should (equal (completion-boundaries string table nil "suffix")
(cons bound 6))))))))))
(ert-deftest bazel--target-completion-table/subpackage ()
"Test target completion in a subpackage."
(bazel-test--with-temp-directory dir "target-completion-package.org"
;; The test cases are of the form (PACKAGE STRING TRY ALL TEST BOUND).
;; PACKAGE is the package name (t stands for both "" and "package"). STRING
;; is the input string. TRY, ALL, and TEST are the expected results of
;; ‘try-completion’, ‘all-completions’, and ‘test-completion’, respectively.
;; BOUND is the expected prefix completion bound returned by
;; ‘completion-bounds’.
(pcase-dolist
(`(,package ,string ,try ,all ,test ,bound)
'(("" "" "" ("package" "..." "//" "@") nil 0)
("" "test" nil () nil 0)
("" "all" nil () nil 0)
("" "all-targets" nil () nil 0)
("" "*" nil () nil 0)
("" ":" nil () nil 1)
("" ":test" nil () nil 1)
("" ":all" nil () nil 1)
("" ":*" nil () nil 1)
("" ":all-targets" nil () nil 1)
("" "pack" "package" ("package") nil 0)
("" "package" "package" ("package") t 0)
("" "package/" "package/..." ("...") nil 8)
("" "package/." "package/..." ("...") nil 8)
("" "package/..." "package/..." ("..." "...:") t 8)
("" "package/...:" "package/...:" ("all" "all-targets" "*") nil 12)
("" "package:" "package:" ("test" "all" "all-targets" "*") nil 8)
("" "package:te" "package:test" ("test") nil 8)
("" "package:test" t ("test") t 8)
("" "package:a" "package:all" ("all" "all-targets") nil 8)
("" "package:all" "package:all" ("all" "all-targets") t 8)
("" "package:all-targets" t ("all-targets") t 8)
("" "package:*" t ("*") t 8)
("package" "" ""
("test" ":all" ":all-targets" ":*" "..." "//" "@") nil 0)
("package" "te" "test" ("test") nil 0)
("package" "test" t ("test") t 0)
("package" "a" nil () nil 0)
("package" "all" nil () nil 0)
("package" ":" ":" ("test" "all" "all-targets" "*") nil 1)
("package" ":te" ":test" ("test") nil 1)
("package" ":test" t ("test") t 1)
("package" ":a" ":all" ("all" "all-targets") nil 1)
("package" ":all" ":all" ("all" "all-targets") t 1)
("package" ":all-targets" t ("all-targets") t 1)
("package" ":*" t ("*") t 1)
(t "@" "@//" ("//") nil 1)
(t "@w" nil () nil 1)
(t "/" "//" ("//") nil 0)
(t "//" "//" (":" "package" "...") nil 2)
(t "//:" nil () nil 3)
(t "//:test" nil () nil 3)
(t "//:all" nil () nil 3)
(t "//:all-targets" nil () nil 3)
(t "//:*" nil () nil 3)
(t "//." "//..." ("...") nil 2)
(t "//..." "//..." ("..." "...:") t 2)
(t "//pack" "//package" ("package") nil 2)
(t "//package" "//package" ("package") t 2)
(t "//package/" "//package/..." ("...") nil 10)
(t "//package/." "//package/..." ("...") nil 10)
(t "//package/..." "//package/..." ("..." "...:") t 10)
(t "//package/...:"
"//package/...:" ("all" "all-targets" "*") nil 14)
(t "//package:"
"//package:" ("test" "all" "all-targets" "*") nil 10)
(t "//package:te" "//package:test" ("test") nil 10)
(t "//package:test" t ("test") t 10)
(t "//package:a" "//package:all" ("all" "all-targets") nil 10)
(t "//package:all" "//package:all" ("all" "all-targets") t 10)
(t "//package:all-targets" t ("all-targets") t 10)
(t "//package:*" t ("*") t 10)))
(ert-info ((prin1-to-string string) :prefix "Input: ")
(let ((packages (cond ((stringp package) (list package))
((eq package t) '("" "package")))))
(should (consp packages))
(dolist (package packages)
(ert-info ((prin1-to-string package) :prefix "Package: ")
(let* ((default-directory (file-name-as-directory
(expand-file-name package dir)))
(table (bazel--target-completion-table :pattern nil)))
(should (equal (try-completion string table) try))
(should (equal (all-completions string table) all))
(should (eq (test-completion string table) test))
(should (equal (completion-boundaries string table nil "suffix")
(cons bound 6)))))))))))
(ert-deftest bazel--target-completion-table/workspace ()
"Test workspace name completion."
(bazel-test--with-temp-directory dir "target-completion-workspace.org"
(make-symbolic-link dir (expand-file-name "bazel-out" dir))
;; The test cases are of the form (STRING TRY ALL TEST BOUND). STRING is
;; the input string. TRY, ALL, and TEST are the expected results of
;; ‘try-completion’, ‘all-completions’, and ‘test-completion’, respectively.
;; BOUND is the expected prefix completion bound returned by
;; ‘completion-bounds’.
(pcase-dolist
(`(,string ,try ,all ,test ,bound)
'(("@" "@" ("ext" "//") nil 1)
("@/" "@//" ("//") nil 1)
("@//" "@//" (":" "main-pkg" "...") nil 3)
("@//:" "@//:" ("all" "all-targets" "*") nil 4)
("@//:all" "@//:all" ("all" "all-targets") t 4)
("@//." "@//..." ("...") nil 3)
("@//..." "@//..." ("..." "...:") t 3)
("@//...:*" t ("*") t 7)
("@//m" "@//main-pkg" ("main-pkg") nil 3)
("@//main-pkg" "@//main-pkg" ("main-pkg") t 3)
("@e" "@ext" ("ext") nil 1)
("@ext" t ("ext" "ext//") t 1)
("@ext/" "@ext//" ("//") nil 4)
("@ext//" "@ext//" (":" "ext-pkg" "...") nil 6)
("@ext//:" "@ext//:" ("all" "all-targets" "*") nil 7)
("@ext//:a" "@ext//:all" ("all" "all-targets") nil 7)
("@ext//:all" "@ext//:all" ("all" "all-targets") t 7)
("@ext//." "@ext//..." ("...") nil 6)
("@ext//..." "@ext//..." ("..." "...:") t 6)
("@ext//...:*" t ("*") t 10)
("@ext//e" "@ext//ext-pkg" ("ext-pkg") nil 6)
("@ext//ext-pkg" "@ext//ext-pkg" ("ext-pkg") t 6)
("@ext//ext-pkg/" "@ext//ext-pkg/..." ("...") nil 14)
("@ext//ext-pkg:" "@ext//ext-pkg:" ("all" "all-targets" "*") nil 14)
("@q" nil () nil 1)
("@qux" nil () nil 1)
("@qux//:" nil () nil 7)
("@qux//p" nil () nil 6)))
(ert-info ((prin1-to-string string) :prefix "Input: ")
(let* ((default-directory (expand-file-name "main/" dir))
(table (bazel--target-completion-table :pattern nil)))
(should (equal (try-completion string table) try))
(should (equal (all-completions string table) all))
(should (eq (test-completion string table) test))
(should (equal (completion-boundaries string table nil "suffix")
(cons bound 6))))))))
(ert-deftest bazel-mode/which-function ()
"Verify ‘which-function’ and ‘add-log-current-defun’ in ‘bazel-mode’."
(bazel-test--with-temp-directory dir "which-function.org"
(pcase-dolist (`(,file . ,cases)
'(("BUILD"
("name = \"lib\"" "lib")
("aaa.cc" "lib")
("name = \"bin\"" "bin")
("//pkg:lib" "bin"))
("defs.bzl"
("# Comment" nil)
(" name = " nil)
("in_foo = 1" "foo")
;; For inner functions the result depends on the Emacs
;; version.
("in_inner = 1" "foo.inner" "inner")
("in_foo = 2" "foo"))))
(bazel-test--with-file-buffer (expand-file-name file dir)
(let ((case-fold-search nil)
(search-spaces-regexp nil))
(pcase-dolist (`(,search-string . ,expected-names) cases)
(ert-info ((format "Search string: %s" search-string))
(goto-char (point-min))
(search-forward search-string)
(let ((begin (match-beginning 0))
(end (match-end 0)))
(pcase-dolist (`(,symbol ,position)
`((begin ,begin)
(end ,end)
(middle ,(/ (+ begin end) 2))))
(ert-info ((format "Location: %s" symbol))
(goto-char position)
(should (member (add-log-current-defun) expected-names))
(should (member (which-function) expected-names))))))))))))
(ert-deftest bazel-build ()
(cl-letf* ((commands ())
((symbol-function #'compile)
(lambda (command &optional _comint)
(push command commands))))
(bazel-build "//foo:bar")
(should (equal commands '("bazel build -- //foo\\:bar")))))
(ert-deftest bazel-run/options ()
(cl-letf* ((commands ())
((symbol-function #'compile)
(lambda (command &optional _comint)
(push command commands)))
(bazel-command-options '("--tool_tag=emacs")))
(bazel-run "//foo:bar")
(should (equal commands '("bazel run --tool_tag\\=emacs -- //foo\\:bar")))))
(ert-deftest bazel-build-mode/font-lock ()
"Test Font Locking in ‘bazel-build-mode’."
(let ((text (ert-propertized-string
'(face font-lock-comment-delimiter-face) "# "
'(face font-lock-comment-face) "comment\n" nil "\n"
'(face font-lock-keyword-face) "load" nil "("
'(face font-lock-string-face) "\"foo.bzl\"" nil ", "
'(face font-lock-string-face) "\"\"\"foo\"\"\"" nil ")\n\n"
nil "cc_library(\n"
nil " name = " '(face font-lock-string-face) "\""
'(face (font-lock-variable-name-face font-lock-string-face))
"foo"
'(face font-lock-string-face) "\"" nil ",\n"
nil " "
'(face font-lock-comment-delimiter-face) "# "
'(face (font-lock-preprocessor-face font-lock-comment-face))
"keep sorted" '(face font-lock-comment-face) "\n"
nil " srcs = "
'(face font-lock-builtin-face) "glob" nil "(["
'(face font-lock-string-face) "\"*.cc\"" nil "]),\n"
nil " alwayslink = "
'(face font-lock-constant-face) "True" nil ",\n"
nil " linkstatic="
'(face font-lock-constant-face) "True" nil ",\n"
nil")\n\n"
nil "some_rule(name = "
'(face font-lock-string-face) "\"foo\"" nil " + SUFFIX)\n\n"
nil "name_only(name = " '(face font-lock-string-face) "\""
'(face (font-lock-variable-name-face font-lock-string-face))
"foo"
'(face font-lock-string-face) "\"" nil ")\n\n"
nil "some_rule(\n"
nil " filename = "
'(face font-lock-string-face) "\"file.txt\"" nil ",\n"
nil ")\n\n")))
(with-temp-buffer
(bazel-build-mode)
(insert (substring-no-properties text))
(font-lock-flush)
(font-lock-ensure)
;; Emacs adds a ‘syntax-table’ text property, which we don’t care about.
(remove-list-of-text-properties (point-min) (point-max) '(syntax-table))
;; FIXME: Once we drop support for Emacs 28, switch to
;; ‘equal-including-properties’.
(with-suppressed-warnings ((obsolete ert-equal-including-properties))
(should (ert-equal-including-properties (buffer-string) text))))))
(ert-deftest bazel-compile-current-file ()
"Test for ‘bazel-compile-current-file’."
(bazel-test--with-temp-directory dir nil
(write-region "" nil (expand-file-name "WORKSPACE" dir))
(write-region "" nil (expand-file-name "BUILD" dir))
(make-directory (expand-file-name "package" dir))
(write-region "" nil (expand-file-name "package/BUILD" dir))
(write-region "" nil (expand-file-name "package/test.cc" dir))
(bazel-test--with-file-buffer (expand-file-name "package/test.cc" dir)
(pcase-dolist
(`(,default-directory ,want-commands)
`((,dir ("bazel build --compile_one_dependency -- package/test.cc"))
(,(expand-file-name "package" dir)
("bazel build --compile_one_dependency -- test.cc"))))
(cl-letf* ((got-commands ())
((symbol-function #'compile)
(lambda (command &optional _comint)
(push command got-commands))))
(bazel-compile-current-file)
(should (equal got-commands want-commands)))))))
(ert-deftest bazel-completion-at-point ()
"Test for ‘completion-at-point’ in ‘bazel-mode’."
(bazel-test--with-temp-directory dir "completion-at-point.org"
(bazel-test--with-file-buffer (expand-file-name "BUILD" dir)
(let* ((case-fold-search nil)
(search-spaces-regexp nil)
(got-args ())
(completion-in-region-function
(lambda (start end collection predicate)
(push (list start end collection predicate) got-args))))
(ert-info ("outside of a label")
(goto-char (point-min))
(completion-at-point)
(should-not got-args))
(ert-info ("within a label")
(re-search-forward
(rx bol (+ blank) "deps = [\"" (group "//:l") (group "i") "\"]"))
(let ((want-begin (match-beginning 1))
(want-end (match-end 2))
(point (match-beginning 2))
(string (match-string-no-properties 1))
(suffix (match-string-no-properties 2)))
(goto-char point)
(completion-at-point)
(pcase got-args
(`((,begin ,end ,collection nil))
(should (integer-or-marker-p begin))
(should (integer-or-marker-p end))
(should (= begin want-begin))
(should (= end want-end))
(should (equal (try-completion string collection) "//:lib"))
(should (equal (all-completions string collection) '("lib")))
(should (equal
(completion-boundaries string collection nil suffix)
'(3 . 1))))
(_ (ert-fail (format "Unexpected arguments %S" got-args))))))))))
(ert-deftest bazel-test-at-point ()
(bazel-test--with-temp-directory dir "test-at-point-elisp.org"
(bazel-test--with-file-buffer (expand-file-name "foo.el" dir)
(let ((case-fold-search nil)
(search-spaces-regexp nil))
(re-search-forward (rx bol "(ert-deftest foo/not-really-a-test ()"))
;; Point is on a test case, but the consuming rule is not a test rule.
(should-error (bazel-test-at-point) :type 'user-error)))
(bazel-test--with-file-buffer (expand-file-name "foo-test.el" dir)
(cl-letf* ((case-fold-search nil)
(search-spaces-regexp nil)
(commands ())
((symbol-function #'compile)
(lambda (command &optional _comint)
(push command commands))))
(should-error (bazel-test-at-point) :type 'user-error)
(re-search-forward (rx bol "(ert-deftest foo/test ()"))
(bazel-test-at-point)
(should
(equal commands
'("bazel test --test_filter\\=foo/test -- //\\:foo_test")))))))
(ert-deftest bazel-test-at-point/python-mode ()
"Test ‘bazel-test-at-point’ in ‘python-mode’."
(bazel-test--with-temp-directory dir "test-at-point-python.org"
(bazel-test--with-file-buffer (expand-file-name "py_test.py" dir)
(cl-letf* ((case-fold-search nil)
(search-spaces-regexp nil)
(commands ())
((symbol-function #'compile)
(lambda (command &optional _comint)
(push command commands))))
(should-error (bazel-test-at-point) :type 'user-error)
(search-forward "# Test case class")
(bazel-test-at-point)
(search-forward "# Test method")
(bazel-test-at-point)
(should
(equal (reverse commands)
'("bazel test --test_filter\\=MyTest -- //\\:py_test"
"bazel test --test_filter\\=MyTest.testFoo -- //\\:py_test")))))))
(ert-deftest bazel-test-at-point/c++-mode ()
"Test ‘bazel-test-at-point’ in ‘c++-mode’."
(bazel-test--with-temp-directory dir "test-at-point-c++.org"
(bazel-test--with-file-buffer (expand-file-name "cc_test.cc" dir)
(cl-letf* ((case-fold-search nil)
(search-spaces-regexp nil)
(commands ())
((symbol-function #'compile)
(lambda (command &optional _comint)
(push command commands))))
(should-error (bazel-test-at-point) :type 'user-error)
(search-forward "EXPECT_EQ(1, 2)")
(bazel-test-at-point)
(search-forward "EXPECT_EQ(4, 5)")
(bazel-test-at-point)
(should
(equal (reverse commands)
'("bazel test --test_filter\\=FooTest.Bar -- //\\:cc_test"
"bazel test --test_filter\\=BazTest.Qux -- //\\:cc_test")))))))
(ert-deftest bazel-test-at-point/go-mode ()
"Test ‘bazel-test-at-point’ in ‘go-mode’."
(bazel-test--with-temp-directory dir "test-at-point-go.org"
(bazel-test--with-file-buffer (expand-file-name "go_test.go" dir)
(cl-letf* ((case-fold-search nil)
(search-spaces-regexp nil)
(commands ())
((symbol-function #'compile)
(lambda (command &optional _comint)
(push command commands))))
(unless (derived-mode-p 'go-mode)
;; ‘go-mode’ might not be installed or available. Simulate it as
;; best as possible.
(setq-local beginning-of-defun-function
(lambda (&optional arg)