Skip to content

Commit

Permalink
Merge pull request #4 from msinamsina/3-space-in-path-make-an-error
Browse files Browse the repository at this point in the history
fix: space in path bug is fixed
  • Loading branch information
msinamsina authored Feb 27, 2024
2 parents 1dd782a + cb54ce3 commit 72aa212
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/pytest_pyvenv/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import platform
import shlex
import subprocess
import tempfile
from contextlib import contextmanager
Expand All @@ -8,6 +9,7 @@
from cookiecutter.utils import rmtree



@contextmanager
def inside_dir(dirpath):
"""
Expand Down Expand Up @@ -38,8 +40,7 @@ def run_inside_dir(command, dirpath):
Path of the directory the command is being run.
"""
with inside_dir(dirpath):
return subprocess.run(shlex.split(command),
shell=True, executable="/bin/bash")
return subprocess.run(shlex.split(command), shell=True)


@pytest.fixture
Expand All @@ -61,7 +62,9 @@ def __init__(self):
else:
raise FileNotFoundError("Python path not found")
print(f"Python path: {python_path}")
python_path = os.path.abspath(python_path)
self.python_path = python_path.replace(os.sep, "/")
venv_dir = os.path.abspath(venv_dir)
self.env_dir = venv_dir.replace(os.sep, "/")
if os.name == "nt":
# Windows
Expand All @@ -74,6 +77,8 @@ def __init__(self):
print(os.listdir(os.path.join(venv_dir, "bin")))
self.activator = self.activator

self.activator = f"\"{self.activator}\""

def __del__(self):
print(f"Deleting virtual environment in {self.env_dir}")
rmtree(self.env_dir)
Expand Down
1 change: 1 addition & 0 deletions tests/my pkg/myapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hello world')
12 changes: 12 additions & 0 deletions tests/my pkg/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "mypkg"
description = "A package for create venv in tests"
readme = "README.md"
version = "0.1.0"

[tool.setuptools]
py-modules = ['myapp']
38 changes: 37 additions & 1 deletion tests/test_venv_class.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from src.pytest_pyvenv import VenvClass
from src.pytest_pyvenv import VenvClass, run_inside_dir, inside_dir


def test_install_package():
Expand Down Expand Up @@ -35,3 +35,39 @@ def test_install_package_with_options():
["--no-deps"]).returncode == 0
# check pytest is installed
assert venv.run("pip show mypkg").returncode == 0


def test_space_in_package_path():
with VenvClass() as venv:
# check mypkg is not installed
print(venv.run("mypkg --version").stdout.decode("utf-8"))
assert venv.run("mypkg --version").returncode != 0
# install mypkg
assert venv.install_package("tests/my pkg").returncode == 0
# check pytest is installed
assert venv.run("pip show mypkg").returncode == 0
# check mypkg is installed
# uninstall mypkg
assert venv.run("pip uninstall -y mypkg").returncode == 0
# check mypkg is not installed
assert venv.run("mypkg --version").returncode != 0


def test_space_in_venv_path():
run_inside_dir("mkdir 'my venv'", "./tests")
with inside_dir("tests/my venv"):
with VenvClass() as venv:
print(venv.run("python --version").stderr.decode("utf-8"))
assert venv.run("python --version").returncode == 0
# check mypkg is not installed
print(venv.run("pip show mypkg").stdout.decode("utf-8"))
assert venv.run("pip show mypkg").returncode != 0
# install mypkg
assert venv.install_package("../my pkg").returncode == 0
# check pytest is installed
assert venv.run("pip show mypkg").returncode == 0
# check mypkg is installed
# uninstall mypkg
assert venv.run("pip uninstall -y mypkg").returncode == 0
# check mypkg is not installed
assert venv.run("mypkg --version").returncode != 0

0 comments on commit 72aa212

Please sign in to comment.