annotate .elisp/nose.el @ 77:45d7441d0cf2

Modularize .emacs
author Augie Fackler <durin42@gmail.com>
date Thu, 09 Apr 2009 13:39:20 -0500
parents 2b2a667092fd
children 423b8666e6f6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
76
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
1 ;; nose.el --- Easy Python test running in Emacs
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
2
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
3 ;; Copyright (C) 2009 Jason Pellerin, Augie Fackler
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
4
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
5 ;; Licensed under the same terms as Emacs.
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
6
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
7 ;; Version: 0.1.0
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
8 ;; Keywords: nose python testing
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
9 ;; Created: 04 Apr 2009
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
10
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
11 ;; This file is NOT part of GNU Emacs.
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
12
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
13 ;; Licensed under the same terms as Emacs.
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
14
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
15 ;;; Commentary:
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
16 ;; This gives a bunch of functions that handle running nosetests on a
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
17 ;; particular buffer or part of a buffer.
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
18
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
19 ;;; Installation
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
20
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
21 ;; In your emacs config:
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
22 ;;
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
23 ;; (require 'nose)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
24 ;; ; next line only for people with non-eco non-global test runners
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
25 ;; ; (add-to-list 'nose-project-names "my/crazy/runner")
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
26
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
27 ;; Note that if your global nose isn't called "nosetests", then you'll want to
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
28 ;; redefine nose-global-name to be the command that should be used.
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
29
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
30 ;; Probably also want some keybindings:
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
31 ;; (add-hook 'python-mode-hook
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
32 ;; (lambda ()
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
33 ;; (local-set-key "\C-ca" 'nosetests-all)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
34 ;; (local-set-key "\C-cm" 'nosetests-module)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
35 ;; (local-set-key "\C-c." 'nosetests-one)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
36 ;; (local-set-key "\C-cpa" 'nosetests-pdb-all)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
37 ;; (local-set-key "\C-cpm" 'nosetests-pdb-module)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
38 ;; (local-set-key "\C-cp." 'nosetests-pdb-one)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
39
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
40 (defvar nose-project-names '("eco/bin/test"))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
41 (defvar nose-global-name "nosetests")
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
42
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
43 (defun run-nose (&optional tests debug)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
44 "run nosetests"
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
45 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
46
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
47 (let* ((nose (nose-find-test-runner))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
48 (where (expand-file-name "../.." (file-name-directory nose)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
49 (args (if debug "--pdb" ""))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
50 (tnames (if tests tests "")))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
51 (print nose)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
52 (print args)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
53 (print tnames)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
54 (funcall (if debug 'pdb 'compile)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
55 (format "%s -v %s -w %s -c %s/setup.cfg %s"
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
56 (nose-find-test-runner) args where where tnames)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
57 )
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
58
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
59 (defun nosetests-all (&optional debug)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
60 "run all tests"
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
61 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
62 (run-nose nil debug))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
63
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
64 (defun nosetests-pdb-all ()
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
65 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
66 (nosetests-all t))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
67
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
68 (defun nosetests-module (&optional debug)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
69 "run nosetests (via eggs/bin/test) on current buffer"
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
70 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
71 (run-nose buffer-file-name debug))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
72
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
73 (defun nosetests-pdb-module ()
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
74 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
75 (nosetests-module t))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
76
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
77 (defun nosetests-one (&optional debug)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
78 "run nosetests (via eggs/bin/test) on testable thing
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
79 at point in current buffer"
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
80 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
81 (run-nose (format "%s:%s" buffer-file-name (nose-py-testable)) debug))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
82
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
83 (defun nosetests-pdb-one ()
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
84 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
85 (nosetests-one t))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
86
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
87 (defun nose-find-test-runner ()
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
88 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
89 (message
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
90 (let ((result (reduce '(lambda (x y) (or x y))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
91 (mapcar 'nose-find-test-runner-names nose-project-names))))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
92 (if result
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
93 result
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
94 nose-global-name))))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
95
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
96 (defun nose-find-test-runner-names (runner)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
97 "find eggs/bin/test in a parent dir of current buffer's file"
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
98 (nose-find-test-runner-in-dir-named (file-name-directory buffer-file-name) runner))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
99
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
100 (defun nose-find-test-runner-in-dir-named (dn runner)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
101 (let ((fn (expand-file-name runner dn)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
102 (cond ((file-regular-p fn) fn)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
103 ((equal dn "/") nil)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
104 (t (nose-find-test-runner-in-dir-named
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
105 (file-name-directory (directory-file-name dn))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
106 runner)))))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
107
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
108 (defun nose-py-testable ()
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
109 (interactive)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
110 (let ((remember-point (point)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
111 (re-search-backward
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
112 "^ \\{0,4\\}\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
113 (setq t1 (buffer-substring-no-properties (match-beginning 2) (match-end 2)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
114 (goto-char remember-point)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
115 (re-search-backward
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
116 "^\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
117 (setq outer
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
118 (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
119 (setq t2 (buffer-substring-no-properties (match-beginning 2) (match-end 2)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
120 (let
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
121 ((result (cond ((string= outer "def") t2)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
122 ((string= t1 t2) t2)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
123 (t (format "%s.%s" t2 t1)))))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
124 (goto-char remember-point)
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
125 result)))
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
126
2b2a667092fd Add nose.el.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
127 (provide 'nose)