def test_load_when_lockfile_is_corrupted(tmp_dir, dvc, corrupt_data): with open("Dvcfile.lock", "w+") as f: yaml.dump(corrupt_data, f) lockfile = Lockfile(dvc, "Dvcfile.lock") with pytest.raises(LockfileCorruptedError) as exc_info: lockfile.load() assert "Dvcfile.lock" in str(exc_info.value)
def test_stage_dump_with_deps_and_outs(tmp_dir, dvc): data = { "s1": { "cmd": "command", "deps": [{ "md5": "1.txt", "path": "checksum" }], "outs": [{ "md5": "2.txt", "path": "checksum" }], } } dump_yaml("path.lock", data) lockfile = Lockfile(dvc, "path.lock") stage = PipelineStage(name="s2", repo=dvc, path="path", cmd="command2") lockfile.dump(stage) assert lockfile.load() == { "schema": "2.0", "stages": { **data, "s2": { "cmd": "command2" } }, }
def test_stage_dump_no_outs_deps(tmp_dir, dvc): stage = PipelineStage(name="s1", repo=dvc, path="path", cmd="command") lockfile = Lockfile(dvc, "path.lock") lockfile.dump(stage) assert lockfile.load() == { "schema": "2.0", "stages": {"s1": {"cmd": "command"}}, }
def test_stage_dump_when_already_exists(tmp_dir, dvc): data = {"s1": {"cmd": "command", "deps": [], "outs": []}} dump_yaml("path.lock", data) stage = PipelineStage(name="s2", repo=dvc, path="path", cmd="command2") lockfile = Lockfile(dvc, "path.lock") lockfile.dump(stage) assert lockfile.load() == { "schema": "2.0", "stages": {**data, "s2": {"cmd": "command2"}}, }
def test_stage_dump_when_already_exists(tmp_dir, dvc): data = {"s1": {"cmd": "command", "deps": [], "outs": []}} with open("path.lock", "w+") as f: yaml.dump(data, f) stage = PipelineStage(name="s2", repo=dvc, path="path", cmd="command2") lockfile = Lockfile(dvc, "path.lock") lockfile.dump(stage) assert lockfile.load() == { **data, "s2": {"cmd": "command2"}, }
def test_stage_overwrites_if_already_exists(tmp_dir, dvc): lockfile = Lockfile(dvc, "path.lock",) stage = PipelineStage(name="s2", repo=dvc, path="path", cmd="command2") lockfile.dump(stage) stage = PipelineStage(name="s2", repo=dvc, path="path", cmd="command3") lockfile.dump(stage) assert lockfile.load() == { "s2": {"cmd": "command3"}, }
def test_lockfile_invalid_versions(tmp_dir, dvc, version_info): lockdata = {**version_info, "stages": {"foo": {"cmd": "echo foo"}}} dump_yaml("dvc.lock", lockdata) with pytest.raises(LockfileCorruptedError) as exc_info: Lockfile(dvc, tmp_dir / "dvc.lock").load() assert str(exc_info.value) == "Lockfile 'dvc.lock' is corrupted." assert (str(exc_info.value.__cause__) == "'dvc.lock' format error: " f"invalid schema version {version_info['schema']}, " "expected one of ['2.0'] for dictionary value @ " "data['schema']")
def test_lockfile_invalid_versions(tmp_dir, dvc, version_info): lockdata = {**version_info, "stages": {"foo": {"cmd": "echo foo"}}} (tmp_dir / "dvc.lock").dump(lockdata) with pytest.raises(YAMLValidationError) as exc_info: Lockfile(dvc, tmp_dir / "dvc.lock").load() rel = make_relpath("dvc.lock") assert f"'{rel}' validation failed" in str(exc_info.value) assert (str(exc_info.value.__cause__) == f"invalid schema version {version_info['schema']}, " "expected one of ['2.0'] for dictionary value @ " "data['schema']")
def test_try_loading_lockfile_that_is_gitignored(tmp_dir, dvc, scm, dvcignored, file_exists): # it should raise error if the file is git-ignored, even if: # 1. The file does not exist at all. # 2. Or, is dvc-ignored. files = [".gitignore"] if dvcignored: files.append(".dvcignore") for file in files: with (tmp_dir / file).open(mode="a+") as fd: fd.write("dvc.lock") if file_exists: (tmp_dir / "dvc.lock").write_text("") scm._reset() with pytest.raises(FileIsGitIgnored) as exc_info: Lockfile(dvc, "dvc.lock").load() assert str(exc_info.value) == "'dvc.lock' is git-ignored."
def test_load_when_lockfile_does_not_exist(tmp_dir, dvc): assert {} == Lockfile(dvc, "pipelines.lock").load()