|
1 | 1 | import filecmp |
2 | 2 | import logging |
3 | 3 | import os |
4 | | -import shutil |
5 | 4 | import uuid |
| 5 | +from pathlib import Path |
6 | 6 |
|
7 | 7 | import mock |
8 | 8 | import pytest |
@@ -611,93 +611,57 @@ def test_cwd_is_ignored(self): |
611 | 611 | ) |
612 | 612 |
|
613 | 613 |
|
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") |
638 | 616 |
|
| 617 | + assert run_copy("foo", "out") is not None |
| 618 | + assert run_copy("foo", "out") is None |
639 | 619 |
|
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 |
645 | 620 |
|
| 621 | +def test_rerun_deterministic_ignore_cache(tmp_dir, run_copy): |
| 622 | + tmp_dir.gen("foo", "foo content") |
646 | 623 |
|
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 |
649 | 626 |
|
650 | 627 |
|
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 | + ) |
655 | 633 |
|
| 634 | + assert run_callback() is not None |
656 | 635 |
|
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() |
662 | 636 | with mock.patch("dvc.prompt.confirm", return_value=True): |
663 | | - assert deterministic_run.run() |
| 637 | + assert run_callback() is not None |
664 | 638 |
|
665 | 639 |
|
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 |
672 | 643 |
|
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") |
675 | 645 | with pytest.raises(StageFileAlreadyExistsError): |
676 | | - deterministic_run.run() |
| 646 | + run_copy("foo", "out", overwrite=False) |
677 | 647 |
|
678 | 648 |
|
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 |
684 | 652 |
|
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") |
687 | 654 | with pytest.raises(StageFileAlreadyExistsError): |
688 | | - deterministic_run.run() |
| 655 | + run_copy("bar", "out", overwrite=False) |
689 | 656 |
|
690 | 657 |
|
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 |
696 | 661 |
|
697 | | -def test_run_deterministic_changed_cmd(deterministic_run): |
698 | | - deterministic_run.cmd += " arg" |
| 662 | + Path("out").write_text("modification") |
699 | 663 | with pytest.raises(StageFileAlreadyExistsError): |
700 | | - deterministic_run.run() |
| 664 | + run_copy("foo", "out", overwrite=False) |
701 | 665 |
|
702 | 666 |
|
703 | 667 | class TestRunCommit(TestDvc): |
@@ -931,33 +895,24 @@ def test_ignore_build_cache(self): |
931 | 895 | assert "hello\nhello\n" == fobj.read() |
932 | 896 |
|
933 | 897 |
|
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 | + |
936 | 901 | 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") |
943 | 904 |
|
944 | 905 | # Check that command hasn't been run |
945 | | - assert not os.path.exists("out") |
| 906 | + assert not (tmp_dir / "foo_copy").exists() |
946 | 907 |
|
947 | 908 |
|
948 | | -def test_should_raise_on_stage_dependency(repo_dir, dvc_repo): |
| 909 | +def test_should_raise_on_stage_dependency(run_copy): |
949 | 910 | 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 | + |
955 | 913 |
|
| 914 | +def test_should_raise_on_stage_output(tmp_dir, dvc, run_copy): |
| 915 | + tmp_dir.dvc_gen("foo", "foo content") |
956 | 916 |
|
957 | | -def test_should_raise_on_stage_output(repo_dir, dvc_repo): |
958 | 917 | 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