Mercurial > dotfiles
annotate .elisp/nose.el @ 92:2563edf11e59
Latest nosemacs.
author | Augie Fackler <durin42@gmail.com> |
---|---|
date | Wed, 22 Apr 2009 10:17:31 -0500 |
parents | 79eec81404c3 |
children | e83373ab1581 |
rev | line source |
---|---|
76 | 1 ;; nose.el --- Easy Python test running in Emacs |
2 | |
3 ;; Copyright (C) 2009 Jason Pellerin, Augie Fackler | |
4 | |
5 ;; Licensed under the same terms as Emacs. | |
6 | |
7 ;; Version: 0.1.0 | |
8 ;; Keywords: nose python testing | |
9 ;; Created: 04 Apr 2009 | |
10 | |
11 ;; This file is NOT part of GNU Emacs. | |
12 | |
13 ;; Licensed under the same terms as Emacs. | |
14 | |
15 ;;; Commentary: | |
16 ;; This gives a bunch of functions that handle running nosetests on a | |
17 ;; particular buffer or part of a buffer. | |
18 | |
19 ;;; Installation | |
20 | |
21 ;; In your emacs config: | |
22 ;; | |
23 ;; (require 'nose) | |
24 ;; ; next line only for people with non-eco non-global test runners | |
25 ;; ; (add-to-list 'nose-project-names "my/crazy/runner") | |
26 | |
27 ;; Note that if your global nose isn't called "nosetests", then you'll want to | |
28 ;; redefine nose-global-name to be the command that should be used. | |
29 | |
88 | 30 ;; By default, the root of a project is found by looking for any of the files |
31 ;; 'setup.py', '.hg' and '.git'. You can add files to check for to the file | |
32 ;; list: | |
33 ;; | |
34 ;; ; (add-to-list 'nose-project-root-files "something") | |
35 | |
36 ;; or you can change the project root test to detect in some other way | |
37 ;; whether a directory is the project root: | |
38 ;; | |
39 ;; ; (setq nose-project-root-test (lambda (dirname) (equal dirname "foo"))) | |
40 | |
41 ;; If you want dots as output, rather than the verbose output: | |
42 ;; (defvar nose-use-verbose nil) ; default is t | |
43 | |
76 | 44 ;; Probably also want some keybindings: |
45 ;; (add-hook 'python-mode-hook | |
46 ;; (lambda () | |
47 ;; (local-set-key "\C-ca" 'nosetests-all) | |
48 ;; (local-set-key "\C-cm" 'nosetests-module) | |
49 ;; (local-set-key "\C-c." 'nosetests-one) | |
50 ;; (local-set-key "\C-cpa" 'nosetests-pdb-all) | |
51 ;; (local-set-key "\C-cpm" 'nosetests-pdb-module) | |
52 ;; (local-set-key "\C-cp." 'nosetests-pdb-one))) | |
53 | |
54 (defvar nose-project-names '("eco/bin/test")) | |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
55 (defvar nose-project-root-files '("setup.py" ".hg" ".git")) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
56 (defvar nose-project-root-test 'nose-project-root) |
76 | 57 (defvar nose-global-name "nosetests") |
88 | 58 (defvar nose-use-verbose t) |
76 | 59 |
60 (defun run-nose (&optional tests debug) | |
61 "run nosetests" | |
62 (let* ((nose (nose-find-test-runner)) | |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
63 (where (nose-find-project-root)) |
76 | 64 (args (if debug "--pdb" "")) |
65 (tnames (if tests tests ""))) | |
85 | 66 (funcall (if debug |
67 'pdb | |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
68 '(lambda (command) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
69 (compilation-start command |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
70 nil |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
71 (lambda (mode) (concat "*nosetests*"))))) |
88 | 72 (format |
73 (concat "%s " | |
92 | 74 (if nose-use-verbose "-v " "") |
88 | 75 "%s -w %s -c %ssetup.cfg %s") |
76 (nose-find-test-runner) args where where tnames))) | |
76 | 77 ) |
78 | |
79 (defun nosetests-all (&optional debug) | |
80 "run all tests" | |
81 (interactive) | |
82 (run-nose nil debug)) | |
83 | |
84 (defun nosetests-pdb-all () | |
85 (interactive) | |
86 (nosetests-all t)) | |
87 | |
88 (defun nosetests-module (&optional debug) | |
89 "run nosetests (via eggs/bin/test) on current buffer" | |
90 (interactive) | |
91 (run-nose buffer-file-name debug)) | |
92 | |
93 (defun nosetests-pdb-module () | |
94 (interactive) | |
95 (nosetests-module t)) | |
96 | |
97 (defun nosetests-one (&optional debug) | |
98 "run nosetests (via eggs/bin/test) on testable thing | |
99 at point in current buffer" | |
100 (interactive) | |
101 (run-nose (format "%s:%s" buffer-file-name (nose-py-testable)) debug)) | |
102 | |
103 (defun nosetests-pdb-one () | |
104 (interactive) | |
105 (nosetests-one t)) | |
106 | |
107 (defun nose-find-test-runner () | |
108 (message | |
85 | 109 (let ((result |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
110 (reduce '(lambda (x y) (or x y)) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
111 (mapcar 'nose-find-test-runner-names nose-project-names)))) |
76 | 112 (if result |
113 result | |
114 nose-global-name)))) | |
115 | |
116 (defun nose-find-test-runner-names (runner) | |
117 "find eggs/bin/test in a parent dir of current buffer's file" | |
85 | 118 (nose-find-test-runner-in-dir-named |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
119 (file-name-directory buffer-file-name) runner)) |
76 | 120 |
121 (defun nose-find-test-runner-in-dir-named (dn runner) | |
122 (let ((fn (expand-file-name runner dn))) | |
123 (cond ((file-regular-p fn) fn) | |
124 ((equal dn "/") nil) | |
125 (t (nose-find-test-runner-in-dir-named | |
126 (file-name-directory (directory-file-name dn)) | |
127 runner))))) | |
128 | |
129 (defun nose-py-testable () | |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
130 (let* ((inner-obj (inner-testable)) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
131 (outer (outer-testable)) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
132 ;; elisp can't return multiple values |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
133 (outer-def (car outer)) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
134 (outer-obj (cdr outer))) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
135 (cond ((equal outer-def "def") outer-obj) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
136 ((equal inner-obj outer-obj) outer-obj) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
137 (t (format "%s.%s" outer-obj inner-obj))))) |
85 | 138 |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
139 (defun inner-testable () |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
140 (save-excursion |
76 | 141 (re-search-backward |
142 "^ \\{0,4\\}\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t) | |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
143 (buffer-substring-no-properties (match-beginning 2) (match-end 2)))) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
144 |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
145 (defun outer-testable () |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
146 (save-excursion |
76 | 147 (re-search-backward |
148 "^\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t) | |
85 | 149 (let ((result |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
150 (buffer-substring-no-properties (match-beginning 2) (match-end 2)))) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
151 |
85 | 152 (cons |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
153 (buffer-substring-no-properties (match-beginning 1) (match-end 1)) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
154 result)))) |
76 | 155 |
85 | 156 (defun nose-find-project-root (&optional dirname) |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
157 (let ((dn |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
158 (if dirname |
85 | 159 dirname |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
160 (file-name-directory buffer-file-name)))) |
88 | 161 (cond ((funcall nose-project-root-test dn) (expand-file-name dn)) |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
162 ((equal (expand-file-name dn) "/") nil) |
85 | 163 (t (nose-find-project-root |
84
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
164 (file-name-directory (directory-file-name dn))))))) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
165 |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
166 (defun nose-project-root (dirname) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
167 (reduce '(lambda (x y) (or x y)) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
168 (mapcar (lambda (d) (member d (directory-files dirname))) |
f013f40e7b63
Import latest nosemacs, set nose to output dots instead of verbose output.
Augie Fackler <durin42@gmail.com>
parents:
80
diff
changeset
|
169 nose-project-root-files))) |
85 | 170 |
76 | 171 (provide 'nose) |