From 929de7c65180347ab1807ee7ebb217f476e4347c Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Wed, 9 Nov 2022 12:02:23 +0800 Subject: [PATCH] Solve the crash on getting name of applied experiment branch fix: #8526 1. The current `get_names` will crash if an experiment branch had already been given a new branch name to it. 1. Solve the crash when we getting the name of a newly fixed experiment branch. 2. Add a new unit test to it. --- dvc/repo/experiments/show.py | 3 +-- tests/unit/repo/experiments/test_show.py | 27 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/unit/repo/experiments/test_show.py diff --git a/dvc/repo/experiments/show.py b/dvc/repo/experiments/show.py index 0ab3f4928..09653e636 100644 --- a/dvc/repo/experiments/show.py +++ b/dvc/repo/experiments/show.py @@ -248,8 +248,7 @@ def get_names(repo: "Repo", result: Dict[str, Dict[str, Any]]): rev = baseline if rev == "workspace": continue - name = names.get(rev, None) - name = name or exact_name[rev] + name = names.get(rev, None) or exact_name[rev] if name: rev_result["data"]["name"] = name diff --git a/tests/unit/repo/experiments/test_show.py b/tests/unit/repo/experiments/test_show.py new file mode 100644 index 000000000..378e5cf32 --- /dev/null +++ b/tests/unit/repo/experiments/test_show.py @@ -0,0 +1,27 @@ +from funcy import first +from scmrepo.git import Git + +from dvc.repo.experiments.show import get_names + + +def test_get_show_branch(tmp_dir, scm: "Git", dvc, exp_stage): + new_branch = "new" + + baseline = scm.get_rev() + base_branch = scm.active_branch() + results = dvc.experiments.run(exp_stage.addressing, params=["foo=2"]) + exp_a = first(results) + dvc.experiments.branch(exp_a, new_branch) + + scm.checkout(new_branch, force=True) + + result = { + "workspace": {"baseline": {"data": {}}}, + exp_a: {"baseline": {"data": {}}}, + baseline: {"baseline": {"data": {}}, exp_a: {"data": {}}}, + } + + get_names(dvc, result) + assert result[exp_a]["baseline"]["data"] == {"name": new_branch} + assert result[baseline]["baseline"]["data"] == {"name": base_branch} + assert result[baseline][exp_a]["data"] == {"name": new_branch}