From 8984e9fed49671e7f8831fcd20f908f9ff82266d Mon Sep 17 00:00:00 2001 From: Alexander Schepanovski Date: Thu, 22 Aug 2019 18:41:46 +0700 Subject: [PATCH] dvc: use wdir == "." on add Fixes #2404 --- dvc/stage.py | 19 ++++++++++++++----- tests/func/test_add.py | 20 +++++++++++--------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/dvc/stage.py b/dvc/stage.py index 373de499e..f91aff25d 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -9,6 +9,7 @@ import subprocess import logging import signal import threading +from itertools import chain from dvc.utils import relpath from dvc.utils.compat import pathlib @@ -438,6 +439,7 @@ class Stage(object): wdir = kwargs.get("wdir", None) cwd = kwargs.get("cwd", None) fname = kwargs.get("fname", None) + add = kwargs.get("add", False) # Backward compatibility for `cwd` option if wdir is None and cwd is not None: @@ -449,8 +451,8 @@ class Stage(object): " directory.".format(fname=fname) ) wdir = cwd - else: - wdir = os.curdir if wdir is None else wdir + elif wdir is None: + wdir = os.curdir stage = Stage( repo=repo, @@ -468,11 +470,18 @@ class Stage(object): stage._check_duplicated_arguments() if not fname: - fname = Stage._stage_fname( - stage.outs, add=kwargs.get("add", False) - ) + fname = Stage._stage_fname(stage.outs, add) stage._check_dvc_filename(fname) + # Autodetecting wdir for add, we need to create outs first to do that, + # so we start with wdir = . and remap out paths later. + if add and kwargs.get("wdir") is None and cwd is None: + wdir = os.path.dirname(fname) + + for out in chain(stage.outs, stage.deps): + if out.is_in_repo: + out.def_path = relpath(out.path_info, wdir) + wdir = os.path.abspath(wdir) if cwd is not None: diff --git a/tests/func/test_add.py b/tests/func/test_add.py index 2c1aa2927..a9c52185c 100644 --- a/tests/func/test_add.py +++ b/tests/func/test_add.py @@ -169,15 +169,17 @@ class TestAddModifiedDir(TestDvc): self.assertTrue(stages[0] is not None) -class TestAddFileInDir(TestDvc): - def test(self): - stages = self.dvc.add(self.DATA_SUB) - self.assertEqual(len(stages), 1) - stage = stages[0] - self.assertNotEqual(stage, None) - self.assertEqual(len(stage.deps), 0) - self.assertEqual(len(stage.outs), 1) - self.assertEqual(stage.relpath, self.DATA_SUB + ".dvc") +def test_add_file_in_dir(repo_dir, dvc_repo): + stage, = dvc_repo.add(repo_dir.DATA_SUB) + + assert stage is not None + assert len(stage.deps) == 0 + assert len(stage.outs) == 1 + assert stage.relpath == repo_dir.DATA_SUB + ".dvc" + + # Current dir should not be taken into account + assert stage.wdir == os.path.dirname(stage.path) + assert stage.outs[0].def_path == "data_sub" class TestAddExternalLocalFile(TestDvc):