Skip to content

Commit 93c5476

Browse files
committed
repro: tests: yield dict of stages from repro_dir
1 parent 25437ca commit 93c5476

File tree

1 file changed

+79
-88
lines changed

1 file changed

+79
-88
lines changed

tests/func/test_repro.py

Lines changed: 79 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,73 +1395,75 @@ def repro_dir(tmp_dir, dvc, run_copy):
13951395
}
13961396
)
13971397

1398-
tmp_dir.origin_copy = "origin_copy"
1399-
assert run_copy("origin_data", tmp_dir.origin_copy) is not None
1400-
assert (tmp_dir / tmp_dir.origin_copy).read_text() == "origin data content"
1398+
stages = {}
1399+
1400+
origin_copy = "origin_copy"
1401+
stage = run_copy("origin_data", origin_copy)
1402+
assert stage is not None
1403+
assert (tmp_dir / origin_copy).read_text() == "origin data content"
1404+
stages["origin_copy"] = stage
1405+
1406+
origin_copy_2 = os.path.join("dir", "origin_copy_2")
1407+
stage = run_copy(origin_copy, origin_copy_2, fname=origin_copy_2 + ".dvc")
1408+
assert stage is not None
1409+
assert (tmp_dir / origin_copy_2).read_text() == "origin data content"
1410+
stages["origin_copy_2"] = stage
14011411

1402-
# Unrelated are to verify that reproducing `dir` will not trigger them too
1403-
assert run_copy(tmp_dir.origin_copy, "unrelated1") is not None
14041412
dir_file_path = tmp_dir / "data_dir" / "dir_file"
1413+
dir_file_copy = os.path.join("dir", "subdir", "dir_file_copy")
1414+
stage = run_copy(
1415+
fspath(dir_file_path), dir_file_copy, fname=dir_file_copy + ".dvc"
1416+
)
1417+
assert stage is not None
1418+
assert (tmp_dir / dir_file_copy).read_text() == "dir file content"
1419+
stages["dir_file_copy"] = stage
1420+
1421+
last_stage = os.path.join("dir", "Dvcfile")
1422+
stage = dvc.run(fname=last_stage, deps=[origin_copy_2, dir_file_copy])
1423+
assert stage is not None
1424+
stages["last_stage"] = stage
1425+
1426+
# Unrelated are to verify that reproducing `dir` will not trigger them too
1427+
assert run_copy(origin_copy, "unrelated1") is not None
14051428
assert run_copy(fspath(dir_file_path), "unrelated2") is not None
14061429

1407-
tmp_dir.origin_copy_2 = os.path.join("dir", "origin_copy_2")
1408-
assert (
1409-
run_copy(
1410-
tmp_dir.origin_copy,
1411-
tmp_dir.origin_copy_2,
1412-
fname=tmp_dir.origin_copy_2 + ".dvc",
1413-
)
1414-
is not None
1415-
)
1416-
assert (
1417-
tmp_dir / tmp_dir.origin_copy_2
1418-
).read_text() == "origin data content"
1430+
yield stages
14191431

1420-
tmp_dir.dir_file_copy = os.path.join("dir", "subdir", "dir_file_copy")
1421-
assert (
1422-
run_copy(
1423-
fspath(dir_file_path),
1424-
tmp_dir.dir_file_copy,
1425-
fname=tmp_dir.dir_file_copy + ".dvc",
1426-
)
1427-
is not None
1428-
)
1429-
assert (tmp_dir / tmp_dir.dir_file_copy).read_text() == "dir file content"
14301432

1431-
tmp_dir.last_stage = os.path.join("dir", "Dvcfile")
1432-
assert (
1433-
dvc.run(
1434-
fname=tmp_dir.last_stage,
1435-
deps=[tmp_dir.origin_copy_2, tmp_dir.dir_file_copy],
1436-
)
1437-
is not None
1438-
)
1433+
def _rewrite_file(path_elements, new_content):
1434+
if isinstance(path_elements, str):
1435+
path_elements = [path_elements]
1436+
file = Path(os.sep.join(path_elements))
1437+
file.unlink()
1438+
file.write_text(new_content)
14391439

1440-
yield tmp_dir
1440+
1441+
def _out_path(stage):
1442+
return Path(stage.outs[0].fspath)
14411443

14421444

14431445
def test_recursive_repro_default(dvc, repro_dir):
14441446
"""
14451447
Test recursive repro on dir after a dep outside this dir has changed.
14461448
"""
1447-
origin = Path("origin_data")
1448-
origin.unlink()
1449-
origin.write_text("new origin data content")
1449+
_rewrite_file("origin_data", "new origin data content")
14501450

14511451
stages = dvc.reproduce("dir", recursive=True)
14521452

1453-
# Check that the dependency ("source") and the dependent stages
1454-
# inside the folder have been reproduced ("first", "third")
1455-
names = [s.relpath for s in stages]
1456-
assert len(names) == 3
1457-
assert set(names) == {
1458-
repro_dir.origin_copy + ".dvc",
1459-
repro_dir.origin_copy_2 + ".dvc",
1460-
repro_dir.last_stage,
1461-
}
1462-
assert Path(repro_dir.origin_copy).read_text() == "new origin data content"
1453+
# Check that the dependency ("origin_copy") and the dependent stages
1454+
# inside the folder have been reproduced ("origin_copy_2", "last_stage")
1455+
assert stages == [
1456+
repro_dir["origin_copy"],
1457+
repro_dir["origin_copy_2"],
1458+
repro_dir["last_stage"],
1459+
]
1460+
assert (
1461+
_out_path(repro_dir["origin_copy"]).read_text()
1462+
== "new origin data content"
1463+
)
14631464
assert (
1464-
Path(repro_dir.origin_copy_2).read_text() == "new origin data content"
1465+
_out_path(repro_dir["origin_copy_2"]).read_text()
1466+
== "new origin data content"
14651467
)
14661468

