Skip to content

Commit 9e9f37e

Browse files
authored
Merge pull request #3094 from pared/2896_run
run: tests: migrate to dir helpers
2 parents d6092c1 + 23288aa commit 9e9f37e

File tree

1 file changed

+43
-88
lines changed

1 file changed

+43
-88
lines changed

tests/func/test_run.py

Lines changed: 43 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import filecmp
22
import logging
33
import os
4-
import shutil
54
import uuid
5+
from pathlib import Path
66

77
import mock
88
import pytest
@@ -611,93 +611,57 @@ def test_cwd_is_ignored(self):
611611
)
612612

613613

614-
class DeterministicRunBaseFixture(object):
615-
def __init__(self, repo_dir, dvc_repo):
616-
self.out_file = "out"
617-
self.stage_file = self.out_file + ".dvc"
618-
self.cmd = "python {} {} {}".format(
619-
repo_dir.CODE, repo_dir.FOO, self.out_file
620-
)
621-
self.deps = [repo_dir.FOO, repo_dir.CODE]
622-
self.outs = [self.out_file]
623-
self.overwrite = False
624-
self.ignore_build_cache = False
625-
self.dvc_repo = dvc_repo
626-
self.stage = None
627-
628-
def run(self):
629-
self.stage = self.dvc_repo.run(
630-
cmd=self.cmd,
631-
fname=self.stage_file,
632-
overwrite=self.overwrite,
633-
ignore_build_cache=self.ignore_build_cache,
634-
deps=self.deps,
635-
outs=self.outs,
636-
)
637-
return self.stage
614+
def test_rerun_deterministic(tmp_dir, run_copy):
615+
tmp_dir.gen("foo", "foo content")
638616

617+
assert run_copy("foo", "out") is not None
618+
assert run_copy("foo", "out") is None
639619

640-
@pytest.fixture
641-
def deterministic_run(dvc_repo, repo_dir):
642-
run_base = DeterministicRunBaseFixture(repo_dir, dvc_repo)
643-
run_base.run()
644-
yield run_base
645620

621+
def test_rerun_deterministic_ignore_cache(tmp_dir, run_copy):
622+
tmp_dir.gen("foo", "foo content")
646623

647-
def test_run_deterministic(deterministic_run):
648-
deterministic_run.run()
624+
assert run_copy("foo", "out") is not None
625+
assert run_copy("foo", "out", ignore_build_cache=True) is not None
649626

650627

651-
def test_run_deterministic_overwrite(deterministic_run):
652-
deterministic_run.overwrite = True
653-
deterministic_run.ignore_build_cache = True
654-
deterministic_run.run()
628+
def test_rerun_callback(dvc):
629+
def run_callback():
630+
return dvc.run(
631+
cmd=("echo content > out"), outs=["out"], deps=[], overwrite=False
632+
)
655633

634+
assert run_callback() is not None
656635

657-
def test_run_deterministic_callback(deterministic_run):
658-
with deterministic_run.stage.repo.lock:
659-
deterministic_run.stage.remove()
660-
deterministic_run.deps = []
661-
deterministic_run.run()
662636
with mock.patch("dvc.prompt.confirm", return_value=True):
663-
assert deterministic_run.run()
637+
assert run_callback() is not None
664638

665639

666-
def test_run_deterministic_changed_dep(deterministic_run, repo_dir):
667-
os.unlink(repo_dir.FOO)
668-
shutil.copy(repo_dir.BAR, repo_dir.FOO)
669-
with pytest.raises(StageFileAlreadyExistsError):
670-
deterministic_run.run()
671-
640+
def test_rerun_changed_dep(tmp_dir, run_copy):
641+
tmp_dir.gen("foo", "foo content")
642+
assert run_copy("foo", "out") is not None
672643

673-
def test_run_deterministic_changed_deps_list(deterministic_run, repo_dir):
674-
deterministic_run.deps = [repo_dir.BAR, repo_dir.CODE]
644+
tmp_dir.gen("foo", "changed content")
675645
with pytest.raises(StageFileAlreadyExistsError):
676-
deterministic_run.run()
646+
run_copy("foo", "out", overwrite=False)
677647

