Commit 94304e9d by Sébastien Eustace

Fix environment detection

parent c2c6f3be
......@@ -5,6 +5,7 @@
### Fixed
- Fixed `run` not executing scripts.
- Fixed environment detection.
## [0.12.9] - 2018-11-19
......
......@@ -15,7 +15,7 @@ class DebugInfoCommand(Command):
from ....utils.env import Env
poetry = self.poetry
env = Env.get(cwd=poetry.file.parent)
env = Env.get(poetry.file.parent)
poetry_python_version = ".".join(str(s) for s in sys.version_info[:3])
......
......@@ -79,7 +79,7 @@ class DebugResolveCommand(Command):
return 0
env = Env.get()
env = Env.get(self.poetry.file.parent)
current_python_version = parse_constraint(
".".join(str(v) for v in env.version_info)
)
......
......@@ -15,7 +15,7 @@ class EnvCommand(Command):
# Checking compatibility of the current environment with
# the python dependency specified in pyproject.toml
current_env = Env.get()
current_env = Env.get(self.poetry.file.parent)
supported_python = self.poetry.package.python_constraint
current_python = parse_constraint(
".".join(str(v) for v in current_env.version_info[:3])
......@@ -30,7 +30,7 @@ class EnvCommand(Command):
)
self._env = Env.create_venv(
o, self.poetry.package.name, cwd=self.poetry.file.parent
self.poetry.file.parent, o, self.poetry.package.name
)
if self._env.is_venv() and o.is_verbose():
......
......@@ -102,7 +102,7 @@ The <info>init</info> command creates a basic <comment>pyproject.toml</> file in
question.validator = self._validate_license
license = self.ask(question)
current_env = Env.get()
current_env = Env.get(Path.cwd())
default_python = "^{}".format(
".".join(str(v) for v in current_env.version_info[:2])
)
......
......@@ -45,7 +45,7 @@ class NewCommand(Command):
if author_email:
author += " <{}>".format(author_email)
current_env = Env.get()
current_env = Env.get(Path.cwd())
default_python = "^{}".format(
".".join(str(v) for v in current_env.version_info[:2])
)
......
......@@ -275,7 +275,7 @@ class Provider:
try:
cwd = dependency.full_path
venv = Env.get(NullIO(), cwd=cwd)
venv = Env.get(cwd)
venv.run("python", "setup.py", "egg_info")
except EnvCommandError:
result = SetupReader.read_from_directory(dependency.full_path)
......
......@@ -153,7 +153,7 @@ class Env(object):
return self._bin("pip")
@classmethod
def get(cls, reload=False, cwd=None): # type: (bool, Path) -> Env
def get(cls, cwd, reload=False): # type: (Path, bool) -> Env
if cls._env is not None and not reload:
return cls._env
......@@ -162,7 +162,7 @@ class Env(object):
if not in_venv:
# Checking if a local virtualenv exists
if cwd and (cwd / ".venv").exists():
if (cwd / ".venv").exists():
venv = cwd / ".venv"
return VirtualEnv(venv)
......@@ -179,9 +179,6 @@ class Env(object):
else:
venv_path = Path(venv_path)
if cwd is None:
cwd = Path.cwd()
name = cwd.name
name = "{}-py{}".format(
name, ".".join([str(v) for v in sys.version_info[:2]])
......@@ -204,11 +201,11 @@ class Env(object):
return VirtualEnv(prefix, base_prefix)
@classmethod
def create_venv(cls, io, name=None, cwd=None): # type: (IO, bool, Path) -> Env
def create_venv(cls, cwd, io, name=None): # type: (Path, IO, bool) -> Env
if cls._env is not None:
return cls._env
env = cls.get(cwd=cwd)
env = cls.get(cwd)
if env.is_venv():
# Already inside a virtualenv.
return env
......@@ -220,9 +217,6 @@ class Env(object):
venv_path = config.setting("settings.virtualenvs.path")
if root_venv:
if not cwd:
raise RuntimeError("Unable to determine the project's directory")
venv_path = cwd / ".venv"
elif venv_path is None:
venv_path = Path(CACHE_DIR) / "virtualenvs"
......@@ -230,9 +224,6 @@ class Env(object):
venv_path = Path(venv_path)
if not name:
if not cwd:
cwd = Path.cwd()
name = cwd.name
name = "{}-py{}".format(name, ".".join([str(v) for v in sys.version_info[:2]]))
......
import os
import pytest
import shutil
import tempfile
......@@ -45,6 +46,15 @@ def mock_clone(_, source, dest):
shutil.copytree(str(folder), str(dest))
@pytest.fixture
def environ():
original_environ = os.environ
yield os.environ
os.environ = original_environ
@pytest.fixture(autouse=True)
def git_mock(mocker):
# Patch git module to not actually clone projects
......
import os
from poetry.utils._compat import Path
from poetry.utils.env import Env
from poetry.utils.env import VirtualEnv
......@@ -11,3 +13,14 @@ def test_virtualenvs_with_spaces_in_their_path_work_as_expected(tmp_dir):
venv = VirtualEnv(venv_path)
assert venv.run("python", "-V", shell=True).startswith("Python")
def test_env_get_in_project_venv(tmp_dir, environ):
if "VIRTUAL_ENV" in environ:
del environ["VIRTUAL_ENV"]
(Path(tmp_dir) / ".venv").mkdir()
venv = Env.get(cwd=Path(tmp_dir))
assert venv.path == Path(tmp_dir) / ".venv"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment