Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 85 lines (73 sloc) 2.735 kb
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
;; Author: Chmouel Boudjnah <chmouel@chmouel.com>
;;
;; Provide two function to help with running nosetests. One
;; `nosetests-get-command` that would copy your yank ring the current
;; test to feed to nose on the shell and the other `nosetests-compile`
;; that would launch the current test with nose in a compile buffer.

(defvar nosetests-arg "-sx")
(defvar nosetests-executable "nosetests")

(defun inner-testable ()
  (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 outer-testable ()
  (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 nose-py-testable ()
  (let* ((inner-obj (inner-testable))
         (outer (outer-testable))
         ;; elisp can't return multiple values
         (outer-def (car outer))
         (outer-obj (cdr outer)))
    (cond ((equal outer-def "def") outer-obj)
          ((equal inner-obj outer-obj) outer-obj)
          (t (format "%s.%s" outer-obj inner-obj)))))

(defun nosetests-get-executable (topdir &optional env)
  (if (null env)
      (setq env "py27"))
  (let ((n (concat topdir ".tox/" env "/py27/bin/" nosetests-executable)))
    (if (file-exists-p n) n
      nosetests-executable)))

(defun nosetests-get-command (&optional withcd module)
  (let (topdir test-path current-function cmd)
    (setq topdir (file-truename (or (locate-dominating-file
                                     (buffer-file-name) "setup.py") "./")))
    (setq test-path (substring (file-truename
                                (buffer-file-name))
                               (length topdir)))
    (when (not module)
      (setq current-function (nose-py-testable))
      (if (not current-function)
          (error "No function at point")))
    (setq cmd "")
    (if withcd
        (setq cmd (concat "cd " topdir ";")))
    (concat cmd (nosetests-get-executable topdir) " "
            nosetests-arg " " test-path
            (if (not module)
                (concat ":" current-function)))
    ))

(defun nosetests-compile ()
  (interactive)
  (let (cmd)
    (setq cmd (nosetests-get-command t))
    (compile cmd)))

(defun nosetests-compile-module ()
  (interactive)
  (let (cmd)
    (setq cmd (nosetests-get-command t t))
    (compile cmd)))

(defun nosetests-copy-shell-comand ()
  (interactive)
  (let (cmd)
    (setq cmd (nosetests-get-command))
    (kill-new cmd)
    (message cmd)))

(provide 'nosetests)
Something went wrong with that request. Please try again.