Skip to content

Commit

Permalink
Add venv-mkvirtualenv-using, allowing programmatic choice of interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
sambrightman committed Nov 11, 2017
1 parent 8217d10 commit f2f5792
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Cask
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(source gnu)
(source melpa)

(package "virtualenvwrapper" "20140315" "a featureful virtualenv tool for Emacs")
Expand All @@ -7,4 +8,5 @@

(development
(depends-on "ert-runner")
(depends-on "noflet"))
(depends-on "noflet")
(depends-on "with-simulated-input"))
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ list), then the new virtualenv will be created in the current default
directory. Also callable noninteractively as `(venv-mkvirtualenv
"name")`.

#### `venv-mkvirtualenv-using`

Supplying a prefix command (`C-u`) to `venv-mkvirtualenv` will prompt
for a Python interpreter to use. You can use this function to specify
the interpreter noninteractively.

#### `venv-rmvirtualenv`

Prompt for the name of a virutalenv and delete it. Also callable
Expand Down
57 changes: 56 additions & 1 deletion test/virtualenvwrapper-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(load (expand-file-name "virtualenvwrapper.el" default-directory))
(require 's)
(require 'noflet)
(require 'with-simulated-input)

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

Expand Down Expand Up @@ -58,6 +59,60 @@
(venv-rmvirtualenv venv-tmp-env)
(should-error (venv-workon venv-tmp-env))))

(ert-deftest venv-mkvirtualenv-select-default-interpreter ()
(with-temp-location
(let ((current-prefix-arg '(4)))
(with-simulated-input
"RET"
(venv-mkvirtualenv venv-tmp-env))
(should (equal venv-current-name venv-tmp-env))
(venv-deactivate)
(venv-rmvirtualenv venv-tmp-env))))

(ert-deftest venv-mkvirtualenv-select-different-interpreter ()
(with-temp-location
(let ((current-prefix-arg '(4)))
(with-simulated-input
(concat (executable-find "python") " RET")
(venv-mkvirtualenv venv-tmp-env))
(should (equal venv-current-name venv-tmp-env))
(venv-deactivate)
(venv-rmvirtualenv venv-tmp-env))))

(ert-deftest venv-mkvirtualenv-using-default-interpreter-works ()
(with-temp-location
(venv-mkvirtualenv-using nil venv-tmp-env)
(should (equal venv-current-name venv-tmp-env))
(venv-deactivate)
(venv-rmvirtualenv venv-tmp-env)))

(ert-deftest venv-mkvirtualenv-using-different-interpreter-works ()
(with-temp-location
(venv-mkvirtualenv-using (executable-find "python") venv-tmp-env)
(should (equal venv-current-name venv-tmp-env))
(venv-deactivate)
(venv-rmvirtualenv venv-tmp-env)))

(ert-deftest venv-mkvirtualenv-using-select-default-interpreter ()
(with-temp-location
(with-simulated-input
"RET"
(let ((current-prefix-arg '(4)))
(venv-mkvirtualenv-using "some invalid interpreter" venv-tmp-env)))
(should (equal venv-current-name venv-tmp-env))
(venv-deactivate)
(venv-rmvirtualenv venv-tmp-env)))

(ert-deftest venv-mkvirtualenv-using-select-different-interpreter ()
(with-temp-location
(with-simulated-input
(concat (executable-find "python") " RET")
(let ((current-prefix-arg '(4)))
(venv-mkvirtualenv-using "some invalid interpreter" venv-tmp-env)))
(should (equal venv-current-name venv-tmp-env))
(venv-deactivate)
(venv-rmvirtualenv venv-tmp-env)))

(ert-deftest venv-workon-works ()
(with-temp-env
venv-tmp-env
Expand All @@ -82,7 +137,7 @@
;; we remove out dir to exec-path
(should (not (s-contains? venv-tmp-env (car exec-path)))))

(ert-deftest venv-workon-errors-for-nonexistance ()
(ert-deftest venv-workon-errors-for-nonexistence ()
(should-error (venv-workon "i-hopefully-do-not-exist")))

(ert-deftest venv-list-virtualenvs-works ()
Expand Down
29 changes: 20 additions & 9 deletions virtualenvwrapper.el
Original file line number Diff line number Diff line change
Expand Up @@ -324,20 +324,22 @@ throwing an error if not"
)

;;;###autoload
(defun venv-mkvirtualenv (&rest names)
"Create new virtualenvs NAMES. If venv-location is a single
directory, the new virtualenvs are made there; if it is a list of
directories, the new virtualenvs are made in the current
default-directory."
(defun venv-mkvirtualenv-using (interpreter &rest names)
"Create new virtualenvs NAMES using INTERPRETER. If venv-location
is a single directory, the new virtualenvs are made there; if it
is a list of directories, the new virtualenvs are made in the
current `default-directory'."
(interactive)
(venv--check-executable)
(let ((parent-dir (if (stringp venv-location)
(let* ((foo (if current-prefix-arg
(read-string "Python executable: ")
interpreter))
(parent-dir (if (stringp venv-location)
(file-name-as-directory
(expand-file-name venv-location))
default-directory))
(python-exe-arg (when current-prefix-arg
(concat "--python="
(read-string "Python executable: " "python"))))
(python-exe-arg (when foo
(concat "--python=" foo)))
(names (if names names
(list (read-from-minibuffer "New virtualenv: ")))))
;; map over all the envs we want to make
Expand All @@ -356,6 +358,15 @@ default-directory."
;; workon the last venv we made
(venv-workon (car (last names)))))

;;;###autoload
(defun venv-mkvirtualenv (&rest names)
"Create new virtualenvs NAMES. If venv-location is a single
directory, the new virtualenvs are made there; if it is a list of
directories, the new virtualenvs are made in the current
`default-directory'."
(interactive)
(apply #'venv-mkvirtualenv-using nil names))

;;;###autoload
(defun venv-rmvirtualenv (&rest names)
"Delete virtualenvs NAMES."
Expand Down

0 comments on commit f2f5792

Please sign in to comment.