forked from ionrock/pytest-el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest.el
279 lines (234 loc) · 9.52 KB
/
pytest.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
;;; pytest.el --- Easy Python test running in Emacs
;; Copyright (C) 2009 Eric Larson
;; Licensed under the same terms as Emacs.
;; Version: 0.2.1
;; Keywords: pytest python testing
;; URL: https://github.com/ionrock/pytest-el
;; Package-Requires: ((s "1.9.0"))
;; Created: 07 Oct 2011
;; This file is NOT part of GNU Emacs.
;; Licensed under the same terms as Emacs.
;;; Commentary:
;; This gives a bunch of functions that handle running pytest on a
;; particular buffer or part of a buffer. This started as a direct
;; port of nosemacs (https://bitbucket.org/durin42/nosemacs). A
;; special thanks to Jason Pellerin and Augie Fackler for writing
;; nose.el.
;;; Installation
;; In your Emacs config:
;;
;; (require 'pytest)
;;
;; If you don't use a global installation of py.test (ie in
;; virtualenv) then add something like the following that points to
;; either the non-global version or a test runner script.:
;;
;; (add-to-list 'pytest-project-names "my/crazy/runner")
;;
;; You can generate a script with py.test:
;;
;; py.test --genscript=run-tests.py
;; Another option is if your global pytest isn't called "pytest" is to
;; redefine pytest-global-name to be the command that should be used.
;; By default, the root of a project is found by looking for any of the files
;; 'setup.py', '.hg' and '.git'. You can add files to check for to the file
;; list:
;;
;; ; (add-to-list 'pytest-project-root-files "something")
;; or you can change the project root test to detect in some other way
;; whether a directory is the project root:
;;
;; ; (setq pytest-project-root-test (lambda (dirname) (equal dirname "foo")))
;; Probably also want some keybindings:
;; (add-hook 'python-mode-hook
;; (lambda ()
;; (local-set-key "\C-ca" 'pytest-all)
;; (local-set-key "\C-cm" 'pytest-module)
;; (local-set-key "\C-c." 'pytest-one)
;; (local-set-key "\C-cd" 'pytest-directory)
;; (local-set-key "\C-cpa" 'pytest-pdb-all)
;; (local-set-key "\C-cpm" 'pytest-pdb-module)
;; (local-set-key "\C-cp." 'pytest-pdb-one)))
;;; Code:
(require 's)
(require 'cl)
(require 'python)
(defgroup pytest nil
"Easy Python test running in Emacs"
:group 'python)
(defcustom pytest-project-names '("runtests")
"The name of the script that starts the tests.")
(defcustom pytest-project-root-files '("setup.py" ".hg" ".git")
"Names of files or directories that signify the root of a project.")
(defcustom pytest-project-root-test 'pytest-project-root
"A function used to determine the directory the tests will be run from.")
(defcustom pytest-global-name "py.test"
"The name of the py.test executable.")
(put 'pytest-global-name 'safe-local-variable 'stringp)
(defcustom pytest-cmd-flags "-x -s"
"These are the flags passed to the pytest runner.")
(defcustom pytest-cmd-format-string "cd '%s' && %s %s %s"
"Format string used to run the py.test command.")
(defun pytest-cmd-format (format-string working-directory test-runner command-flags test-names)
"Create the string used for running the py.test command.
FORMAT-STRING is a template string used by (format) to compose
the py.test command invocation. The string should contain enough
'%s' placeholders to satisfy the remaining arguments to this
function.
WORKING-DIRECTORY is the directory to run py.test in.
TEST-RUNNER is the name of the command to run.
COMMAND-FLAGS are the flags to pass into py.test.
TEST-NAMES are the names of the tests to run.
The function returns a string used to run the py.test command. Here's an example:
'cd WORKING-DIRECTORY && TEST-RUNNER COMMAND-FLAGS TEST-NAMES'"
(format format-string working-directory test-runner command-flags test-names))
(defun pytest-check-test-file (path)
(if (not (file-exists-p path))
(error (format "'%s' is not an extant file." path))))
(defun pytest-run (&optional tests flags)
"Run pytest.
Optional argument TESTS Tests to run.
Optional argument FLAGS py.test command line flags."
(interactive "fTest directory or file: \nspy.test flags: ")
(let* ((pytest (pytest-find-test-runner))
(where (if tests
(let ((testpath (if (listp tests) (car tests) tests)))
(pytest-find-project-root (file-name-directory testpath)))
(pytest-find-project-root)))
(tests (cond ((not tests) "")
((listp tests) tests)
((stringp tests) (split-string tests))))
(_ (mapc 'pytest-check-test-file tests))
(tnames (mapconcat (apply-partially 'format "'%s'") tests " "))
(cmd-flags (if flags flags pytest-cmd-flags))
(use-comint (s-contains? "pdb" cmd-flags)))
(funcall #'(lambda (command)
(compilation-start command use-comint
(lambda (mode) (concat "*pytest*"))))
(pytest-cmd-format pytest-cmd-format-string where pytest cmd-flags tnames))
(if use-comint
(with-current-buffer (get-buffer "*pytest*")
(inferior-python-mode)))))
;;; Run entire test suite
;;;###autoload
(defun pytest-all (&optional flags)
"Run all tests.
Optional argument FLAGS py.test command line flags."
(interactive)
(pytest-run nil flags))
;;;###autoload
(defun pytest-failed ()
"Quit test suite on first failed test."
(interactive)
(pytest-all "-x "))
;;;###autoload
(defun pytest-pdb-all ()
"Start pdb on error."
(interactive)
(pytest-all (concat "--pdb " pytest-cmd-flags)))
;;; Run all the tests in a directory (and its child directories)
;;;###autoload
(defun pytest-directory (&optional flags)
"Run pytest on all the files in the current buffer.
Optional argument FLAGS py.test command line flags."
(interactive)
(pytest-run (file-name-directory buffer-file-name) flags))
;;;###autoload
(defun pytest-pdb-directory (&optional flags)
"Run pytest on all the files in the current buffer.
Optional argument FLAGS py.test command line flags."
(interactive)
(pytest-directory (concat "--pdb " pytest-cmd-flags)))
;;; Run all the tests in a file
;;;###autoload
(defun pytest-module (&optional flags)
"Run pytest (via eggs/bin/test) on current buffer.
Optional argument FLAGS py.test command line flags."
(interactive)
(pytest-run buffer-file-name flags))
;;;###autoload
(defun pytest-pdb-module ()
"Run pytest on a module, enter debugger on error."
(interactive)
(pytest-module (concat "--pdb " pytest-cmd-flags)))
;;; Run the test surrounding the current point
;;;###autoload
(defun pytest-one (&optional flags)
"Run pytest (via eggs/bin/test) on testable thing at point in current buffer.
Optional argument FLAGS py.test command line flags."
(interactive)
(pytest-run (format "%s" (pytest-py-testable)) flags))
;;;###autoload
(defun pytest-pdb-one ()
"Run pytest on testable thing at point, enter debugger on error."
(interactive)
(pytest-one (concat "--pdb " pytest-cmd-flags)))
;;; Utility functions
(defun pytest-find-test-runner ()
(let ((result
(reduce '(lambda (x y) (or x y))
(mapcar 'pytest-find-test-runner-names pytest-project-names))))
(if result
result
pytest-global-name)))
(defun pytest-find-test-runner-names (runner)
"Find eggs/bin/test in a parent dir of current buffer's file."
(pytest-find-test-runner-in-dir-named
(file-name-directory buffer-file-name) runner))
(defun pytest-find-test-runner-in-dir-named (dn runner)
(let ((fn (expand-file-name runner dn)))
(cond ((file-regular-p fn) fn)
((equal dn "/") nil)
(t (pytest-find-test-runner-in-dir-named
(file-name-directory (directory-file-name dn))
runner)))))
(defun pytest-py-testable ()
"Create a path to a test.
This uses the `::` delimiter between the
filename, class and method in order to find the specific test
case. This requires pytest >= 1.2."
(let* ((inner-obj (pytest-inner-testable))
(outer (pytest-outer-testable))
;; elisp can't return multiple values
(outer-def (car outer))
(outer-obj (cdr outer)))
(concat
(buffer-file-name)
(cond ((equal outer-def "def") (format "::%s" outer-obj))
((equal inner-obj outer-obj) (format "::%s" outer-obj))
(t (format "::%s::%s" outer-obj inner-obj))))))
(defun pytest-inner-testable ()
"Find the function name for `pytest-one'."
(save-excursion
(re-search-backward
"^ \\{0,4\\}\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t)
(buffer-substring-no-properties (match-beginning 2) (match-end 2))))
(defun pytest-outer-testable ()
"Find the class for the `pytest-one'."
(save-excursion
(re-search-backward
"^\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t)
(let ((result
(buffer-substring-no-properties (match-beginning 2) (match-end 2))))
(cons
(buffer-substring-no-properties (match-beginning 1) (match-end 1))
result))))
(defun pytest-find-project-root (&optional dirname)
(let ((dn
(if dirname
dirname
(file-name-directory buffer-file-name))))
(cond ((funcall pytest-project-root-test dn) (expand-file-name dn))
((equal (expand-file-name dn) "/") nil)
(t (pytest-find-project-root
(file-name-directory (directory-file-name dn)))))))
(defun pytest-project-root (dirname)
(reduce '(lambda (x y) (or x y))
(mapcar (lambda (d) (member d (directory-files dirname)))
pytest-project-root-files)))
(defun pytest-current-root ()
(if (not (buffer-file-name))
(expand-file-name default-directory)
(file-name-directory (expand-file-name (buffer-file-name)))))
(provide 'pytest)
;;; pytest.el ends here