Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…apper.el into customize-virtualenv
  • Loading branch information
vzell-opitz committed Feb 23, 2019
2 parents 0b0317c + 49e3219 commit 0404678
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
45 changes: 40 additions & 5 deletions test/virtualenvwrapper-test.el
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
;; Rudimentary test suite for virtualenvwrapper.el

;; WARNING I am fairly confident these tests are safe to run -- they
;; do everything in a tmp dir and clean up after themselves. However
;; they do modify the filesystem and filesystems are horrible, so use
;; with caution just in case.

(load (expand-file-name "virtualenvwrapper.el" default-directory))
(require 's)
(require 'noflet)
(require 'with-simulated-input)

;; unclear why this is required, we get `(void-function string-trim)'
;; errors without, probably has something to do with byte-compiling
(require 'subr-x)

(setq venv-tmp-env "emacs-venvwrapper-test")

(defmacro with-temp-location (&rest forms)
Expand All @@ -27,6 +26,15 @@
,@forms)
(venv-rmvirtualenv ,name))))


(defmacro with-temp-dir (&rest forms)
`(let ((temp-dir (file-name-as-directory (make-temp-file nil t))))
(unwind-protect
(progn
,@forms)
(delete-directory temp-dir t))))


(defun assert-venv-activated ()
"Runs various assertions to check if a venv is activated."
;; M-x pdb should ask to run "python -m pdb"
Expand Down Expand Up @@ -210,3 +218,30 @@
(venv-deactivate)
(venv-projectile-auto-workon)
(assert-venv-activated)))))

(ert-deftest venv-test-auto-cd-to-project-dir-works ()
(with-temp-env
venv-tmp-env
(with-temp-dir
(venv-deactivate)
(should (not (equal default-directory temp-dir)))
;; set the project dir to be `temp-dir'
(append-to-file temp-dir nil
(s-concat (venv-name-to-dir venv-tmp-env) ".project"))
(venv-workon venv-tmp-env)
;; TODO should probably set these up to reset current-directory
;; when done for hygeine purposes
(should (equal default-directory temp-dir)))))

(ert-deftest venv-test-workon-does-not-cd-to-project-when-disabled ()
(with-temp-env
venv-tmp-env
(with-temp-dir
(let ((venv-workon-cd nil))
(venv-deactivate)
(should (not (equal default-directory temp-dir)))
;; set the project dir to be `temp-dir'
(append-to-file temp-dir nil
(s-concat (venv-name-to-dir venv-tmp-env) ".project"))
(venv-workon venv-tmp-env)
(should (not (equal default-directory temp-dir)))))))
13 changes: 12 additions & 1 deletion virtualenvwrapper.el
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ to activate when one of them is found."
:type '(repeat file)
:group 'virtualenvwrapper)


(defcustom venv-workon-cd
t
"If set to t, cd to the virtualenv's project root when
activating it. Analgous to the VIRTUALENVWRAPPER_WORKON_CD
enviornment variable in the original virtualenvwrapper"
:type '(boolean)
:group 'virtualenvwrapper)


;; hooks

(defvar venv-premkvirtualenv-hook nil
Expand Down Expand Up @@ -233,7 +243,8 @@ prompting the user with the string PROMPT"
;; keep eshell path in sync
(setq eshell-path-env path))
(setenv "VIRTUAL_ENV" venv-current-dir)
(venv--switch-to-project-dir)
(if venv-workon-cd
(venv--switch-to-project-dir))
(venv--set-venv-gud-pdb-command-name)
(run-hooks 'venv-postactivate-hook))

Expand Down

0 comments on commit 0404678

Please sign in to comment.