Skip to content

Commit

Permalink
fix-conda-temp-file-permission-error (#161)
Browse files Browse the repository at this point in the history
* fix-conda-temp-file-permission-error

* Fix test for conda env creation

* Bump pre-commit isort version to fix poetry dependency issue

* Fix basepython for pkg_meta target

---------

Co-authored-by: Kuan Wee Heng <kuanwee.heng@sgx.com>
Co-authored-by: Antoine Dechaume <AntoineD@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 13, 2023
1 parent 099ec51 commit 2170e1e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
args:
- --py3-plus
- repo: https://github.com/PyCQA/isort
rev: 5.11.4
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
Expand Down
17 changes: 11 additions & 6 deletions tests/test_conda_env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import os
import pathlib
import re
from unittest.mock import mock_open, patch

Expand Down Expand Up @@ -278,10 +279,12 @@ def test_conda_env(tmpdir, newconfig, mocksession):

mock_file = mock_open()
with patch("tox_conda.plugin.tempfile.NamedTemporaryFile", mock_file):
with mocksession.newaction(venv.name, "getenv") as action:
tox_testenv_create(action=action, venv=venv)
with patch.object(pathlib.Path, "unlink", autospec=True) as mock_unlink:
with mocksession.newaction(venv.name, "getenv") as action:
tox_testenv_create(action=action, venv=venv)
mock_unlink.assert_called_once

mock_file.assert_called_with(dir=tmpdir, prefix="tox_conda_tmp", suffix=".yaml")
mock_file.assert_called_with(dir=tmpdir, prefix="tox_conda_tmp", suffix=".yaml", delete=False)

pcalls = mocksession._pcalls
assert len(pcalls) >= 1
Expand Down Expand Up @@ -335,10 +338,12 @@ def test_conda_env_and_spec(tmpdir, newconfig, mocksession):

mock_file = mock_open()
with patch("tox_conda.plugin.tempfile.NamedTemporaryFile", mock_file):
with mocksession.newaction(venv.name, "getenv") as action:
tox_testenv_create(action=action, venv=venv)
with patch.object(pathlib.Path, "unlink", autospec=True) as mock_unlink:
with mocksession.newaction(venv.name, "getenv") as action:
tox_testenv_create(action=action, venv=venv)
mock_unlink.assert_called_once

mock_file.assert_called_with(dir=tmpdir, prefix="tox_conda_tmp", suffix=".yaml")
mock_file.assert_called_with(dir=tmpdir, prefix="tox_conda_tmp", suffix=".yaml", delete=False)

pcalls = mocksession._pcalls
assert len(pcalls) >= 1
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ depends =

[testenv:pkg_meta]
description = check that the long description is valid
basepython = python3.9
basepython = python3.10
skip_install = true
deps =
build>=0.0.4
Expand Down
37 changes: 21 additions & 16 deletions tox_conda/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,27 @@ def tox_testenv_create(venv, action):
env_file = yaml.load(env_path)
env_file["dependencies"].append(python)

with tempfile.NamedTemporaryFile(
dir=env_path.parent, prefix="tox_conda_tmp", suffix=".yaml"
) as tmp_env:
yaml.dump(env_file, tmp_env)

args = [
venv.envconfig.conda_exe,
"env",
"create",
"-p",
envdir,
"--file",
tmp_env.name,
]

_run_conda_process(args, venv, action, basepath)
tmp_env = tempfile.NamedTemporaryFile(
dir=env_path.parent,
prefix="tox_conda_tmp",
suffix=".yaml",
delete=False,
)
yaml.dump(env_file, tmp_env)

args = [
venv.envconfig.conda_exe,
"env",
"create",
"-p",
envdir,
"--file",
tmp_env.name,
]
tmp_env.close()
_run_conda_process(args, venv, action, basepath)
Path(tmp_env.name).unlink()

else:
args = [venv.envconfig.conda_exe, "create", "--yes", "-p", envdir]
for channel in venv.envconfig.conda_channels:
Expand Down

1 comment on commit 2170e1e

@saeed15153178
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gf

Please sign in to comment.