14671469

@@ -1470,26 +1472,20 @@ def test_recursive_repro_single(dvc, repro_dir):
14701472
Test recursive single-item repro on dir
14711473
after a dep outside this dir has changed.
14721474
"""
1473-
origin_data = Path("origin_data")
1474-
origin_data.unlink()
1475-
origin_data.write_text("new origin content")
1476-
1477-
dir_file = repro_dir / "data_dir" / "dir_file"
1478-
dir_file.unlink()
1479-
dir_file.write_text("new dir file content")
1475+
_rewrite_file("origin_data", "new origin content")
1476+
_rewrite_file(["data_dir", "dir_file"], "new dir file content")
14801477

14811478
stages = dvc.reproduce("dir", recursive=True, single_item=True)
14821479
# Check that just stages inside given dir
14831480
# with changed direct deps have been reproduced.
1484-
# This means that "first" stage should not be reproduced
1485-
# since it depends on "source".
1486-
# Also check that "dir_file_copy" stage was reproduced before "third" stage
1487-
assert len(stages) == 2
1488-
assert [s.relpath for s in stages] == [
1489-
repro_dir.dir_file_copy + ".dvc",
1490-
repro_dir.last_stage,
1491-
]
1492-
assert Path(repro_dir.dir_file_copy).read_text() == "new dir file content"
1481+
# This means that "origin_copy_2" stage should not be reproduced
1482+
# since it depends on "origin_copy".
1483+
# Also check that "dir_file_copy" stage was reproduced before "last_stage"
1484+
assert stages == [repro_dir["dir_file_copy"], repro_dir["last_stage"]]
1485+
assert (
1486+
_out_path(repro_dir["dir_file_copy"]).read_text()
1487+
== "new dir file content"
1488+
)
14931489

14941490

14951491
def test_recursive_repro_single_force(dvc, repro_dir):
@@ -1498,32 +1494,31 @@ def test_recursive_repro_single_force(dvc, repro_dir):
14981494
without any dependencies changing.
14991495
"""
15001496
stages = dvc.reproduce("dir", recursive=True, single_item=True, force=True)
1501-
assert len(stages) == 3
15021497
# Check that all stages inside given dir have been reproduced
1503-
# Also check that "dir_file_copy" stage was reproduced before "third" stage
1504-
# and that "first" stage was reproduced before "third" stage
1505-
names = [s.relpath for s in stages]
1506-
assert {
1507-
repro_dir.origin_copy_2 + ".dvc",
1508-
repro_dir.dir_file_copy + ".dvc",
1509-
repro_dir.last_stage,
1510-
} == set(names)
1511-
assert names.index(repro_dir.origin_copy_2 + ".dvc") < names.index(
1512-
repro_dir.last_stage
1498+
# Also check that "dir_file_copy" stage was reproduced before "last_stage"
1499+
# and that "origin_copy" stage was reproduced before "last_stage" stage
1500+
assert len(stages) == 3
1501+
assert set(stages) == {
1502+
repro_dir["origin_copy_2"],
1503+
repro_dir["dir_file_copy"],
1504+
repro_dir["last_stage"],
1505+
}
1506+
assert stages.index(repro_dir["origin_copy_2"]) < stages.index(
1507+
repro_dir["last_stage"]
15131508
)
1514-
assert names.index(repro_dir.dir_file_copy + ".dvc") < names.index(
1515-
repro_dir.last_stage
1509+
assert stages.index(repro_dir["dir_file_copy"]) < stages.index(
1510+
repro_dir["last_stage"]
15161511
)
15171512

15181513

1519-
def test_recursive_repro_empty_dir(dvc, repro_dir):
1514+
def test_recursive_repro_empty_dir(tmp_dir, dvc, repro_dir):
15201515
"""
15211516
Test recursive repro on an empty directory
15221517
"""
1523-
(repro_dir / "emptydir").mkdir()
1518+
(tmp_dir / "emptydir").mkdir()
15241519

15251520
stages = dvc.reproduce("emptydir", recursive=True, force=True)
1526-
assert len(stages) == 0
1521+
assert stages == []
15271522

15281523

15291524
def test_recursive_repro_recursive_missing_file(dvc):
@@ -1541,13 +1536,9 @@ def test_recursive_repro_on_stage_file(dvc, repro_dir):
15411536
Test recursive repro on a stage file instead of directory
15421537
"""
15431538
stages = dvc.reproduce(
1544-
repro_dir.origin_copy_2 + ".dvc", recursive=True, force=True
1539+
repro_dir["origin_copy_2"].relpath, recursive=True, force=True
15451540
)
1546-
assert len(stages) == 2
1547-
assert [
1548-
repro_dir.origin_copy + ".dvc",
1549-
repro_dir.origin_copy_2 + ".dvc",
1550-
] == [stage.relpath for stage in stages]
1541+
assert stages == [repro_dir["origin_copy"], repro_dir["origin_copy_2"]]
15511542

15521543

15531544
def test_dvc_formatting_retained(tmp_dir, dvc, run_copy):

0 commit comments

Comments
 (0)