Skip to content

Commit

Permalink
Add pathlib support
Browse files Browse the repository at this point in the history
  • Loading branch information
jdufresne committed Dec 31, 2020
1 parent 3eeb083 commit 107c1dc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion piptools/repositories/pypi.py
Expand Up @@ -86,7 +86,7 @@ def __init__(self, pip_args, cache_dir):
# Setup file paths
self._build_dir = None
self._source_dir = None
self._cache_dir = normalize_path(cache_dir)
self._cache_dir = normalize_path(str(cache_dir))
self._download_dir = os.path.join(self._cache_dir, "pkgs")
if PIP_VERSION[:2] <= (20, 2):
self._wheel_download_dir = os.path.join(self._cache_dir, "wheels")
Expand Down
15 changes: 6 additions & 9 deletions tests/conftest.py
Expand Up @@ -135,13 +135,13 @@ def repository():
def pypi_repository(tmpdir):
return PyPIRepository(
["--index-url", PyPIRepository.DEFAULT_INDEX_URL],
cache_dir=str(tmpdir / "pypi-repo"),
cache_dir=(tmpdir / "pypi-repo"),
)


@pytest.fixture
def depcache(tmpdir):
return DependencyCache(str(tmpdir / "dep-cache"))
return DependencyCache(tmpdir / "dep-cache")


@pytest.fixture
Expand Down Expand Up @@ -246,8 +246,7 @@ def _make_package(name, version="0.1", install_requires=None):
package_dir = tmp_path / "packages" / name / version
package_dir.mkdir(parents=True)

setup_file = str(package_dir / "setup.py")
with open(setup_file, "w") as fp:
with (package_dir / "setup.py").open("w") as fp:
fp.write(
dedent(
f"""\
Expand All @@ -265,9 +264,7 @@ def _make_package(name, version="0.1", install_requires=None):
)

# Create a README to avoid setuptools warnings.
readme_file = str(package_dir / "README")
with open(readme_file, "w"):
pass
(package_dir / "README").touch()

return package_dir

Expand All @@ -281,9 +278,9 @@ def run_setup_file():
"""

def _run_setup_file(package_dir_path, *args):
setup_file = str(package_dir_path / "setup.py")
setup_file = package_dir_path / "setup.py"
return subprocess.run(
[sys.executable, setup_file, *args],
[sys.executable, str(setup_file), *args],
cwd=str(package_dir_path),
stdout=subprocess.DEVNULL,
check=True,
Expand Down
11 changes: 4 additions & 7 deletions tests/test_cache.py
Expand Up @@ -64,28 +64,25 @@ def test_read_cache_file_successful():


def test_read_cache_does_not_exist(tmpdir):
cache = DependencyCache(cache_dir=str(tmpdir))
cache = DependencyCache(cache_dir=tmpdir)
assert cache.cache == {}


@pytest.mark.skipif(
sys.platform == "win32", reason="os.fchmod() not available on Windows"
)
def test_read_cache_permission_error(tmpdir):
cache = DependencyCache(cache_dir=str(tmpdir))
cache = DependencyCache(cache_dir=tmpdir)
with open(cache._cache_file, "w") as fp:
os.fchmod(fp.fileno(), 0o000)
with pytest.raises(IOError, match="Permission denied"):
cache.read_cache()


def test_reverse_dependencies(from_line, tmpdir):
# Since this is a test, make a temporary directory. Converting to str from py.path.
tmp_dir_path = str(tmpdir)

# Create a cache object. The keys are packages, and the values are lists
# of packages on which the keys depend.
cache = DependencyCache(cache_dir=tmp_dir_path)
cache = DependencyCache(cache_dir=tmpdir)
cache[from_line("top==1.2")] = ["middle>=0.3", "bottom>=5.1.2"]
cache[from_line("top[xtra]==1.2")] = ["middle>=0.3", "bottom>=5.1.2", "bonus==0.4"]
cache[from_line("middle==0.4")] = ["bottom<6"]
Expand Down Expand Up @@ -121,4 +118,4 @@ def test_reverse_dependencies(from_line, tmpdir):
}

# Clean up our temp directory
rmtree(tmp_dir_path)
rmtree(tmpdir)
8 changes: 4 additions & 4 deletions tests/test_repository_pypi.py
Expand Up @@ -160,7 +160,7 @@ def test_relative_path_cache_dir_is_normalized(from_line):
def test_relative_path_pip_cache_dir_is_normalized(from_line, tmpdir):
relative_cache_dir = "pip-cache"
pypi_repository = PyPIRepository(
["--cache-dir", relative_cache_dir], cache_dir=str(tmpdir / "pypi-repo-cache")
["--cache-dir", relative_cache_dir], cache_dir=(tmpdir / "pypi-repo-cache")
)

assert os.path.isabs(pypi_repository.options.cache_dir)
Expand All @@ -169,7 +169,7 @@ def test_relative_path_pip_cache_dir_is_normalized(from_line, tmpdir):

def test_pip_cache_dir_is_empty(from_line, tmpdir):
pypi_repository = PyPIRepository(
["--no-cache-dir"], cache_dir=str(tmpdir / "pypi-repo-cache")
["--no-cache-dir"], cache_dir=(tmpdir / "pypi-repo-cache")
)

assert not pypi_repository.options.cache_dir
Expand Down Expand Up @@ -264,7 +264,7 @@ def _get_project(self, ireq):
return project_data

pypi_repository = MockPyPIRepository(
["--no-cache-dir"], cache_dir=str(tmpdir / "pypi-repo-cache")
["--no-cache-dir"], cache_dir=(tmpdir / "pypi-repo-cache")
)
ireq = from_line("fake-package==0.1")

Expand Down Expand Up @@ -372,7 +372,7 @@ def test_name_collision(from_line, pypi_repository, make_package, make_sdist, tm
}

for pkg_name, pkg in packages.items():
pkg_path = str(tmpdir / pkg_name)
pkg_path = tmpdir / pkg_name

make_sdist(pkg, pkg_path, "--formats=zip")

Expand Down
2 changes: 1 addition & 1 deletion tests/test_top_level_editable.py
Expand Up @@ -19,7 +19,7 @@ def get_dependencies(self, ireq):

@pytest.fixture
def mocked_repository(tmpdir):
return MockedPyPIRepository(["--no-index"], cache_dir=str(tmpdir / "pypi-repo"))
return MockedPyPIRepository(["--no-index"], cache_dir=(tmpdir / "pypi-repo"))


def test_editable_top_level_deps_preserved(
Expand Down

0 comments on commit 107c1dc

Please sign in to comment.