Skip to content

Commit 69195f5

Browse files
committed
Add run all tests feature
1 parent e63e203 commit 69195f5

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 1.10.5
4+
* Add `lsp-dart-run-all-tests` command.
5+
36
## 1.10.0
47
* Support for debugging Dart/Flutter tests.
58
* Add "Debug" code lens on tests.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Besides the `lsp-mode` features, `lsp-dart` implements the [custom methods featu
6868

6969
### Run/Debug tests
7070

71+
`lsp-dart-run-all-tests` - Run all tests from project.
72+
7173
`lsp-dart-run-test-file` - Run all tests from current test buffer.
7274

7375
`lsp-dart-run-test-at-point` - Run single test at point.

lsp-dart-test-support.el

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@
5151
(or (string= kind "UNIT_TEST_TEST")
5252
(string= kind "UNIT_TEST_GROUP")))
5353

54-
(defun lsp-dart-test--flutter-test-file-p (buffer)
55-
"Return non-nil if the BUFFER appears to be a flutter test file."
56-
(with-current-buffer buffer
57-
(save-excursion
58-
(goto-char (point-min))
59-
(re-search-forward "^import 'package:flutter_test/flutter_test.dart';"
60-
nil t))))
61-
6254
(defun lsp-dart-test--last-index-of (regex str &optional ignore-case)
6355
"Find the last index of a REGEX in a string STR.
6456
IGNORE-CASE is a optional arg to ignore the case sensitive on regex search."
@@ -78,11 +70,9 @@ IGNORE-CASE is a optional arg to ignore the case sensitive on regex search."
7870
,@body)
7971
(error "Dart or Flutter project not found (pubspec.yaml not found)"))))
8072

81-
(defun lsp-dart-test--build-command (buffer)
82-
"Build the dart or flutter build command.
83-
If the given BUFFER is a flutter test file, return the flutter command
84-
otherwise the dart command."
85-
(if (lsp-dart-test--flutter-test-file-p buffer)
73+
(defun lsp-dart-test--build-command ()
74+
"Build the dart or flutter build command checking project type."
75+
(if (lsp-dart--flutter-project-p)
8676
(lsp-dart-flutter-command)
8777
(concat (lsp-dart-pub-command) " run")))
8878

@@ -105,28 +95,33 @@ otherwise the dart command."
10595
escaped-str nil t)))
10696
escaped-str))
10797

108-
(defun lsp-dart-test--run (test)
98+
(defun lsp-dart-test--run (&optional test)
10999
"Run Dart/Flutter test command in a compilation buffer.
110-
If TEST is non nil, it will run only this test."
100+
If TEST is nil, it will run all tests from project.
101+
If TEST is non nil, it will check if contains any test specific name
102+
to run otherwise run all tests from file-name in TEST."
111103
(lsp-dart-test--from-project-root
112-
(let* ((file-name (lsp-dart-test-file-name test))
113-
(buffer (get-file-buffer file-name))
114-
(names (lsp-dart-test-names test))
115-
(kind (lsp-dart-test-kind test))
116-
(test-file (file-relative-name file-name
117-
(lsp-dart-get-project-root)))
118-
(test-name (lsp-dart-test--build-test-name names))
119-
(group-kind? (string= kind "UNIT_TEST_GROUP"))
120-
(test-arg (when test-name
121-
(concat "--name '^"
122-
(lsp-dart-test--escape-test-name test-name)
123-
(if group-kind? "'" "$'")))))
124-
(when names
125-
(lsp-workspace-set-metadata "last-ran-test" test))
126-
(compilation-start (format "%s test %s %s"
127-
(lsp-dart-test--build-command buffer)
128-
(or test-arg "")
129-
test-file)
104+
(if test
105+
(let* ((file-name (lsp-dart-test-file-name test))
106+
(names (lsp-dart-test-names test))
107+
(kind (lsp-dart-test-kind test))
108+
(test-file (file-relative-name file-name
109+
(lsp-dart-get-project-root)))
110+
(test-name (lsp-dart-test--build-test-name names))
111+
(group-kind? (string= kind "UNIT_TEST_GROUP"))
112+
(test-arg (when test-name
113+
(concat "--name '^"
114+
(lsp-dart-test--escape-test-name test-name)
115+
(if group-kind? "'" "$'")))))
116+
(when names
117+
(lsp-workspace-set-metadata "last-ran-test" test))
118+
(compilation-start (format "%s test %s %s"
119+
(lsp-dart-test--build-command)
120+
(or test-arg "")
121+
test-file)
122+
t
123+
(lambda (_) lsp-dart-tests-buffer-name)))
124+
(compilation-start (format "%s test" (lsp-dart-test--build-command))
130125
t
131126
(lambda (_) lsp-dart-tests-buffer-name)))))
132127

@@ -142,7 +137,7 @@ If TEST is non nil, it will run only this test."
142137
(unless group-kind? "$")))
143138
(test-arg `("--name" ,regex)))
144139
(lsp-workspace-set-metadata "last-ran-test" test)
145-
(if (lsp-dart-test--flutter-test-file-p (get-file-buffer file-name))
140+
(if (lsp-dart--flutter-project-p)
146141
(lsp-dart-dap-debug-flutter-test file-name test-arg)
147142
(lsp-dart-dap-debug-dart-test file-name test-arg))))
148143

@@ -253,12 +248,18 @@ Search for the last test overlay."
253248

254249
;;;###autoload
255250
(defun lsp-dart-run-test-file ()
256-
"Run dart/Flutter test command only for current buffer."
251+
"Run Dart/Flutter test command only for current buffer."
257252
(interactive)
258253
(if (lsp-dart-test-file-p (buffer-file-name))
259254
(lsp-dart-test--run (->> (current-buffer) buffer-name file-truename (make-lsp-dart-test :file-name)))
260255
(user-error "Current buffer is not a Dart/Flutter test file")))
261256

257+
;;;###autoload
258+
(defun lsp-dart-run-all-tests ()
259+
"Run each test from project."
260+
(interactive)
261+
(lsp-dart-test--run))
262+
262263
;;;###autoload
263264
(defun lsp-dart-visit-last-test ()
264265
"Visit the last ran test going to test definition."

0 commit comments

Comments
 (0)