|
| 1 | +import os |
| 2 | +from collections import OrderedDict |
| 3 | +from operator import itemgetter |
| 4 | +from textwrap import dedent |
| 5 | + |
| 6 | +import pytest |
| 7 | +import yaml |
| 8 | +from dvc.dvcfile import PIPELINE_LOCK |
| 9 | +from dvc.serialize import get_params_deps |
| 10 | +from dvc.utils.fs import remove |
| 11 | +from dvc.utils.stage import parse_stage_for_update |
| 12 | + |
| 13 | +from tests.func.test_run_multistage import supported_params |
| 14 | + |
| 15 | + |
| 16 | +FS_STRUCTURE = { |
| 17 | + "foo": "bar\nfoobar", |
| 18 | + "bar": "foo\nfoobar", |
| 19 | + "foobar": "foobar\nbar", |
| 20 | + "params.yaml": yaml.dump(supported_params), |
| 21 | + "params2.yaml": yaml.dump(supported_params), |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def run_head(tmp_dir, dvc): |
| 27 | + """Output first line of each file to different file with '-1' appended.""" |
| 28 | + tmp_dir.gen( |
| 29 | + "head.py", |
| 30 | + dedent( |
| 31 | + """ |
| 32 | + import sys |
| 33 | + _, *files = sys.argv |
| 34 | + for file in files: |
| 35 | + with open(file) as f, open(file +"-1","w+") as w: |
| 36 | + w.write(f.readline()) |
| 37 | + """ |
| 38 | + ), |
| 39 | + ) |
| 40 | + |
| 41 | + def run(*args, **run_kwargs): |
| 42 | + return dvc.run( |
| 43 | + cmd="python head.py {}".format(" ".join(args)), |
| 44 | + outs=[dep + "-1" for dep in args], |
| 45 | + deps=args, |
| 46 | + **run_kwargs |
| 47 | + ) |
| 48 | + |
| 49 | + return run |
| 50 | + |
| 51 | + |
| 52 | +def read_lock_file(file=PIPELINE_LOCK): |
| 53 | + with open(file) as f: |
| 54 | + data = parse_stage_for_update(f.read(), file) |
| 55 | + assert isinstance(data, OrderedDict) |
| 56 | + return data |
| 57 | + |
| 58 | + |
| 59 | +def assert_eq_lockfile(previous, new): |
| 60 | + for content in (previous, new): |
| 61 | + assert isinstance(content, OrderedDict) |
| 62 | + |
| 63 | + # if they both are OrderedDict, then `==` will also check for order |
| 64 | + assert previous == new |
| 65 | + |
| 66 | + |
| 67 | +def test_deps_outs_are_sorted_by_path(tmp_dir, dvc, run_head): |
| 68 | + tmp_dir.gen(FS_STRUCTURE) |
| 69 | + deps = ["foo", "bar", "foobar"] |
| 70 | + run_head(*deps, name="copy-first-line") |
| 71 | + |
| 72 | + initial_content = read_lock_file() |
| 73 | + lock = initial_content["copy-first-line"] |
| 74 | + |
| 75 | + # lock stage key order: |
| 76 | + assert list(lock.keys()) == ["cmd", "deps", "outs"] |
| 77 | + |
| 78 | + # `path` key appear first and then the `md5` |
| 79 | + assert all(list(dep.keys()) == ["path", "md5"] for dep in lock["deps"]) |
| 80 | + assert all(list(out.keys()) == ["path", "md5"] for out in lock["outs"]) |
| 81 | + |
| 82 | + # deps are always sorted by the file path naming |
| 83 | + assert list(map(itemgetter("path"), lock["deps"])) == sorted(deps) |
| 84 | + |
| 85 | + # outs are too |
| 86 | + assert list( |
| 87 | + map(itemgetter("path"), initial_content["copy-first-line"]["outs"]) |
| 88 | + ) == [d + "-1" for d in sorted(deps)] |
| 89 | + |
| 90 | + |
| 91 | +def test_order_is_preserved_when_pipeline_order_changes( |
| 92 | + tmp_dir, dvc, run_head |
| 93 | +): |
| 94 | + tmp_dir.gen(FS_STRUCTURE) |
| 95 | + deps = ["foo", "bar", "foobar"] |
| 96 | + stage = run_head(*deps, name="copy-first-line") |
| 97 | + |
| 98 | + initial_content = read_lock_file() |
| 99 | + # reverse order of stage.outs and dump to the pipeline file |
| 100 | + # then, again change stage.deps and dump to the pipeline file |
| 101 | + reversal = stage.outs.reverse, stage.deps.reverse |
| 102 | + for reverse_items in reversal: |
| 103 | + reverse_items() |
| 104 | + stage.dvcfile._dump_pipeline_file(stage) |
| 105 | + |
| 106 | + # we only changed the order, should not reproduce |
| 107 | + assert not dvc.reproduce(stage.addressing) |
| 108 | + |
| 109 | + new_lock_content = read_lock_file() |
| 110 | + assert_eq_lockfile(new_lock_content, initial_content) |
| 111 | + |
| 112 | + (tmp_dir / PIPELINE_LOCK).unlink() |
| 113 | + assert dvc.reproduce(stage.addressing) == [stage] |
| 114 | + new_lock_content = read_lock_file() |
| 115 | + assert_eq_lockfile(new_lock_content, initial_content) |
| 116 | + |
| 117 | + |
| 118 | +def test_cmd_changes_other_orders_are_preserved(tmp_dir, dvc, run_head): |
| 119 | + tmp_dir.gen(FS_STRUCTURE) |
| 120 | + deps = ["foo", "bar", "foobar"] |
| 121 | + stage = run_head(*deps, name="copy-first-line") |
| 122 | + |
| 123 | + initial_content = read_lock_file() |
| 124 | + # let's change cmd in pipeline file |
| 125 | + # it should only change "cmd", otherwise it should be |
| 126 | + # structurally same as cmd |
| 127 | + stage.cmd = " ".join(stage.cmd.split()) |
| 128 | + stage.dvcfile._dump_pipeline_file(stage) |
| 129 | + |
| 130 | + initial_content["copy-first-line"]["cmd"] = stage.cmd |
| 131 | + |
| 132 | + assert dvc.reproduce(stage.addressing) == [stage] |
| 133 | + |
| 134 | + new_lock_content = read_lock_file() |
| 135 | + assert_eq_lockfile(new_lock_content, initial_content) |
| 136 | + |
| 137 | + |
| 138 | +def test_params_dump(tmp_dir, dvc, run_head): |
| 139 | + tmp_dir.gen(FS_STRUCTURE) |
| 140 | + |
| 141 | + stage = run_head( |
| 142 | + "foo", |
| 143 | + "bar", |
| 144 | + "foobar", |
| 145 | + name="copy-first-line", |
| 146 | + params=[ |
| 147 | + "params2.yaml:answer,lists,name", |
| 148 | + "params.yaml:lists,floats,nested.nested1,nested.nested1.nested2", |
| 149 | + ], |
| 150 | + ) |
| 151 | + |
| 152 | + initial_content = read_lock_file() |
| 153 | + lock = initial_content["copy-first-line"] |
| 154 | + |
| 155 | + # lock stage key order: |
| 156 | + assert list(lock.keys()) == ["cmd", "deps", "params", "outs"] |
| 157 | + assert list(lock["params"].keys()) == ["params.yaml", "params2.yaml"] |
| 158 | + |
| 159 | + # # params keys are always sorted by the name |
| 160 | + assert list(lock["params"]["params.yaml"].keys()) == [ |
| 161 | + "floats", |
| 162 | + "lists", |
| 163 | + "nested.nested1", |
| 164 | + "nested.nested1.nested2", |
| 165 | + ] |
| 166 | + assert list(lock["params"]["params2.yaml"]) == ["answer", "lists", "name"] |
| 167 | + |
| 168 | + assert not dvc.reproduce(stage.addressing) |
| 169 | + |
| 170 | + # let's change the order of params and dump them in pipeline file |
| 171 | + params, _ = get_params_deps(stage) |
| 172 | + for param in params: |
| 173 | + param.params.reverse() |
| 174 | + |
| 175 | + stage.dvcfile._dump_pipeline_file(stage) |
| 176 | + assert not dvc.reproduce(stage.addressing) |
| 177 | + |
| 178 | + (tmp_dir / PIPELINE_LOCK).unlink() |
| 179 | + # XXX: temporary workaround due to lack of params support in build cache |
| 180 | + remove(os.path.join(dvc.cache.local.cache_dir, "stages")) |
| 181 | + |
| 182 | + assert dvc.reproduce(stage.addressing) == [stage] |
| 183 | + assert_eq_lockfile(initial_content, read_lock_file()) |
0 commit comments