Skip to content

Commit

Permalink
Fix environment detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Nov 21, 2018
1 parent c2c6f3b commit 94304e9
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

- Fixed `run` not executing scripts.
- Fixed environment detection.


## [0.12.9] - 2018-11-19
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/debug/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def handle(self):
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])

Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/debug/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def handle(self):

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)
)
Expand Down
4 changes: 2 additions & 2 deletions poetry/console/commands/env_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(self, i, o):

# 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])
Expand All @@ -30,7 +30,7 @@ def initialize(self, i, o):
)

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():
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def handle(self):
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])
)
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def handle(self):
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])
)
Expand Down
2 changes: 1 addition & 1 deletion poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def search_for_directory(

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)
Expand Down
17 changes: 4 additions & 13 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def pip(self): # type: () -> str
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

Expand All @@ -162,7 +162,7 @@ def get(cls, reload=False, cwd=None): # type: (bool, Path) -> Env

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)
Expand All @@ -179,9 +179,6 @@ def get(cls, reload=False, cwd=None): # type: (bool, Path) -> Env
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]])
Expand All @@ -204,11 +201,11 @@ def get(cls, reload=False, cwd=None): # type: (bool, Path) -> Env
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
Expand All @@ -220,19 +217,13 @@ def create_venv(cls, io, name=None, cwd=None): # type: (IO, bool, Path) -> Env

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"
else:
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]]))
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest
import shutil
import tempfile
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from poetry.utils._compat import Path
from poetry.utils.env import Env
from poetry.utils.env import VirtualEnv
Expand All @@ -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"

0 comments on commit 94304e9

Please sign in to comment.