Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions test/uv.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ python-dateutil==2.9.0.post0
(should-not (uv-activate-venv))))

(ert-deftest uv--activate-venv-venv-available ()
(setq uv--projects-last-venv nil)
(mocker-let ((project-current () ((:input '() :output (cons 'project "/foo/bar/project"))))
(project-root (project) ((:input '((project . "/foo/bar/project"))
:output "/foo/bar/project")))
Expand All @@ -490,6 +491,7 @@ python-dateutil==2.9.0.post0
(should (equal python-shell-virtualenv-root "/foo/bar/project/.venv"))))

(ert-deftest uv--activate-venv-unify-path ()
(setq uv--projects-last-venv nil)
(mocker-let ((project-current () ((:input '() :output (cons 'project "/foo/bar/project"))))
(project-root (project) ((:input '((project . "/foo/bar/project"))
:output "/foo/bar/project")))
Expand All @@ -505,6 +507,7 @@ python-dateutil==2.9.0.post0


(ert-deftest uv--activate-venv-venv-expand-path ()
(setq uv--projects-last-venv nil)
(let ((native-comp-enable-subr-trampolines nil))
(mocker-let ((project-current () ((:input '() :output (cons 'project "~/project"))))
(project-root (project) ((:input '((project . "~/project"))
Expand All @@ -523,7 +526,43 @@ python-dateutil==2.9.0.post0
(should (equal python-shell-virtualenv-root "/home/me/project/.venv")))))


(ert-deftest uv--activate-change-venv-expand-path ()
(setq uv--projects-last-venv nil)
(let ((native-comp-enable-subr-trampolines nil))
(mocker-let ((project-current () ((:input '() :output (cons 'project "~/project-one") :occur 1)
(:input '() :output (cons 'project "~/project-two") :occur 1)))
(project-root (project) ((:input '((project . "~/project-one"))
:output "~/project-one")
(:input '((project . "~/project-two"))
:output "~/project-two")))
(expand-file-name (name) ((:input '("~/project-one/.venv")
:output "/home/me/project-one/.venv")
(:input '("~/project-two/.venv")
:output "/home/me/project-two/.venv")))
(file-directory-p (venvdir) ((:input '("/home/me/project-one/.venv/bin")
:output t)
(:input '("/home/me/project-two/.venv/bin")
:output t)))
(getenv (var) ((:input '("PATH") :output "/some/path:/usr/bin")
(:input '("VIRTUAL_ENV") :output nil)
(:input '("PYTHONHOME") :output nil)
(:input '("VIRTUAL_ENV") :output "/home/me/project-one/.venv")
(:input '("PYTHONHOME") :output nil)))
(setenv (var value) ((:input '("VIRTUAL_ENV" "/home/me/project-one/.venv"))
(:input '("PYTHONHOME" nil))
(:input '("PATH" "/home/me/project-one/.venv/bin:/some/path:/usr/bin"))
(:input '("VIRTUAL_ENV" "/home/me/project-two/.venv"))
(:input '("PYTHONHOME" nil))
(:input '("PATH" "/home/me/project-two/.venv/bin:/some/path:/usr/bin")))))
(should (uv-activate-venv))
(should (equal python-shell-virtualenv-root "/home/me/project-one/.venv"))
(should (uv-activate-venv))
(should (equal python-shell-virtualenv-root "/home/me/project-two/.venv"))
)))


(ert-deftest uv--activate-and-deactivate-venv ()
(setq uv--projects-last-venv nil)
(mocker-let ((project-current () ((:input '() :output (cons 'project "/foo/bar/project"))))
(project-root (project) ((:input '((project . "/foo/bar/project"))
:output "/foo/bar/project")))
Expand Down
22 changes: 13 additions & 9 deletions uv.el
Original file line number Diff line number Diff line change
Expand Up @@ -709,23 +709,27 @@ suitable. Use `uv-lock' instead."
(let* ((candidate-venv (expand-file-name (concat (uv--project-root) ".venv")))
(candidate-bin (concat (file-name-as-directory candidate-venv) "bin")))
(if (file-directory-p candidate-bin)
(let ((old-venv-info `(:path ,(getenv "PATH")
:venv ,(getenv "VIRTUAL_ENV")
:python-home ,(getenv "PYTHONHOME"))))
(unless (equal old-venv-info uv--projects-last-venv)
(let ((current-path (or (plist-get uv--projects-last-venv :path)
(getenv "PATH")))
(virtual-env (getenv "VIRTUAL_ENV")))
(unless (equal virtual-env candidate-venv)
(setenv "VIRTUAL_ENV" candidate-venv)
(setenv "PYTHONHOME" nil)
(setenv "PATH" (string-join (cons candidate-bin (remove candidate-bin (string-split (plist-get old-venv-info :path) ":"))) ":"))
(setenv "PATH" (string-join (cons candidate-bin (remove candidate-bin (string-split current-path ":"))) ":"))
(setq python-shell-virtualenv-root candidate-venv)
(setq uv--projects-last-venv old-venv-info)
(setq uv--projects-last-venv
`(:path ,current-path
:venv ,virtual-env
:python-home ,(getenv "PYTHONHOME")))
candidate-venv))
(uv-deactivate-venv))))

(defun uv-deactivate-venv ()
(setq python-shell-virtualenv-root nil)
(setenv "VIRTUAL_ENV" nil)
(setenv "PYTHONHOME" (plist-get uv--projects-last-venv :python-home))
(when-let* ((path (plist-get uv--projects-last-venv :path))) (setenv "PATH" path))
(when-let* ((path (plist-get uv--projects-last-venv :path)))
(setenv "PATH" path))
(setq uv--projects-last-venv nil))

(cl-defmethod transient-infix-read ((obj uv--transient-multiswitch))
Expand Down Expand Up @@ -767,8 +771,8 @@ OJB is just the self reference."

(defun uv--project-root ()
"Save determination of the project root with `default-directory' as default."
(if (project-current)
(file-name-as-directory (project-root (project-current)))
(if-let* ((pc (project-current)))
(file-name-as-directory (project-root pc))
default-directory))

(defun uv--command-stdout-to-string (command-line)
Expand Down