Skip to content

Commit

Permalink
Fix #36 pet should find conda env without prefix in environment.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuenho committed Jul 5, 2024
1 parent 3581980 commit dc176e5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 12 additions & 3 deletions pet.el
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,20 @@ Selects a virtualenv in the follow order:
(default-directory (file-name-directory (pet-environment-path))))
(condition-case err
(with-temp-buffer
(let ((exit-code (process-file program nil t nil "env" "list" "--json"))
(let ((exit-code (process-file program nil t nil "info" "--json"))
(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)))))
(let* ((json-output (pet-parse-json output))
(env-dirs (or (let-alist json-output .envs_dirs)
(let-alist json-output .envs\ directories)))
(env-name (alist-get 'name (pet-environment)))
(env (seq-find 'file-directory-p
(seq-map (lambda (dir)
(file-name-as-directory
(concat
(file-name-as-directory dir)
env-name)))
env-dirs))))
(or env
(user-error "Please create the environment with `$ %s create --file %s' first" program (pet-environment-path))))
(user-error (buffer-string)))))
Expand Down
18 changes: 15 additions & 3 deletions test/pet-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@
(describe "pet-virtualenv-root"
:var ((project-root "/home/user/project/")
(conda-path "/usr/bin/conda")
(conda-env-dirs '("/home/user/miniforge3/envs" "/home/user/.conda/envs"))
(conda-virtualenv "/home/user/miniforge3/envs/project/")
(poetry-path "/usr/bin/poetry")
(poetry-virtualenv "/home/user/.cache/pypoetry/virtualenvs/project/")
Expand Down Expand Up @@ -755,11 +756,22 @@
(it "should return the absolute path of the virtualenv for a project using `conda'"
(spy-on 'pet-use-conda-p :and-return-value conda-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\"]}" conda-virtualenv)) 0))
(spy-on 'pet-environment :and-return-value `((prefix . ,conda-virtualenv)))
(spy-on 'call-process :and-call-fake
(lambda (&rest _)
(insert
(format "{\"envs_dirs\": [%s]}"
(string-join
(seq-map
(lambda (dir)
(format "\"%s\"" dir))
conda-env-dirs)
",")))
0))
(spy-on 'pet-environment :and-return-value `((name . "project")))
(spy-on 'file-directory-p :and-call-fake (lambda (filename) (equal filename 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 "env" "list" "--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 `poetry'"
(spy-on 'pet-use-conda-p :and-return-value nil)
Expand Down

0 comments on commit dc176e5

Please sign in to comment.