678648

679-
def test_run_deterministic_new_dep(deterministic_run, repo_dir):
680-
deterministic_run.deps = [repo_dir.FOO, repo_dir.BAR, repo_dir.CODE]
681-
with pytest.raises(StageFileAlreadyExistsError):
682-
deterministic_run.run()
683-
649+
def test_rerun_changed_stage(tmp_dir, run_copy):
650+
tmp_dir.gen("foo", "foo content")
651+
assert run_copy("foo", "out") is not None
684652

685-
def test_run_deterministic_remove_dep(deterministic_run, repo_dir):
686-
deterministic_run.deps = [repo_dir.CODE]
653+
tmp_dir.gen("bar", "bar content")
687654
with pytest.raises(StageFileAlreadyExistsError):
688-
deterministic_run.run()
655+
run_copy("bar", "out", overwrite=False)
689656

690657

691-
def test_run_deterministic_changed_out(deterministic_run):
692-
os.unlink(deterministic_run.out_file)
693-
with pytest.raises(StageFileAlreadyExistsError):
694-
deterministic_run.run()
695-
658+
def test_rerun_changed_out(tmp_dir, run_copy):
659+
tmp_dir.gen("foo", "foo content")
660+
assert run_copy("foo", "out") is not None
696661

697-
def test_run_deterministic_changed_cmd(deterministic_run):
698-
deterministic_run.cmd += " arg"
662+
Path("out").write_text("modification")
699663
with pytest.raises(StageFileAlreadyExistsError):
700-
deterministic_run.run()
664+
run_copy("foo", "out", overwrite=False)
701665

702666

703667
class TestRunCommit(TestDvc):
@@ -931,33 +895,24 @@ def test_ignore_build_cache(self):
931895
assert "hello\nhello\n" == fobj.read()
932896

933897

934-
def test_bad_stage_fname(repo_dir, dvc_repo):
935-
dvc_repo.add(repo_dir.FOO)
898+
def test_bad_stage_fname(tmp_dir, dvc, run_copy):
899+
tmp_dir.dvc_gen("foo", "foo content")
900+
936901
with pytest.raises(StageFileBadNameError):
937-
dvc_repo.run(
938-
cmd="python {} {} {}".format(repo_dir.CODE, repo_dir.FOO, "out"),
939-
deps=[repo_dir.FOO, repo_dir.CODE],
940-
outs=["out"],
941-
fname="out_stage", # Bad name, should end with .dvc
942-
)
902+
# fname should end with .dvc
903+
run_copy("foo", "foo_copy", fname="out_stage")
943904

944905
# Check that command hasn't been run
945-
assert not os.path.exists("out")
906+
assert not (tmp_dir / "foo_copy").exists()
946907

947908

948-
def test_should_raise_on_stage_dependency(repo_dir, dvc_repo):
909+
def test_should_raise_on_stage_dependency(run_copy):
949910
with pytest.raises(DependencyIsStageFileError):
950-
dvc_repo.run(
951-
cmd="python {} {} {}".format(repo_dir.CODE, repo_dir.FOO, "out"),
952-
deps=[repo_dir.FOO, "name.dvc"],
953-
outs=["out"],
954-
)
911+
run_copy("name.dvc", "stage_copy")
912+
955913

914+
def test_should_raise_on_stage_output(tmp_dir, dvc, run_copy):
915+
tmp_dir.dvc_gen("foo", "foo content")
956916

957-
def test_should_raise_on_stage_output(repo_dir, dvc_repo):
958917
with pytest.raises(OutputIsStageFileError):
959-
dvc_repo.run(
960-
cmd="python {} {} {}".format(repo_dir.CODE, repo_dir.FOO, "out"),
961-
deps=[repo_dir.FOO],
962-
outs=["name.dvc"],
963-
)
918+
run_copy("foo", "name.dvc")

0 commit comments

Comments
 (0)