From 0385810662049abf4ccd04dedf2880e951ad252f Mon Sep 17 00:00:00 2001 From: Alexander Schepanovski Date: Fri, 29 Nov 2019 21:48:35 +0100 Subject: [PATCH] test: fix python 2, 3.5 and mac issues with new fixtures --- dvc/utils/compat.py | 2 -- tests/dir_helpers.py | 44 ++++++++++++++++++++++----------------- tests/func/test_api.py | 4 +++- tests/func/test_commit.py | 2 ++ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/dvc/utils/compat.py b/dvc/utils/compat.py index 9d979f391..372c8d2c6 100644 --- a/dvc/utils/compat.py +++ b/dvc/utils/compat.py @@ -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) diff --git a/tests/dir_helpers.py b/tests/dir_helpers.py index d3ce382e3..39e28d4d7 100644 --- a/tests/dir_helpers.py +++ b/tests/dir_helpers.py @@ -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"] @@ -29,6 +32,10 @@ class TmpDir(pathlib.Path): 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( @@ -37,25 +44,24 @@ class TmpDir(pathlib.Path): ) 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) @@ -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 @@ -183,7 +189,7 @@ def run_copy(tmp_dir, dvc, request): cmd="python copy.py {} {}".format(src, dst), outs=[dst], deps=[src, "copy.py"], - **run_kwargs, + **run_kwargs ) return run_copy @@ -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") diff --git a/tests/func/test_api.py b/tests/func/test_api.py index e9d18eeff..166d4e4e1 100644 --- a/tests/func/test_api.py +++ b/tests/func/test_api.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import os import shutil @@ -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" diff --git a/tests/func/test_commit.py b/tests/func/test_commit.py index 46be53d29..c3238e6ad 100644 --- a/tests/func/test_commit.py +++ b/tests/func/test_commit.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import pytest from dvc.stage import StageCommitError