comparison .elisp/nose.el @ 76:2b2a667092fd

Add nose.el.
author Augie Fackler <durin42@gmail.com>
date Thu, 09 Apr 2009 13:29:13 -0500
parents
children 423b8666e6f6
comparison
equal deleted inserted replaced
75:585a46facb75 76:2b2a667092fd
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
30 ;; Probably also want some keybindings:
31 ;; (add-hook 'python-mode-hook
32 ;; (lambda ()
33 ;; (local-set-key "\C-ca" 'nosetests-all)
34 ;; (local-set-key "\C-cm" 'nosetests-module)
35 ;; (local-set-key "\C-c." 'nosetests-one)
36 ;; (local-set-key "\C-cpa" 'nosetests-pdb-all)
37 ;; (local-set-key "\C-cpm" 'nosetests-pdb-module)
38 ;; (local-set-key "\C-cp." 'nosetests-pdb-one)))
39
40 (defvar nose-project-names '("eco/bin/test"))
41 (defvar nose-global-name "nosetests")
42
43 (defun run-nose (&optional tests debug)
44 "run nosetests"
45 (interactive)
46
47 (let* ((nose (nose-find-test-runner))
48 (where (expand-file-name "../.." (file-name-directory nose)))
49 (args (if debug "--pdb" ""))
50 (tnames (if tests tests "")))
51 (print nose)
52 (print args)
53 (print tnames)
54 (funcall (if debug 'pdb 'compile)
55 (format "%s -v %s -w %s -c %s/setup.cfg %s"
56 (nose-find-test-runner) args where where tnames)))
57 )
58
59 (defun nosetests-all (&optional debug)
60 "run all tests"
61 (interactive)
62 (run-nose nil debug))
63
64 (defun nosetests-pdb-all ()
65 (interactive)
66 (nosetests-all t))
67
68 (defun nosetests-module (&optional debug)
69 "run nosetests (via eggs/bin/test) on current buffer"
70 (interactive)
71 (run-nose buffer-file-name debug))
72
73 (defun nosetests-pdb-module ()
74 (interactive)
75 (nosetests-module t))
76
77 (defun nosetests-one (&optional debug)
78 "run nosetests (via eggs/bin/test) on testable thing
79 at point in current buffer"
80 (interactive)
81 (run-nose (format "%s:%s" buffer-file-name (nose-py-testable)) debug))
82
83 (defun nosetests-pdb-one ()
84 (interactive)
85 (nosetests-one t))
86
87 (defun nose-find-test-runner ()
88 (interactive)
89 (message
90 (let ((result (reduce '(lambda (x y) (or x y))
91 (mapcar 'nose-find-test-runner-names nose-project-names))))
92 (if result
93 result
94 nose-global-name))))
95
96 (defun nose-find-test-runner-names (runner)
97 "find eggs/bin/test in a parent dir of current buffer's file"
98 (nose-find-test-runner-in-dir-named (file-name-directory buffer-file-name) runner))
99
100 (defun nose-find-test-runner-in-dir-named (dn runner)
101 (let ((fn (expand-file-name runner dn)))
102 (cond ((file-regular-p fn) fn)
103 ((equal dn "/") nil)
104 (t (nose-find-test-runner-in-dir-named
105 (file-name-directory (directory-file-name dn))
106 runner)))))
107
108 (defun nose-py-testable ()
109 (interactive)
110 (let ((remember-point (point)))
111 (re-search-backward
112 "^ \\{0,4\\}\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t)
113 (setq t1 (buffer-substring-no-properties (match-beginning 2) (match-end 2)))
114 (goto-char remember-point)
115 (re-search-backward
116 "^\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t)
117 (setq outer
118 (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
119 (setq t2 (buffer-substring-no-properties (match-beginning 2) (match-end 2)))
120 (let
121 ((result (cond ((string= outer "def") t2)
122 ((string= t1 t2) t2)
123 (t (format "%s.%s" t2 t1)))))
124 (goto-char remember-point)
125 result)))
126
127 (provide 'nose)