Skip to content

Commit

Permalink
test: fix python 2, 3.5 and mac issues with new fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Suor committed Dec 2, 2019
1 parent fcebad8 commit 0385810
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
2 changes: 0 additions & 2 deletions dvc/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ def _makedirs(name, mode=0o777, exist_ok=False):
"""Source: https://github.com/python/cpython/blob/
3ce3dea60646d8a5a1c952469a2eb65f937875b3/Lib/os.py#L196-L226
"""
name = fspath_py35(name)

head, tail = os.path.split(name)
if not tail:
head, tail = os.path.split(head)
Expand Down
44 changes: 25 additions & 19 deletions tests/dir_helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import unicode_literals

import os
import pytest

from funcy import lmap, retry
from funcy.py3 import lmap, retry

from dvc.utils.compat import pathlib, fspath, makedirs, basestring
from dvc.utils import makedirs
from dvc.utils.compat import basestring, is_py2, pathlib, fspath, fspath_py35


__all__ = ["tmp_dir", "scm", "dvc", "repo_template", "run_copy", "erepo_dir"]
Expand All @@ -29,6 +32,10 @@ def __new__(cls, *args, **kwargs):
self._init()
return self

# Not needed in Python 3.6+
def __fspath__(self):
return str(self)

def _require(self, name):
if not hasattr(self, name):
raise TypeError(
Expand All @@ -37,25 +44,24 @@ def _require(self, name):
)

def gen(self, struct, text=""):
if isinstance(struct, str):
if isinstance(struct, basestring):
struct = {struct: text}

return self._gen(struct)
self._gen(struct)
return struct.keys()

def _gen(self, struct, prefix=None):
paths = []

for name, contents in struct.items():
path = (prefix or self) / name

if isinstance(contents, dict):
paths.extend(self._gen(contents, prefix=path))
self._gen(contents, prefix=path)
else:
makedirs(path.parent, exist_ok=True)
path.write_text(contents)
paths.append(path.relative_to(self))

return paths
if is_py2 and isinstance(contents, str):
path.write_bytes(contents)
else:
path.write_text(contents)

def dvc_gen(self, struct, text="", commit=None):
paths = self.gen(struct, text)
Expand Down Expand Up @@ -106,7 +112,7 @@ class PosixTmpDir(TmpDir, pathlib.PurePosixPath):
@pytest.fixture
def tmp_dir(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
return TmpDir(tmp_path)
return TmpDir(fspath_py35(tmp_path))


@pytest.fixture
Expand Down Expand Up @@ -183,7 +189,7 @@ def run_copy(src, dst, **run_kwargs):
cmd="python copy.py {} {}".format(src, dst),
outs=[dst],
deps=[src, "copy.py"],
**run_kwargs,
**run_kwargs
)

return run_copy
Expand All @@ -194,25 +200,25 @@ def erepo_dir(tmp_path_factory, monkeypatch):
from dvc.repo import Repo
from dvc.remote.config import RemoteConfig

path = TmpDir(tmp_path_factory.mktemp("erepo"))
path.gen(REPO_TEMPLATE)
path = TmpDir(fspath_py35(tmp_path_factory.mktemp("erepo")))

# Chdir for git and dvc to work locally
monkeypatch.chdir(path)
monkeypatch.chdir(fspath_py35(path))

_git_init()
path.dvc = Repo.init()
path.scm = path.dvc.scm

path.dvc_add(["foo", "bar", "dir"], commit="init repo")
path.dvc_gen(REPO_TEMPLATE, commit="init repo")

rconfig = RemoteConfig(path.dvc.config)
rconfig.add("upstream", path.dvc.cache.local.cache_dir, default=True)
path.scm_add([path.dvc.config.config_file], commit="add remote")

path.dvc_gen("version", "master", commit="master")
path.dvc_gen("version", "master")
path.scm_add([".gitignore", "version.dvc"], commit="master")

path.scm.checkout("branch", create_new=True)
(path / "version").unlink() # For mac ???
path.dvc_gen("version", "branch")
path.scm_add([".gitignore", "version.dvc"], commit="branch")

Expand Down
4 changes: 3 additions & 1 deletion tests/func/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

import os
import shutil

Expand Down Expand Up @@ -106,7 +108,7 @@ def test_open_scm_controlled(dvc_repo, repo_dir):
assert fd.read() == stage_content


# TODO: simplify, we shouldn't need run. This also duplicates the previous one.
# TODO: simplify, we shouldn't need run.
def test_open_not_cached(dvc):
metric_file = "metric.txt"
metric_content = "0.6"
Expand Down
2 changes: 2 additions & 0 deletions tests/func/test_commit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

import pytest

from dvc.stage import StageCommitError
Expand Down

0 comments on commit 0385810

Please sign in to comment.