Skip to content

Commit 0990f57

Browse files
committed
Add more tests
A couple of unit test but mostly integration tests that depends on the rg binary.
1 parent 60575d7 commit 0990f57

11 files changed

+243
-3
lines changed

.ert-runner

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-L .

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ before_install:
1010
- curl -fsSkL https://raw.github.com/cask/cask/master/go | python
1111
- export PATH=$HOME/.cask/bin:$PATH
1212
- cask
13+
14+
# ripgrep
15+
- cargo install ripgrep
16+
- export PATH=$HOME/.cargo/bin:$PATH
1317
env:
1418
- EVM_EMACS=emacs-24.2-travis
1519
- EVM_EMACS=emacs-24.3-travis

Cask

+2
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55

66
(development
77
(depends-on "cl-lib" "0.5")
8+
(depends-on "ert-runner")
9+
(depends-on "noflet")
810
(depends-on "s"))

Makefile

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ PKG_NAME = $(shell cask info | head -1 | cut -f2 -d" ")
44
PKG_VERSION = $(shell cask version)
55
PKG = $(PKG_NAME)-$(PKG_VERSION).el
66

7-
test: build-test package-test
7+
all: install test
8+
9+
test: build-test package-test ert-test
810

911
build-test:
1012
cask clean-elc
@@ -15,7 +17,16 @@ package-test:
1517
cask package
1618
cask eval "(package-install-file \"dist/$(PKG)\")"
1719

20+
unit-test:
21+
cask exec ert-runner --pattern rg-unit
22+
23+
integration-test:
24+
cask exec ert-runner --pattern rg-integration
25+
26+
ert-test:
27+
cask exec ert-runner
28+
1829
install:
1930
cask install
2031

21-
.PHONY: build-test package-test test install
32+
.PHONY: test build-test package-test unit-test integration-test ert-test install

