From 87807febc13c5964ca74ba789854b7212a605b2a Mon Sep 17 00:00:00 2001 From: Jimmy Yuen Ho Wong Date: Mon, 24 Jun 2024 18:31:35 +0100 Subject: [PATCH] micromamba support --- README.rst | 4 +++- pet.el | 11 +++++++---- test/pet-test.el | 18 +++++++++++++++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 6c62c57..9677f0b 100644 --- a/README.rst +++ b/README.rst @@ -64,7 +64,9 @@ Supported Python Virtual Environment Tools it unless you are using Homebrew on macOS) - `uv `_ - `docker `_ -- conda/mamba/micromamba (preliminary) +- `conda `_ +- `mamba `_ +- `micromamba `_ - Whatever is on your ``VIRTUAL_ENV`` environment variable - Even when you aren't in a virtual environment diff --git a/pet.el b/pet.el index 0e95742..c08c3a1 100644 --- a/pet.el +++ b/pet.el @@ -596,17 +596,20 @@ Selects a virtualenv in the follow order: (when-let ((ev (getenv "VIRTUAL_ENV"))) (expand-file-name ev)) (let ((venv-path - (cond ((when-let ((program (pet-use-conda-p)) - (default-directory (file-name-directory (pet-environment-path)))) + (cond ((when-let* ((program (pet-use-conda-p)) + (default-directory (file-name-directory (pet-environment-path))) + (program-args (if (string-match-p "micromamba" program) + '("env" "list" "--json") + '("info" "--json")))) (condition-case err (with-temp-buffer - (let ((exit-code (process-file program nil t nil "info" "--envs" "--json")) + (let ((exit-code (apply 'process-file program nil t nil program-args)) (output (string-trim (buffer-string)))) (if (zerop exit-code) (let* ((prefix (alist-get 'prefix (pet-environment))) (env (car (member prefix (let-alist (pet-parse-json output) .envs))))) (or env - (user-error "Please create the environment with `$ %s create -f %s' first" program (pet-environment-path)))) + (user-error "Please create the environment with `$ %s create --file %s' first" program (pet-environment-path)))) (user-error (buffer-string))))) (error (pet-report-error err))))) ((when-let ((program (pet-use-poetry-p)) diff --git a/test/pet-test.el b/test/pet-test.el index 0b91c3f..23d04c4 100644 --- a/test/pet-test.el +++ b/test/pet-test.el @@ -668,7 +668,8 @@ (describe "when using `pre-commit'" (before-each - (spy-on 'pet-use-pre-commit-p :and-return-value "/usr/bin/pre-commit")) + (spy-on 'pet-use-pre-commit-p :and-return-value "/usr/bin/pre-commit") + (spy-on 'pet--executable-find :and-return-value nil)) (it "should return the absolute path to the executable if hook and hook repo are found and the executable is found in hook repo" (spy-on 'pet-pre-commit-config-has-hook-p :and-return-value t) @@ -718,7 +719,9 @@ (describe "pet-virtualenv-root" :var ((project-root "/home/user/project/") (conda-path "/usr/bin/conda") - (conda-virtualenv "/home/user/.conda/envs/project/") + (conda-virtualenv "/home/user/miniforge3/envs/project/") + (micromamba-path "/usr/bin/micromamba") + (micromamba-virtualenv "/home/user/micromamba/envs/project/") (poetry-path "/usr/bin/poetry") (poetry-virtualenv "/home/user/.cache/pypoetry/virtualenvs/project/") (pipenv-path "/usr/bin/pipenv") @@ -758,7 +761,16 @@ (spy-on 'pet-environment :and-return-value `((prefix . ,conda-virtualenv))) (expect (pet-virtualenv-root) :to-equal conda-virtualenv) (expect (assoc-default project-root pet-project-virtualenv-cache) :to-equal conda-virtualenv) - (expect 'call-process :to-have-been-called-with conda-path nil t nil "info" "--envs" "--json")) + (expect 'call-process :to-have-been-called-with conda-path nil t nil "info" "--json")) + + (it "should return the absolute path of the virtualenv for a project using `micromamba'" + (spy-on 'pet-use-conda-p :and-return-value micromamba-path) + (spy-on 'pet-environment-path :and-return-value "/home/user/project/environment.yml") + (spy-on 'call-process :and-call-fake (lambda (&rest _) (insert (format "{\"envs\": [\"%s\"]}" micromamba-virtualenv)) 0)) + (spy-on 'pet-environment :and-return-value `((prefix . ,micromamba-virtualenv))) + (expect (pet-virtualenv-root) :to-equal micromamba-virtualenv) + (expect (assoc-default project-root pet-project-virtualenv-cache) :to-equal micromamba-virtualenv) + (expect 'call-process :to-have-been-called-with micromamba-path nil t nil "env" "list" "--json")) (it "should return the absolute path of the virtualenv for a project using `poetry'" (spy-on 'pet-use-conda-p :and-return-value nil)