|
| 1 | +import json |
1 | 2 | from urllib.parse import urljoin |
2 | 3 |
|
3 | 4 | import pytest |
|
9 | 10 | DVC_STUDIO_TOKEN, |
10 | 11 | DVC_STUDIO_URL, |
11 | 12 | ) |
12 | | -from dvc.utils.studio import STUDIO_URL, config_to_env, env_to_config, notify_refs |
| 13 | +from dvc.repo import Repo |
| 14 | +from dvc.utils.studio import ( |
| 15 | + STUDIO_URL, |
| 16 | + config_to_env, |
| 17 | + env_to_config, |
| 18 | + get_dvc_experiment_parent_data, |
| 19 | + get_subrepo_relpath, |
| 20 | + notify_refs, |
| 21 | +) |
13 | 22 |
|
14 | 23 | CONFIG = {"offline": True, "repo_url": "repo_url", "token": "token", "url": "url"} |
15 | 24 |
|
@@ -67,3 +76,97 @@ def test_config_to_env(): |
67 | 76 |
|
68 | 77 | def test_env_to_config(): |
69 | 78 | assert env_to_config(ENV) == CONFIG |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.studio |
| 82 | +def test_monorepo_relpath(tmp_dir, scm): |
| 83 | + from dvc.repo.destroy import destroy |
| 84 | + |
| 85 | + tmp_dir.gen({"project_a": {}, "subdir/project_b": {}}) |
| 86 | + |
| 87 | + non_monorepo = Repo.init(tmp_dir) |
| 88 | + assert get_subrepo_relpath(non_monorepo) == "" |
| 89 | + |
| 90 | + destroy(non_monorepo) |
| 91 | + |
| 92 | + monorepo_project_a = Repo.init(tmp_dir / "project_a", subdir=True) |
| 93 | + |
| 94 | + assert get_subrepo_relpath(monorepo_project_a) == "project_a" |
| 95 | + |
| 96 | + monorepo_project_b = Repo.init(tmp_dir / "subdir" / "project_b", subdir=True) |
| 97 | + |
| 98 | + assert get_subrepo_relpath(monorepo_project_b) == "subdir/project_b" |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.studio |
| 102 | +def test_virtual_monorepo_relpath(tmp_dir, scm): |
| 103 | + from dvc.fs.git import GitFileSystem |
| 104 | + from dvc.repo.destroy import destroy |
| 105 | + |
| 106 | + tmp_dir.gen({"project_a": {}, "subdir/project_b": {}}) |
| 107 | + scm.commit("initial commit") |
| 108 | + gfs = GitFileSystem(scm=scm, rev="master") |
| 109 | + |
| 110 | + non_monorepo = Repo.init(tmp_dir) |
| 111 | + non_monorepo.fs = gfs |
| 112 | + non_monorepo.root_dir = "/" |
| 113 | + |
| 114 | + assert get_subrepo_relpath(non_monorepo) == "" |
| 115 | + |
| 116 | + destroy(non_monorepo) |
| 117 | + |
| 118 | + monorepo_project_a = Repo.init(tmp_dir / "project_a", subdir=True) |
| 119 | + monorepo_project_a.fs = gfs |
| 120 | + monorepo_project_a.root_dir = "/project_a" |
| 121 | + |
| 122 | + assert get_subrepo_relpath(monorepo_project_a) == "project_a" |
| 123 | + |
| 124 | + monorepo_project_b = Repo.init(tmp_dir / "subdir" / "project_b", subdir=True) |
| 125 | + monorepo_project_b.fs = gfs |
| 126 | + monorepo_project_b.root_dir = "/subdir/project_b" |
| 127 | + |
| 128 | + assert get_subrepo_relpath(monorepo_project_b) == "subdir/project_b" |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.studio |
| 132 | +def test_dvc_experiment_parent_data(M, tmp_dir, scm, dvc): |
| 133 | + parent_shas = [scm.get_rev()] |
| 134 | + |
| 135 | + for i in range(5): |
| 136 | + tmp_dir.scm_gen({"metrics.json": json.dumps({"metric": i})}, commit=f"step {i}") |
| 137 | + parent_shas.insert(0, scm.get_rev()) |
| 138 | + |
| 139 | + title = "a final commit with a fairly long message" |
| 140 | + message = f"{title}\nthat is split over two lines" |
| 141 | + |
| 142 | + tmp_dir.scm_gen({"metrics.json": json.dumps({"metric": 100})}, commit=message) |
| 143 | + |
| 144 | + head_sha = scm.get_rev() |
| 145 | + |
| 146 | + assert isinstance(head_sha, str) |
| 147 | + assert head_sha not in parent_shas |
| 148 | + |
| 149 | + dvc_experiment_parent_data = get_dvc_experiment_parent_data(dvc, head_sha) |
| 150 | + |
| 151 | + assert dvc_experiment_parent_data is not None |
| 152 | + assert isinstance(dvc_experiment_parent_data["date"], str) |
| 153 | + |
| 154 | + assert dvc_experiment_parent_data == { |
| 155 | + "author": { |
| 156 | + "email": "dvctester@example.com", |
| 157 | + "name": "DVC Tester", |
| 158 | + }, |
| 159 | + "date": M.any, |
| 160 | + "message": message, |
| 161 | + "parent_shas": parent_shas, |
| 162 | + "title": title, |
| 163 | + "sha": head_sha, |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | +@pytest.mark.parametrize("func", ["get_rev", "resolve_commit"]) |
| 168 | +@pytest.mark.studio |
| 169 | +def test_no_dvc_experiment_parent_data(mocker, scm, dvc, func): |
| 170 | + mocker.patch.object(scm, func, return_value=None) |
| 171 | + |
| 172 | + assert get_dvc_experiment_parent_data(dvc, scm.get_rev()) is None |
0 commit comments