Skip to content
Open
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
21 changes: 17 additions & 4 deletions pet.el
Original file line number Diff line number Diff line change
Expand Up @@ -1037,15 +1037,28 @@ Selects a virtualenv in the following order:

1. Cached virtualenv path (from previous detection or manual switching).
2. The value of the environment variable `VIRTUAL_ENV' if defined.
3. Poetry virtualenv from `pyproject.toml'.
4. Pipenv virtualenv from `Pipfile'.
5. A directory in `pet-venv-dir-names' in the project root if found.
6. Pyenv virtualenv from `.python-version'."
3. Pixi default environment from `pixi.toml' or `pyproject.toml'.
4. Conda/Mamba environment from `environment*.yml'.
5. Poetry virtualenv from `pyproject.toml'.
6. Pipenv virtualenv from `Pipfile'.
7. A directory in `pet-venv-dir-names' in the project root if found.
8. Pyenv virtualenv from `.python-version'."
(let ((root (pet-project-root)))
(or (pet-cache-get (list root :virtualenv))
(let ((venv-path
(cond ((when-let* ((ev (getenv "VIRTUAL_ENV")))
(expand-file-name ev)))
((when-let* ((program (pet-use-pixi-p)))
(let* ((result (pet-run-process-get-output program "info" "--json"))
(info (pet-parse-json result)))
(let-alist info
(when .environments_info
(let-alist (alist-get 'default .environments_info)
.prefix))))))
((when-let* ((program (or (pet-use-conda-p) (pet-use-mamba-p)))
(env-config (pet-environment)))
(let-alist env-config
.prefix)))
((when-let* ((program (pet-use-poetry-p))
(default-directory (file-name-directory (pet-pyproject-path))))
(pet-run-process-get-output program "env" "info" "--no-ansi" "--path")))
Expand Down
40 changes: 40 additions & 0 deletions test/pet-virtualenv-root-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,46 @@
(expect (pet-cache-get (list project-root :virtualenv)) :to-equal pyenv-virtualenv-truename)
(expect 'process-file :to-have-been-called-with pyenv-path nil t nil "prefix"))

(it "should return the absolute path of the virtualenv for a project using `pixi'"
(spy-on 'pet-use-conda-p)
(spy-on 'pet-use-mamba-p)
(spy-on 'pet-use-poetry-p)
(spy-on 'pet-use-pipenv-p)
(spy-on 'locate-dominating-file)
(spy-on 'pet-use-pyenv-p)
(spy-on 'pet-use-pixi-p :and-return-value pixi-path)
(spy-on 'pet-run-process-get-output :and-call-fake
(lambda (program &rest args)
(when (and (equal program pixi-path)
(equal args '("info" "--json")))
(format "{\"environments_info\":{\"default\":{\"prefix\":\"%s\"}}}" pixi-virtualenv))))
(expect (pet-virtualenv-root) :to-equal pixi-virtualenv)
(expect (pet-cache-get (list project-root :virtualenv)) :to-equal pixi-virtualenv))

(it "should return the absolute path of the virtualenv for a project using `conda'"
(spy-on 'pet-use-pixi-p)
(spy-on 'pet-use-mamba-p)
(spy-on 'pet-use-poetry-p)
(spy-on 'pet-use-pipenv-p)
(spy-on 'locate-dominating-file)
(spy-on 'pet-use-pyenv-p)
(spy-on 'pet-use-conda-p :and-return-value conda-path)
(spy-on 'pet-environment :and-return-value `((prefix . ,conda-virtualenv)))
(expect (pet-virtualenv-root) :to-equal conda-virtualenv)
(expect (pet-cache-get (list project-root :virtualenv)) :to-equal conda-virtualenv))

(it "should return the absolute path of the virtualenv for a project using `mamba'"
(spy-on 'pet-use-pixi-p)
(spy-on 'pet-use-conda-p)
(spy-on 'pet-use-poetry-p)
(spy-on 'pet-use-pipenv-p)
(spy-on 'locate-dominating-file)
(spy-on 'pet-use-pyenv-p)
(spy-on 'pet-use-mamba-p :and-return-value mamba-path)
(spy-on 'pet-environment :and-return-value `((prefix . ,mamba-virtualenv)))
(expect (pet-virtualenv-root) :to-equal mamba-virtualenv)
(expect (pet-cache-get (list project-root :virtualenv)) :to-equal mamba-virtualenv))

(it "should return the absolute path of the virtualenv for a project if the root is found in cache"
(pet-cache-put (list project-root :virtualenv) "/home/user/.venvs/env/")
(expect (pet-virtualenv-root) :to-equal "/home/user/.venvs/env/")))
Expand Down