rg.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ This function is called from `compilation-filter-hook'."
196196
grep-error-screen-columns)
197197
(add-hook 'compilation-filter-hook 'rg-filter nil t))
198198

199-
(defun rg-expand-template (template &optional regexp files dir excl)
199+
(defun rg-expand-template (template regexp &optional files dir excl)
200200
"Patch rg TEMPLATE string replacing <C>, <D>, <F>, <R>, and <X>."
201201
(when (string-match "<C>" template)
202202
(setq template

test/data/bar.baz

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
Hello
3+
hellO

test/data/bar.el

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
Hello
3+
hellO

test/data/foo.baz

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
Hello
3+
hellO

test/data/foo.el

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
Hello
3+
hellO

test/rg.el-test.el

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
;;; rg.el-test.el --- rg.el: Tests
2+
3+
;; Copyright (C) 2017 David Landell <[email protected]>
4+
;;
5+
;; Author: David Landell <[email protected]>
6+
;; Homepage: https://github.com/davja/rg.el
7+
8+
;; This file is not part of GNU Emacs.
9+
10+
;; This program is free software; you can redistribute it and/or
11+
;; modify it under the terms of the GNU General Public License
12+
;; as published by the Free Software Foundation; either version 3
13+
;; of the License, or (at your option) any later version.
14+
15+
;; This program is distributed in the hope that it will be useful,
16+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
;; GNU General Public License for more details.
19+
20+
;; You should have received a copy of the GNU General Public License
21+
;; along with this program; if not, write to the Free Software
22+
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23+
;; 02110-1301, USA.
24+
25+
;;; Commentary:
26+
27+
;;; Code:
28+
29+
30+
;; Unit tests
31+
32+
(ert-deftest rg-unit/test-case-expand-template ()
33+
"Test that rg-expand-template handles case settings correctly."
34+
(let ((template "<F> <C> <R>"))
35+
(let ((case-fold-search t))
36+
(should (s-matches? (regexp-anywhere "-i") (rg-expand-template template "foo")))
37+
(should-not (s-matches? (regexp-anywhere "-i") (rg-expand-template template "fOo"))))
38+
(let ((case-fold-search nil))
39+
(should-not (s-matches? (regexp-anywhere "-i") (rg-expand-template template "foo")))
40+
(should-not (s-matches? (regexp-anywhere "-i") (rg-expand-template template "Foo"))))))
41+
42+
(ert-deftest rg-unit/test-build-template ()
43+
"Test that rg-build-template is correct."
44+
(let* ((rg-command "rg")
45+
(rg-custom-type-aliases nil)
46+
(notype-template (rg-build-template))
47+
(type-template (rg-build-template t))
48+
(custom-template (rg-build-template t "glob")))
49+
(should (s-matches? (regexp-anywhere-but-last "<C>") notype-template))
50+
(should (s-matches? (regexp-last "<R>") notype-template))
51+
(should-not (s-matches? (regexp-anywhere-but-last "--type <F>") notype-template))
52+
(should-not (s-matches? (regexp-anywhere-but-last "--type-add 'custom:") notype-template))
53+
54+
(should (s-matches? (regexp-anywhere-but-last "--type <F>") type-template))
55+
(should-not (s-matches? (regexp-anywhere-but-last "--type-add 'custom:") type-template))
56+
57+
(should (s-matches? (regexp-anywhere-but-last "--type-add 'custom: *glob'") custom-template))
58+
(should (s-matches? (regexp-anywhere-but-last "--type <F>") custom-template))))
59+
60+
61+
;; Integration tests
62+
63+
(ert-deftest rg-integration/test-read-files-default-alias () :tags '(need-rg)
64+
"Test that rg-read-files detects the current file and selects matching alias."
65+
(let (prompt)
66+
(noflet ((completing-read (pr &rest args) (setq prompt pr)))
67+
(find-file "test/data/foo.el")
68+
(rg-read-files "foo")
69+
(should (s-matches? "\[elisp\]" prompt))
70+
(find-file "test/data/foo.baz")
71+
(rg-read-files "foo")
72+
(should-not (s-matches? "\[.*\]" prompt))
73+
(let ((rg-custom-type-aliases '(("test" . "*.baz"))))
74+
(find-file "test/data/foo.baz")
75+
(rg-read-files "foo")
76+
(should (s-matches? "\[test\]" prompt))))))
77+
78+
(ert-deftest rg-integration/test-search-alias-builtin () :tags '(need-rg)
79+
"Test that rg builtin aliases works."
80+
(let ((case-fold-search t))
81+
(rg "hello" "elisp" (concat default-directory "test/data"))
82+
(with-current-buffer "*rg*"
83+
(should (rg-wait-for-search-result))
84+
(let ((bufstr (buffer-substring-no-properties (point-min) (point-max))))
85+
(should (= 3 (s-count-matches "foo.el.*hello" bufstr)))
86+
(should (= 3 (s-count-matches "bar.el.*hello" bufstr)))
87+
(should (= 0 (s-count-matches "foo.baz.*hello" bufstr)))
88+
(should (= 0 (s-count-matches "bar.baz.*hello" bufstr)))))))
89+
90+
(ert-deftest rg-integration/test-search-alias-custom () :tags '(need-rg)
91+
"Test that aliases defined in rg-custom-type-aliases works if explicitly selected."
92+
(let ((case-fold-search t)
93+
(rg-custom-type-aliases '(("test" . "*.baz"))))
94+
(rg "hello" "test" (concat default-directory "test/data"))
95+
(with-current-buffer "*rg*"
96+
(should (rg-wait-for-search-result))
97+
(let ((bufstr (buffer-substring-no-properties (point-min) (point-max))))
98+
(should (= 0 (s-count-matches "foo.el.*hello" bufstr)))
99+
(should (= 0 (s-count-matches "bar.el.*hello" bufstr)))
100+
(should (= 3 (s-count-matches "foo.baz.*hello" bufstr)))
101+
(should (= 3 (s-count-matches "bar.baz.*hello" bufstr)))))))
102+
103+
(ert-deftest rg-integration/test-search-alias-all-custom () :tags '(need-rg)
104+
"Test that aliases defined in rg-custom-type-aliases works if
105+
implicitly selected via '--type all'."
106+
(let ((case-fold-search t)
107+
(rg-custom-type-aliases '(("test" . "*.baz"))))
108+
(rg "hello" "all" (concat default-directory "test/data"))
109+
(with-current-buffer "*rg*"
110+
(should (rg-wait-for-search-result))
111+
(let ((bufstr (buffer-substring-no-properties (point-min) (point-max))))
112+
(should (= 3 (s-count-matches "foo.el.*hello" bufstr)))
113+
(should (= 3 (s-count-matches "bar.el.*hello" bufstr)))
114+
(should (= 3 (s-count-matches "foo.baz.*hello" bufstr)))
115+
(should (= 3 (s-count-matches "bar.baz.*hello" bufstr)))))))
116+
117+
(ert-deftest rg-integration/test-search-no-alias() :tags '(need-rg)
118+
"Test that custom file pattern that is not an alias works."
119+
(let ((case-fold-search t))
120+
(rg "hello" "*.baz" (concat default-directory "test/data"))
121+
(with-current-buffer "*rg*"
122+
(should (rg-wait-for-search-result))
123+
(let ((bufstr (buffer-substring-no-properties (point-min) (point-max))))
124+
(should (= 0 (s-count-matches "foo.el.*hello" bufstr)))
125+
(should (= 0 (s-count-matches "bar.el.*hello" bufstr)))
126+
(should (= 3 (s-count-matches "foo.baz.*hello" bufstr)))
127+
(should (= 3 (s-count-matches "bar.baz.*hello" bufstr)))))))
128+
129+
(ert-deftest rg-integration/test-search-uppercase-regexp () :tags '(need-rg)
130+
"Test that uppercase search triggers case sensitive search."
131+
(let ((case-fold-search t))
132+
(rg "Hello" "all" (concat default-directory "test/data"))
133+
(with-current-buffer "*rg*"
134+
(should (rg-wait-for-search-result))
135+
(let ((bufstr (buffer-substring-no-properties (point-min) (point-max))))
136+
(should (= 1 (s-count-matches "foo.el.*hello" bufstr)))
137+
(should (= 1 (s-count-matches "bar.el.*hello" bufstr)))))))
138+
139+
(ert-deftest rg-integration/test-search-case-sensitive-regexp ()
140+
:tags '(need-rg)
141+
"Test explicit case sensitive search."
142+
(let ((case-fold-search nil))
143+
(rg "hello" "all" (concat default-directory "test/data")))
144+
(with-current-buffer "*rg*"
145+
(should (rg-wait-for-search-result))
146+
(let ((case-fold-search t)
147+
(bufstr (buffer-substring-no-properties (point-min) (point-max))))
148+
(should (= 1 (s-count-matches "foo.el.*hello" bufstr)))
149+
(should (= 1 (s-count-matches "bar.el.*hello" bufstr))))))
150+
151+
(provide 'rg.el-test)
152+
153+
;;; rg.el-test.el ends here

test/test-helper.el

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
;;; test-helper.el --- rg.el: Helper for tests
2+
3+
;; Copyright (C) 2017 David Landell <[email protected]>
4+
;;
5+
;; Author: David Landell <[email protected]>
6+
;; Homepage: https://github.com/davja/rg.el
7+
8+
;; This file is not part of GNU Emacs.
9+
10+
;; This program is free software; you can redistribute it and/or
11+
;; modify it under the terms of the GNU General Public License
12+
;; as published by the Free Software Foundation; either version 3
13+
;; of the License, or (at your option) any later version.
14+
15+
;; This program is distributed in the hope that it will be useful,
16+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
;; GNU General Public License for more details.
19+
20+
;; You should have received a copy of the GNU General Public License
21+
;; along with this program; if not, write to the Free Software
22+
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23+
;; 02110-1301, USA.
24+
25+
;;; Commentary:
26+
27+
;;; Code:
28+
29+
(require 'ert)
30+
(require 'noflet)
31+
(require 'rg)
32+
(require 's)
33+
34+
(defun regexp-anywhere (needle)
35+
(s-replace "%%%%" needle "\\( \\|^\\)%%%%\\( \\|$\\)"))
36+
37+
(defun regexp-last (needle)
38+
(s-replace "%%%%" needle "\\( \\|^\\)%%%%$"))
39+
40+
(defun regexp-anywhere-but-last (needle)
41+
(s-replace "%%%%" needle "\\( \\|^\\)%%%% "))
42+
43+
(defun rg-wait-for-search-result ()
44+
"Wait for the rg search to finish and returns non nil if the search
45+
was successful. Timeout is 10 s."
46+
(let (search-finished)
47+
(add-hook 'compilation-finish-functions
48+
(lambda (buffer msg) (setq search-finished msg))
49+
t t)
50+
(with-timeout (10 nil)
51+
(while (not search-finished)
52+
(accept-process-output nil 0.1)))
53+
(equal (s-trim search-finished) "finished")))
54+
55+
(provide 'test-helper)
56+
57+
;;; test-helper.el ends here

0 commit comments

Comments
 (0)