Skip to content
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

run: make run respect persist flag #1789

Merged
merged 1 commit into from
Mar 27, 2019
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
7 changes: 3 additions & 4 deletions dvc/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,6 @@ def reproduce(
if not self.changed() and not force:
return None

if (self.cmd or self.is_import) and not self.locked and not dry:
# Removing outputs only if we actually have command to reproduce
self.remove_outs(ignore_remove=False)

msg = (
"Going to reproduce '{stage}'. "
"Are you sure you want to continue?".format(stage=self.relpath)
Expand Down Expand Up @@ -734,6 +730,9 @@ def _run(self):
raise StageCmdFailedError(self)

def run(self, dry=False, resume=False, no_commit=False, force=False):
if self.cmd and not self.locked and not dry:
self.remove_outs(ignore_remove=False, force=False)

if self.locked:
logger.info(
"Verifying outputs in locked stage '{stage}'".format(
Expand Down
79 changes: 69 additions & 10 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def test(self):
)
self.assertEqual(ret, 0)
self.assertFalse(os.access(self.FOO, os.W_OK))
self.assertEqual(open(self.FOO, "r").read(), "foofoo")
self.assertEqual(open(self.FOO, "r").read(), "foo")

ret = main(
[
Expand All @@ -397,15 +397,14 @@ def test(self):
)
self.assertEqual(ret, 0)
self.assertFalse(os.access(self.FOO, os.W_OK))
self.assertEqual(open(self.FOO, "r").read(), "foofoofoo")
self.assertEqual(open(self.FOO, "r").read(), "foo")


class TestRunUnprotectOutsSymlink(TestDvc):
def test(self):
with open(self.CODE, "w+") as fobj:
fobj.write("import sys\n")
fobj.write("import os\n")
fobj.write("assert os.path.exists(sys.argv[1])\n")
fobj.write("with open(sys.argv[1], 'a+') as fobj:\n")
fobj.write(" fobj.write('foo')\n")

Expand All @@ -431,7 +430,7 @@ def test(self):
self.assertEqual(ret, 0)
self.assertFalse(os.access(self.FOO, os.W_OK))
self.assertTrue(System.is_symlink(self.FOO))
self.assertEqual(open(self.FOO, "r").read(), "foofoo")
self.assertEqual(open(self.FOO, "r").read(), "foo")

ret = main(
[
Expand All @@ -450,15 +449,14 @@ def test(self):
self.assertEqual(ret, 0)
self.assertFalse(os.access(self.FOO, os.W_OK))
self.assertTrue(System.is_symlink(self.FOO))
self.assertEqual(open(self.FOO, "r").read(), "foofoofoo")
self.assertEqual(open(self.FOO, "r").read(), "foo")


class TestRunUnprotectOutsHardlink(TestDvc):
def test(self):
with open(self.CODE, "w+") as fobj:
fobj.write("import sys\n")
fobj.write("import os\n")
fobj.write("assert os.path.exists(sys.argv[1])\n")
fobj.write("with open(sys.argv[1], 'a+') as fobj:\n")
fobj.write(" fobj.write('foo')\n")

Expand All @@ -484,7 +482,7 @@ def test(self):
self.assertEqual(ret, 0)
self.assertFalse(os.access(self.FOO, os.W_OK))
self.assertTrue(System.is_hardlink(self.FOO))
self.assertEqual(open(self.FOO, "r").read(), "foofoo")
self.assertEqual(open(self.FOO, "r").read(), "foo")

ret = main(
[
Expand All @@ -503,7 +501,7 @@ def test(self):
self.assertEqual(ret, 0)
self.assertFalse(os.access(self.FOO, os.W_OK))
self.assertTrue(System.is_hardlink(self.FOO))
self.assertEqual(open(self.FOO, "r").read(), "foofoofoo")
self.assertEqual(open(self.FOO, "r").read(), "foo")


class TestCmdRunOverwrite(TestDvc):
Expand Down Expand Up @@ -755,13 +753,13 @@ class TestRunCommit(TestDvc):
def test(self):
fname = "test"
ret = main(
["run", "-o", self.FOO, "--no-commit", "echo", "test", ">", fname]
["run", "-o", fname, "--no-commit", "echo", "test", ">", fname]
)
self.assertEqual(ret, 0)
self.assertTrue(os.path.isfile(fname))
self.assertEqual(len(os.listdir(self.dvc.cache.local.cache_dir)), 0)

ret = main(["commit", self.FOO + ".dvc"])
ret = main(["commit", fname + ".dvc"])
self.assertEqual(ret, 0)
self.assertTrue(os.path.isfile(fname))
self.assertEqual(len(os.listdir(self.dvc.cache.local.cache_dir)), 1)
Expand Down Expand Up @@ -861,3 +859,64 @@ def test(self):
"command with non overlapping outs paths.",
error_output,
)


class TestRerunWithSameOutputs(TestDvc):
def _read_content_only(self, path):
with open(path, "r") as fobj:
return [line.rstrip() for line in fobj.readlines()]

@property
def _outs_command(self):
raise NotImplementedError

def _run_twice_with_same_outputs(self):
ret = main(
[
"run",
"--outs",
self.FOO,
"echo {} > {}".format(self.FOO_CONTENTS, self.FOO),
]
)
self.assertEqual(0, ret)

output_file_content = self._read_content_only(self.FOO)
self.assertEqual([self.FOO_CONTENTS], output_file_content)

ret = main(
[
"run",
self._outs_command,
self.FOO,
"--overwrite-dvcfile",
"echo {} >> {}".format(self.BAR_CONTENTS, self.FOO),
]
)
self.assertEqual(0, ret)


class TestNewRunShouldRemoveOutsOnNoPersist(TestRerunWithSameOutputs):
def test(self):
self._run_twice_with_same_outputs()

output_file_content = self._read_content_only(self.FOO)
self.assertEqual([self.BAR_CONTENTS], output_file_content)

@property
def _outs_command(self):
return "--outs"


class TestNewRunShouldNotRemoveOutsOnPersist(TestRerunWithSameOutputs):
def test(self):
self._run_twice_with_same_outputs()

output_file_content = self._read_content_only(self.FOO)
self.assertEqual(
[self.FOO_CONTENTS, self.BAR_CONTENTS], output_file_content
)

@property
def _outs_command(self):
return "--outs-persist"