From 6d61e627cb5d572adc05e8122faa09e38b5288fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Redzy=C5=84ski?= Date: Thu, 9 Jan 2020 12:56:06 +0100 Subject: [PATCH 1/3] run: tests: migrate to helpers all besides deterministic --- tests/func/test_run.py | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/tests/func/test_run.py b/tests/func/test_run.py index 46bf0f1117..7412b01f0f 100644 --- a/tests/func/test_run.py +++ b/tests/func/test_run.py @@ -931,33 +931,24 @@ def test_ignore_build_cache(self): assert "hello\nhello\n" == fobj.read() -def test_bad_stage_fname(repo_dir, dvc_repo): - dvc_repo.add(repo_dir.FOO) +def test_bad_stage_fname(tmp_dir, dvc, run_copy): + tmp_dir.dvc_gen("foo", "foo content") + with pytest.raises(StageFileBadNameError): - dvc_repo.run( - cmd="python {} {} {}".format(repo_dir.CODE, repo_dir.FOO, "out"), - deps=[repo_dir.FOO, repo_dir.CODE], - outs=["out"], - fname="out_stage", # Bad name, should end with .dvc - ) + # fname should end with .dvc + run_copy("foo", "foo_copy", fname="out_stage") # Check that command hasn't been run - assert not os.path.exists("out") + assert not (tmp_dir / "foo_copy").exists() -def test_should_raise_on_stage_dependency(repo_dir, dvc_repo): +def test_should_raise_on_stage_dependency(run_copy): with pytest.raises(DependencyIsStageFileError): - dvc_repo.run( - cmd="python {} {} {}".format(repo_dir.CODE, repo_dir.FOO, "out"), - deps=[repo_dir.FOO, "name.dvc"], - outs=["out"], - ) + run_copy("name.dvc", "stage_copy") -def test_should_raise_on_stage_output(repo_dir, dvc_repo): +def test_should_raise_on_stage_output(tmp_dir, dvc, run_copy): + tmp_dir.dvc_gen("foo", "foo content") + with pytest.raises(OutputIsStageFileError): - dvc_repo.run( - cmd="python {} {} {}".format(repo_dir.CODE, repo_dir.FOO, "out"), - deps=[repo_dir.FOO], - outs=["name.dvc"], - ) + run_copy("foo", "name.dvc") From f62aeae6d1b6b652a6796ebdc91f71ddb4974735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Redzy=C5=84ski?= Date: Thu, 9 Jan 2020 13:06:37 +0100 Subject: [PATCH 2/3] run: tests: clumsy deterministic_run migration --- tests/func/test_run.py | 45 ++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/tests/func/test_run.py b/tests/func/test_run.py index 7412b01f0f..74cea42be1 100644 --- a/tests/func/test_run.py +++ b/tests/func/test_run.py @@ -1,8 +1,8 @@ import filecmp import logging import os -import shutil import uuid +from pathlib import Path import mock import pytest @@ -612,21 +612,25 @@ def test_cwd_is_ignored(self): class DeterministicRunBaseFixture(object): - def __init__(self, repo_dir, dvc_repo): + def __init__(self, tmp_dir, dvc): + tmp_dir.gen( + "copy.py", + "import sys, shutil\nshutil.copyfile(sys.argv[1], sys.argv[2])", + ) + tmp_dir.gen("foo", "foo content") + self.out_file = "out" self.stage_file = self.out_file + ".dvc" - self.cmd = "python {} {} {}".format( - repo_dir.CODE, repo_dir.FOO, self.out_file - ) - self.deps = [repo_dir.FOO, repo_dir.CODE] + self.cmd = "python {} {} {}".format("copy.py", "foo", self.out_file) + self.deps = ["foo", "copy.py"] self.outs = [self.out_file] self.overwrite = False self.ignore_build_cache = False - self.dvc_repo = dvc_repo + self.dvc = dvc self.stage = None def run(self): - self.stage = self.dvc_repo.run( + self.stage = self.dvc.run( cmd=self.cmd, fname=self.stage_file, overwrite=self.overwrite, @@ -638,8 +642,8 @@ def run(self): @pytest.fixture -def deterministic_run(dvc_repo, repo_dir): - run_base = DeterministicRunBaseFixture(repo_dir, dvc_repo) +def deterministic_run(tmp_dir, dvc): + run_base = DeterministicRunBaseFixture(tmp_dir, dvc) run_base.run() yield run_base @@ -663,27 +667,30 @@ def test_run_deterministic_callback(deterministic_run): assert deterministic_run.run() -def test_run_deterministic_changed_dep(deterministic_run, repo_dir): - os.unlink(repo_dir.FOO) - shutil.copy(repo_dir.BAR, repo_dir.FOO) +def test_run_deterministic_changed_dep(deterministic_run): + foo = Path("foo") + foo.unlink() + foo.write_text("bar") with pytest.raises(StageFileAlreadyExistsError): deterministic_run.run() -def test_run_deterministic_changed_deps_list(deterministic_run, repo_dir): - deterministic_run.deps = [repo_dir.BAR, repo_dir.CODE] +def test_run_deterministic_changed_deps_list(tmp_dir, deterministic_run): + tmp_dir.gen("bar", "bar content") + deterministic_run.deps = ["bar", "copy.py"] with pytest.raises(StageFileAlreadyExistsError): deterministic_run.run() -def test_run_deterministic_new_dep(deterministic_run, repo_dir): - deterministic_run.deps = [repo_dir.FOO, repo_dir.BAR, repo_dir.CODE] +def test_run_deterministic_new_dep(tmp_dir, deterministic_run): + tmp_dir.gen("bar", "bar content") + deterministic_run.deps.append("bar") with pytest.raises(StageFileAlreadyExistsError): deterministic_run.run() -def test_run_deterministic_remove_dep(deterministic_run, repo_dir): - deterministic_run.deps = [repo_dir.CODE] +def test_run_deterministic_remove_dep(deterministic_run): + deterministic_run.deps = ["copy.py"] with pytest.raises(StageFileAlreadyExistsError): deterministic_run.run() From 23288aa568aaacb96ee8e722c4106d03439f4e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Redzy=C5=84ski?= Date: Thu, 9 Jan 2020 15:05:29 +0100 Subject: [PATCH 3/3] run: tests: remove deterministic_run fixture --- tests/func/test_run.py | 115 +++++++++++++---------------------------- 1 file changed, 36 insertions(+), 79 deletions(-) diff --git a/tests/func/test_run.py b/tests/func/test_run.py index 74cea42be1..1b49bc5408 100644 --- a/tests/func/test_run.py +++ b/tests/func/test_run.py @@ -611,100 +611,57 @@ def test_cwd_is_ignored(self): ) -class DeterministicRunBaseFixture(object): - def __init__(self, tmp_dir, dvc): - tmp_dir.gen( - "copy.py", - "import sys, shutil\nshutil.copyfile(sys.argv[1], sys.argv[2])", - ) - tmp_dir.gen("foo", "foo content") - - self.out_file = "out" - self.stage_file = self.out_file + ".dvc" - self.cmd = "python {} {} {}".format("copy.py", "foo", self.out_file) - self.deps = ["foo", "copy.py"] - self.outs = [self.out_file] - self.overwrite = False - self.ignore_build_cache = False - self.dvc = dvc - self.stage = None - - def run(self): - self.stage = self.dvc.run( - cmd=self.cmd, - fname=self.stage_file, - overwrite=self.overwrite, - ignore_build_cache=self.ignore_build_cache, - deps=self.deps, - outs=self.outs, - ) - return self.stage - - -@pytest.fixture -def deterministic_run(tmp_dir, dvc): - run_base = DeterministicRunBaseFixture(tmp_dir, dvc) - run_base.run() - yield run_base - - -def test_run_deterministic(deterministic_run): - deterministic_run.run() - - -def test_run_deterministic_overwrite(deterministic_run): - deterministic_run.overwrite = True - deterministic_run.ignore_build_cache = True - deterministic_run.run() - - -def test_run_deterministic_callback(deterministic_run): - with deterministic_run.stage.repo.lock: - deterministic_run.stage.remove() - deterministic_run.deps = [] - deterministic_run.run() - with mock.patch("dvc.prompt.confirm", return_value=True): - assert deterministic_run.run() +def test_rerun_deterministic(tmp_dir, run_copy): + tmp_dir.gen("foo", "foo content") + assert run_copy("foo", "out") is not None + assert run_copy("foo", "out") is None -def test_run_deterministic_changed_dep(deterministic_run): - foo = Path("foo") - foo.unlink() - foo.write_text("bar") - with pytest.raises(StageFileAlreadyExistsError): - deterministic_run.run() +def test_rerun_deterministic_ignore_cache(tmp_dir, run_copy): + tmp_dir.gen("foo", "foo content") -def test_run_deterministic_changed_deps_list(tmp_dir, deterministic_run): - tmp_dir.gen("bar", "bar content") - deterministic_run.deps = ["bar", "copy.py"] - with pytest.raises(StageFileAlreadyExistsError): - deterministic_run.run() + assert run_copy("foo", "out") is not None + assert run_copy("foo", "out", ignore_build_cache=True) is not None -def test_run_deterministic_new_dep(tmp_dir, deterministic_run): - tmp_dir.gen("bar", "bar content") - deterministic_run.deps.append("bar") - with pytest.raises(StageFileAlreadyExistsError): - deterministic_run.run() +def test_rerun_callback(dvc): + def run_callback(): + return dvc.run( + cmd=("echo content > out"), outs=["out"], deps=[], overwrite=False + ) + assert run_callback() is not None -def test_run_deterministic_remove_dep(deterministic_run): - deterministic_run.deps = ["copy.py"] + with mock.patch("dvc.prompt.confirm", return_value=True): + assert run_callback() is not None + + +def test_rerun_changed_dep(tmp_dir, run_copy): + tmp_dir.gen("foo", "foo content") + assert run_copy("foo", "out") is not None + + tmp_dir.gen("foo", "changed content") with pytest.raises(StageFileAlreadyExistsError): - deterministic_run.run() + run_copy("foo", "out", overwrite=False) -def test_run_deterministic_changed_out(deterministic_run): - os.unlink(deterministic_run.out_file) +def test_rerun_changed_stage(tmp_dir, run_copy): + tmp_dir.gen("foo", "foo content") + assert run_copy("foo", "out") is not None + + tmp_dir.gen("bar", "bar content") with pytest.raises(StageFileAlreadyExistsError): - deterministic_run.run() + run_copy("bar", "out", overwrite=False) + +def test_rerun_changed_out(tmp_dir, run_copy): + tmp_dir.gen("foo", "foo content") + assert run_copy("foo", "out") is not None -def test_run_deterministic_changed_cmd(deterministic_run): - deterministic_run.cmd += " arg" + Path("out").write_text("modification") with pytest.raises(StageFileAlreadyExistsError): - deterministic_run.run() + run_copy("foo", "out", overwrite=False) class TestRunCommit(TestDvc):