Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dvc: normalize targets before entering brancher #7966

Merged
merged 1 commit into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions dvc/repo/diff.py
Expand Up @@ -5,6 +5,7 @@

from dvc.exceptions import PathMissingError
from dvc.repo import locked
from dvc.utils.collections import ensure_list

logger = logging.getLogger(__name__)

Expand All @@ -25,6 +26,8 @@ def diff(self, a_rev="HEAD", b_rev=None, targets=None):
from dvc.fs.dvc import DvcFileSystem

dvcfs = DvcFileSystem(repo=self)
targets = ensure_list(targets)
targets = [dvcfs.from_os_path(target) for target in targets]

b_rev = b_rev if b_rev else "workspace"
results = {}
Expand All @@ -36,15 +39,15 @@ def diff(self, a_rev="HEAD", b_rev=None, targets=None):
continue

targets_paths = None
if targets is not None:
if targets:
# convert targets to paths, and capture any missing targets
targets_paths, missing_targets[rev] = _targets_to_paths(
dvcfs, targets
)

results[rev] = _paths_checksums(self, targets_paths)

if targets is not None:
if targets:
# check for overlapping missing targets between a_rev and b_rev
for target in set(missing_targets[a_rev]) & set(
missing_targets[b_rev]
Expand Down
4 changes: 4 additions & 0 deletions dvc/repo/metrics/show.py
Expand Up @@ -11,6 +11,7 @@
from dvc.repo.live import summary_fs_path
from dvc.scm import NoSCMError
from dvc.utils import error_handler, errored_revisions, onerror_collect
from dvc.utils.collections import ensure_list
from dvc.utils.serialize import load_yaml

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -113,6 +114,9 @@ def show(
if onerror is None:
onerror = onerror_collect

targets = ensure_list(targets)
targets = [repo.dvcfs.from_os_path(target) for target in targets]

res = {}
for rev in repo.brancher(
revs=revs,
Expand Down
4 changes: 4 additions & 0 deletions dvc/repo/params/show.py
Expand Up @@ -21,6 +21,7 @@
from dvc.stage import PipelineStage
from dvc.ui import ui
from dvc.utils import error_handler, errored_revisions, onerror_collect
from dvc.utils.collections import ensure_list
from dvc.utils.serialize import LOADERS

if TYPE_CHECKING:
Expand Down Expand Up @@ -131,6 +132,9 @@ def show(
onerror = onerror_collect
res = {}

targets = ensure_list(targets)
targets = [repo.dvcfs.from_os_path(target) for target in targets]

for branch in repo.brancher(revs=revs):
params = error_handler(_gather_params)(
repo=repo,
Expand Down
2 changes: 2 additions & 0 deletions dvc/repo/plots/__init__.py
Expand Up @@ -112,6 +112,8 @@ def collect(
from dvc.utils.collections import ensure_list

targets = ensure_list(targets)
targets = [self.repo.dvcfs.from_os_path(target) for target in targets]

for rev in self.repo.brancher(revs=revs):
# .brancher() adds unwanted workspace
if revs is not None and rev not in revs:
Expand Down
13 changes: 13 additions & 0 deletions tests/func/metrics/test_show.py
Expand Up @@ -31,6 +31,19 @@ def test_show(tmp_dir, dvc, run_copy_metrics):
}


def test_show_targets(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("metrics_t.yaml", "foo: 1.1")
run_copy_metrics(
"metrics_t.yaml", "metrics.yaml", metrics=["metrics.yaml"]
)
expected = {"": {"data": {"metrics.yaml": {"data": {"foo": 1.1}}}}}
assert dvc.metrics.show(targets=["metrics.yaml"]) == expected
assert (
dvc.metrics.show(targets=(tmp_dir / "metrics.yaml").fs_path)
== expected
)


def test_show_multiple(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("foo_temp", "foo: 1\n")
tmp_dir.gen("baz_temp", "baz: 2\n")
Expand Down
10 changes: 10 additions & 0 deletions tests/func/params/test_show.py
Expand Up @@ -20,6 +20,16 @@ def test_show(tmp_dir, dvc):
}


def test_show_targets(tmp_dir, dvc):
tmp_dir.gen("params.yaml", "foo: bar")
dvc.run(cmd="echo params.yaml", params=["foo"], single_stage=True)
expected = {"": {"data": {"params.yaml": {"data": {"foo": "bar"}}}}}
assert dvc.params.show(targets=["params.yaml"]) == expected
assert (
dvc.params.show(targets=(tmp_dir / "params.yaml").fs_path) == expected
)


def test_show_toml(tmp_dir, dvc):
tmp_dir.gen("params.toml", "[foo]\nbar = 42\nbaz = [1, 2]\n")
dvc.run(
Expand Down
11 changes: 11 additions & 0 deletions tests/func/plots/test_show.py
Expand Up @@ -13,6 +13,17 @@
from tests.utils.plots import get_plot


def test_show_targets(tmp_dir, dvc):
metric = [{"first_val": 100, "val": 2}, {"first_val": 200, "val": 3}]
(tmp_dir / "metric.json").dump_json(metric, sort_keys=True)

plots = dvc.plots.show(targets=["metric.json"])
assert get_plot(plots, "workspace", file="metric.json") == metric

plots = dvc.plots.show(targets=(tmp_dir / "metric.json").fs_path)
assert get_plot(plots, "workspace", file="metric.json") == metric


def test_plot_cache_missing(tmp_dir, scm, dvc, caplog, run_copy_metrics):
metric1 = [{"y": 2}, {"y": 3}]
(tmp_dir / "metric_t.json").dump_json(metric1, sort_keys=True)
Expand Down
12 changes: 12 additions & 0 deletions tests/func/test_diff.py
Expand Up @@ -331,6 +331,18 @@ def test_no_commits(tmp_dir):
assert Repo.init().diff() == {}


def test_abs_target(tmp_dir, scm, dvc):
tmp_dir.dvc_gen("file", "text")

assert dvc.diff(targets=(tmp_dir / "file").fs_path) == {
"added": [{"path": "file", "hash": digest("text")}],
"deleted": [],
"modified": [],
"not in cache": [],
"renamed": [],
}


def setup_targets_test(tmp_dir):
tmp_dir.dvc_gen("file", "first", commit="add a file")

Expand Down