Skip to content

Convert all paths in config to posix-style #3665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions dvc/config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
"""DVC config objects."""
from contextlib import contextmanager
import logging
import os
import re
from contextlib import contextmanager
from functools import partial
from urllib.parse import urlparse

from funcy import cached_property, re_find, walk_values, compact
import configobj
from voluptuous import Schema, Optional, Invalid, ALLOW_EXTRA
from voluptuous import All, Any, Lower, Range, Coerce
from funcy import cached_property, compact, re_find, walk_values
from voluptuous import (
ALLOW_EXTRA,
All,
Any,
Coerce,
Invalid,
Lower,
Optional,
Range,
Schema,
)

from dvc.exceptions import DvcException, NotDvcRepoError
from dvc.path_info import PathInfo
from dvc.utils import relpath

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -312,16 +323,19 @@ def resolve(path):
return Config._map_dirs(conf, resolve)

@staticmethod
def _save_paths(conf, filename):
conf_dir = os.path.dirname(filename)
def _to_relpath(conf_dir, path):
if re.match(r"\w+://", path):
return path

def rel(path):
if re.match(r"\w+://", path):
return path
if isinstance(path, RelPath) or not os.path.isabs(path):
path = relpath(path, conf_dir)

if isinstance(path, RelPath) or not os.path.isabs(path):
return relpath(path, conf_dir)
return path
return PathInfo(path).as_posix()

@staticmethod
def _save_paths(conf, filename):
conf_dir = os.path.dirname(filename)
rel = partial(Config._to_relpath, conf_dir)

return Config._map_dirs(conf, rel)

Expand Down
7 changes: 3 additions & 4 deletions tests/func/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from dvc.main import main
from dvc.remote.base import DirCacheError
from dvc.utils import relpath
from tests.basic_env import TestDir
from tests.basic_env import TestDvc
from tests.basic_env import TestDir, TestDvc


class TestCache(TestDvc):
Expand Down Expand Up @@ -155,7 +154,7 @@ def test_abs_path(self):
self.assertEqual(ret, 0)

config = configobj.ConfigObj(self.dvc.config.files["repo"])
self.assertEqual(config["cache"]["dir"], dname)
self.assertEqual(config["cache"]["dir"], dname.replace("\\", "/"))

def test_relative_path(self):
tmpdir = self.mkdtemp()
Expand All @@ -167,7 +166,7 @@ def test_relative_path(self):
# dir path written to config should be just one level above.
rel = os.path.join("..", dname)
config = configobj.ConfigObj(self.dvc.config.files["repo"])
self.assertEqual(config["cache"]["dir"], rel)
self.assertEqual(config["cache"]["dir"], rel.replace("\\", "/"))

ret = main(["add", self.FOO])
self.assertEqual(ret, 0)
Expand Down
4 changes: 3 additions & 1 deletion tests/func/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def test_relative_path(self):
# dir path written to config should be just one level above.
rel = os.path.join("..", dname)
config = configobj.ConfigObj(self.dvc.config.files["repo"])
self.assertEqual(config['remote "mylocal"']["url"], rel)
self.assertEqual(
config['remote "mylocal"']["url"], rel.replace("\\", "/")
)

def test_overwrite(self):
remote_name = "a"
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import pytest

from dvc.config import Config


@pytest.mark.parametrize(
"path, expected",
[
("cache", "../cache"),
(os.path.join("..", "cache"), "../../cache"),
(os.getcwd(), os.getcwd().replace("\\", "/")),
("ssh://some/path", "ssh://some/path"),
],
)
def test_to_relpath(path, expected):
assert Config._to_relpath(os.path.join(".", "config"), path) == expected