Skip to content

Commit e935046

Browse files
committed
repro: tests: migrate to dir helpers
1 parent 277d28c commit e935046

File tree

1 file changed

+88
-111
lines changed

1 file changed

+88
-111
lines changed

tests/func/test_repro.py

Lines changed: 88 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import getpass
33
import os
44
import posixpath
5-
import pathlib
65
import re
76
import shutil
87
import uuid
@@ -17,6 +16,7 @@
1716
from google.cloud import storage as gc
1817
from mock import patch
1918

19+
from dvc.compat import fspath
2020
from dvc.exceptions import CyclicGraphError
2121
from dvc.exceptions import ReproductionError
2222
from dvc.exceptions import StagePathAsOutputError
@@ -1372,108 +1372,107 @@ def test(self):
13721372

13731373

13741374
@pytest.fixture
1375-
def repro_dir(dvc_repo, repo_dir):
1376-
repo_dir.dname = "dir"
1377-
os.mkdir(repo_dir.dname)
1378-
repo_dir.emptydname = "emptydir"
1379-
os.mkdir(repo_dir.emptydname)
1380-
subdname = os.path.join(repo_dir.dname, "subdir")
1381-
os.mkdir(subdname)
1382-
1383-
repo_dir.source = "source"
1384-
repo_dir.source_stage = repo_dir.source + ".dvc"
1385-
stage = dvc_repo.run(
1386-
fname=repo_dir.source_stage,
1387-
outs=[repo_dir.source],
1388-
deps=[repo_dir.FOO],
1375+
def repro_dir(tmp_dir, dvc, run_copy):
1376+
tmp_dir.gen(
1377+
{
1378+
"foo": "foo content",
1379+
"bar": "bar content",
1380+
"data_dir": {"dir_file": "dir file content"},
1381+
}
1382+
)
1383+
(tmp_dir / "dir").mkdir()
1384+
subdname = os.path.join("dir", "subdir")
1385+
(tmp_dir / "dir" / "subdir").mkdir()
1386+
1387+
tmp_dir.source = "source"
1388+
tmp_dir.source_stage = tmp_dir.source + ".dvc"
1389+
stage = dvc.run(
1390+
fname=tmp_dir.source_stage, outs=[tmp_dir.source], deps=["foo"]
13891391
)
13901392
assert stage is not None
1391-
assert filecmp.cmp(repo_dir.source, repo_dir.FOO, shallow=False)
1392-
1393-
repo_dir.unrelated1 = "unrelated1"
1394-
repo_dir.unrelated1_stage = repo_dir.unrelated1 + ".dvc"
1395-
stage = dvc_repo.run(
1396-
fname=repo_dir.unrelated1_stage,
1397-
outs=[repo_dir.unrelated1],
1398-
deps=[repo_dir.source],
1393+
assert filecmp.cmp(tmp_dir.source, "foo", shallow=False)
1394+
1395+
tmp_dir.unrelated1 = "unrelated1"
1396+
tmp_dir.unrelated1_stage = tmp_dir.unrelated1 + ".dvc"
1397+
stage = dvc.run(
1398+
fname=tmp_dir.unrelated1_stage,
1399+
outs=[tmp_dir.unrelated1],
1400+
deps=[tmp_dir.source],
13991401
)
14001402
assert stage is not None
14011403

1402-
repo_dir.unrelated2 = "unrelated2"
1403-
repo_dir.unrelated2_stage = repo_dir.unrelated2 + ".dvc"
1404-
stage = dvc_repo.run(
1405-
fname=repo_dir.unrelated2_stage,
1406-
outs=[repo_dir.unrelated2],
1407-
deps=[repo_dir.DATA],
1404+
tmp_dir.unrelated2 = "unrelated2"
1405+
tmp_dir.unrelated2_stage = tmp_dir.unrelated2 + ".dvc"
1406+
stage = dvc.run(
1407+
fname=tmp_dir.unrelated2_stage,
1408+
outs=[tmp_dir.unrelated2],
1409+
deps=[fspath(tmp_dir / "data_dir" / "dir_file")],
14081410
)
14091411
assert stage is not None
14101412

1411-
repo_dir.first = os.path.join(repo_dir.dname, "first")
1412-
repo_dir.first_stage = repo_dir.first + ".dvc"
1413+
tmp_dir.first = os.path.join("dir", "first")
1414+
tmp_dir.first_stage = tmp_dir.first + ".dvc"
1415+
1416+
stage = run_copy(tmp_dir.source, tmp_dir.first, fname=tmp_dir.first_stage)
14131417

1414-
stage = dvc_repo.run(
1415-
fname=repo_dir.first_stage,
1416-
deps=[repo_dir.source],
1417-
outs=[repo_dir.first],
1418-
cmd="python {} {} {}".format(
1419-
repo_dir.CODE, repo_dir.source, repo_dir.first
1420-
),
1421-
)
14221418
assert stage is not None
1423-
assert filecmp.cmp(repo_dir.first, repo_dir.FOO, shallow=False)
1424-
1425-
repo_dir.second = os.path.join(subdname, "second")
1426-
repo_dir.second_stage = repo_dir.second + ".dvc"
1427-
stage = dvc_repo.run(
1428-
fname=repo_dir.second_stage,
1429-
outs=[repo_dir.second],
1430-
deps=[repo_dir.DATA],
1419+
assert filecmp.cmp(tmp_dir.first, "foo", shallow=False)
1420+
1421+
tmp_dir.second = os.path.join(subdname, "second")
1422+
tmp_dir.second_stage = tmp_dir.second + ".dvc"
1423+
stage = dvc.run(
1424+
fname=tmp_dir.second_stage,
1425+
outs=[tmp_dir.second],
1426+
deps=[fspath(tmp_dir / "data_dir" / "dir_file")],
14311427
)
14321428
assert stage is not None
1433-
assert filecmp.cmp(repo_dir.second, repo_dir.DATA, shallow=False)
1429+
assert filecmp.cmp(
1430+
tmp_dir.second,
1431+
fspath(tmp_dir / "data_dir" / "dir_file"),
1432+
shallow=False,
1433+
)
14341434

1435-
repo_dir.third_stage = os.path.join(repo_dir.dname, "Dvcfile")
1436-
stage = dvc_repo.run(
1437-
fname=repo_dir.third_stage, deps=[repo_dir.first, repo_dir.second]
1435+
tmp_dir.third_stage = os.path.join("dir", "Dvcfile")
1436+
stage = dvc.run(
1437+
fname=tmp_dir.third_stage, deps=[tmp_dir.first, tmp_dir.second]
14381438
)
14391439
assert stage is not None
14401440

1441-
yield repo_dir
1441+
yield tmp_dir
14421442

14431443

1444-
def test_recursive_repro_default(dvc_repo, repro_dir):
1444+
def test_recursive_repro_default(dvc, repro_dir):
14451445
"""
14461446
Test recursive repro on dir after a dep outside this dir has changed.
14471447
"""
1448-
os.unlink(repro_dir.FOO)
1449-
shutil.copyfile(repro_dir.BAR, repro_dir.FOO)
1448+
os.unlink("foo")
1449+
shutil.copyfile("bar", "foo")
14501450

1451-
stages = dvc_repo.reproduce(repro_dir.dname, recursive=True)
1451+
stages = dvc.reproduce("dir", recursive=True)
14521452
# Check that the dependency ("source") and the dependent stages
14531453
# inside the folder have been reproduced ("first", "third")
14541454
assert len(stages) == 3
14551455
names = [stage.relpath for stage in stages]
14561456
assert repro_dir.source_stage in names
14571457
assert repro_dir.first_stage in names
14581458
assert repro_dir.third_stage in names
1459-
assert filecmp.cmp(repro_dir.source, repro_dir.BAR, shallow=False)
1460-
assert filecmp.cmp(repro_dir.first, repro_dir.BAR, shallow=False)
1459+
assert filecmp.cmp(repro_dir.source, "bar", shallow=False)
1460+
assert filecmp.cmp(repro_dir.first, "bar", shallow=False)
14611461

14621462

1463-
def test_recursive_repro_single(dvc_repo, repro_dir):
1463+
def test_recursive_repro_single(dvc, repro_dir):
14641464
"""
14651465
Test recursive single-item repro on dir
14661466
after a dep outside this dir has changed.
14671467
"""
1468-
os.unlink(repro_dir.FOO)
1469-
shutil.copyfile(repro_dir.BAR, repro_dir.FOO)
1468+
os.unlink("foo")
1469+
shutil.copyfile("bar", "foo")
14701470

1471-
os.unlink(repro_dir.DATA)
1472-
shutil.copyfile(repro_dir.BAR, repro_dir.DATA)
1471+
# os.unlink(repro_dir.DATA)
1472+
(repro_dir / "data_dir" / "dir_file").unlink()
1473+
shutil.copyfile("bar", fspath(repro_dir / "data_dir" / "dir_file"))
14731474

1474-
stages = dvc_repo.reproduce(
1475-
repro_dir.dname, recursive=True, single_item=True
1476-
)
1475+
stages = dvc.reproduce("dir", recursive=True, single_item=True)
14771476
# Check that just stages inside given dir
14781477
# with changed direct deps have been reproduced.
14791478
# This means that "first" stage should not be reproduced
@@ -1482,17 +1481,15 @@ def test_recursive_repro_single(dvc_repo, repro_dir):
14821481
assert len(stages) == 2
14831482
assert repro_dir.second_stage == stages[0].relpath
14841483
assert repro_dir.third_stage == stages[1].relpath
1485-
assert filecmp.cmp(repro_dir.second, repro_dir.BAR, shallow=False)
1484+
assert filecmp.cmp(repro_dir.second, "bar", shallow=False)
14861485

14871486

1488-
def test_recursive_repro_single_force(dvc_repo, repro_dir):
1487+
def test_recursive_repro_single_force(dvc, repro_dir):
14891488
"""
14901489
Test recursive single-item force repro on dir
14911490
without any dependencies changing.
14921491
"""
1493-
stages = dvc_repo.reproduce(
1494-
repro_dir.dname, recursive=True, single_item=True, force=True
1495-
)
1492+
stages = dvc.reproduce("dir", recursive=True, single_item=True, force=True)
14961493
assert len(stages) == 3
14971494
names = [stage.relpath for stage in stages]
14981495
# Check that all stages inside given dir have been reproduced
@@ -1509,73 +1506,53 @@ def test_recursive_repro_single_force(dvc_repo, repro_dir):
15091506
)
15101507

15111508

1512-
def test_recursive_repro_empty_dir(dvc_repo, repro_dir):
1509+
def test_recursive_repro_empty_dir(tmp_dir, dvc):
15131510
"""
15141511
Test recursive repro on an empty directory
15151512
"""
1516-
stages = dvc_repo.reproduce(
1517-
repro_dir.emptydname, recursive=True, force=True
1518-
)
1513+
(tmp_dir / "emptydir").mkdir()
1514+
1515+
stages = dvc.reproduce("emptydir", recursive=True, force=True)
15191516
assert len(stages) == 0
15201517

15211518

1522-
def test_recursive_repro_recursive_missing_file(dvc_repo):
1519+
def test_recursive_repro_recursive_missing_file(dvc):
15231520
"""
15241521
Test recursive repro on a missing file
15251522
"""
15261523
with pytest.raises(StageFileDoesNotExistError):
1527-
dvc_repo.reproduce("notExistingStage.dvc", recursive=True)
1524+
dvc.reproduce("notExistingStage.dvc", recursive=True)
15281525
with pytest.raises(StageFileDoesNotExistError):
1529-
dvc_repo.reproduce("notExistingDir/", recursive=True)
1526+
dvc.reproduce("notExistingDir/", recursive=True)
15301527

15311528

1532-
def test_recursive_repro_on_stage_file(dvc_repo, repro_dir):
1529+
def test_recursive_repro_on_stage_file(dvc, repro_dir):
15331530
"""
15341531
Test recursive repro on a stage file instead of directory
15351532
"""
1536-
stages = dvc_repo.reproduce(
1537-
repro_dir.first_stage, recursive=True, force=True
1538-
)
1533+
stages = dvc.reproduce(repro_dir.first_stage, recursive=True, force=True)
15391534
assert len(stages) == 2
15401535
names = [stage.relpath for stage in stages]
15411536
assert repro_dir.source_stage in names
15421537
assert repro_dir.first_stage in names
15431538

15441539

1545-
@pytest.fixture
1546-
def foo_copy(repo_dir, dvc_repo):
1547-
stages = dvc_repo.add(repo_dir.FOO)
1548-
assert len(stages) == 1
1549-
foo_stage = stages[0]
1550-
assert foo_stage is not None
1551-
1552-
fname = "foo_copy"
1553-
stage_fname = fname + ".dvc"
1554-
dvc_repo.run(
1555-
fname=stage_fname,
1556-
outs=[fname],
1557-
deps=[repo_dir.FOO, repo_dir.CODE],
1558-
cmd="python {} {} {}".format(repo_dir.CODE, repo_dir.FOO, fname),
1559-
)
1560-
return {"fname": fname, "stage_fname": stage_fname}
1561-
1562-
1563-
def test_dvc_formatting_retained(dvc_repo, foo_copy):
1564-
root = pathlib.Path(dvc_repo.root_dir)
1565-
stage_file = root / foo_copy["stage_fname"]
1540+
def test_dvc_formatting_retained(tmp_dir, dvc, run_copy):
1541+
tmp_dir.dvc_gen("foo", "foo content")
1542+
stage = run_copy("foo", "foo_copy", fname="foo_copy.dvc")
1543+
stage_path = tmp_dir / stage.relpath
15661544

15671545
# Add comments and custom formatting to DVC-file
1568-
lines = list(map(_format_dvc_line, stage_file.read_text().splitlines()))
1546+
lines = list(map(_format_dvc_line, stage_path.read_text().splitlines()))
15691547
lines.insert(0, "# Starting comment")
15701548
stage_text = "".join(l + "\n" for l in lines)
1571-
stage_file.write_text(stage_text)
1549+
stage_path.write_text(stage_text)
15721550

15731551
# Rewrite data source and repro
1574-
(root / "foo").write_text("new_foo")
1575-
dvc_repo.reproduce(foo_copy["stage_fname"])
1552+
(tmp_dir / "foo").write_text("new foo")
1553+
dvc.reproduce("foo_copy.dvc", force=True)
15761554

1577-
# All differences should be only about md5
1578-
assert _hide_md5(stage_text) == _hide_md5(stage_file.read_text())
1555+
assert _hide_md5(stage_text) == _hide_md5(stage_path.read_text())
15791556

15801557

15811558
def _format_dvc_line(line):
@@ -1630,7 +1607,7 @@ def test(self):
16301607
assert evaluation[2].relpath == "E.dvc"
16311608

16321609

1633-
def test_ssh_dir_out(dvc_repo):
1610+
def test_ssh_dir_out(dvc):
16341611
if not _should_test_ssh():
16351612
pytest.skip()
16361613

@@ -1643,7 +1620,7 @@ def test_ssh_dir_out(dvc_repo):
16431620
assert main(["config", "cache.ssh", "sshcache"]) == 0
16441621

16451622
# Recreating to reread configs
1646-
repo = DvcRepo(dvc_repo.root_dir)
1623+
repo = DvcRepo(dvc.root_dir)
16471624

16481625
url_info = URLInfo(remote_url)
16491626
mkdir_cmd = "mkdir dir-out;cd dir-out;echo 1 > 1.txt; echo 2 > 2.txt"

0 commit comments

Comments
 (0)