From 8715d73aa66994dc2e6200f59d68db6f7e1a3084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Thu, 1 Dec 2022 18:04:47 +0545 Subject: [PATCH] add tests --- dvc/repo/metrics/show.py | 4 +++- dvc/repo/params/show.py | 4 +++- tests/func/metrics/test_diff.py | 29 +++++++++++++++++++++++++++++ tests/func/params/test_diff.py | 27 +++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py index 8445e0afef1..93168fcb82c 100644 --- a/dvc/repo/metrics/show.py +++ b/dvc/repo/metrics/show.py @@ -34,7 +34,9 @@ def _collect_top_level_metrics(repo): wdir = repo.dvcfs.path.relpath( repo.dvcfs.path.parent(dvcfile), repo.root_dir ) - yield from (repo.dvcfs.path.join(wdir, file) for file in metrics) + for file in metrics: + path = repo.dvcfs.path.join(wdir, file) + yield repo.dvcfs.path.normpath(path) def _collect_metrics(repo, targets, revision, recursive): diff --git a/dvc/repo/params/show.py b/dvc/repo/params/show.py index a3d3aeaef0e..685d3cef8d1 100644 --- a/dvc/repo/params/show.py +++ b/dvc/repo/params/show.py @@ -41,7 +41,9 @@ def _collect_top_level_params(repo): wdir = repo.dvcfs.path.relpath( repo.dvcfs.path.parent(dvcfile), repo.root_dir ) - yield from (repo.dvcfs.path.join(wdir, file) for file in params) + for file in params: + path = repo.dvcfs.path.join(wdir, file) + yield repo.dvcfs.path.normpath(path) def _collect_configs( diff --git a/tests/func/metrics/test_diff.py b/tests/func/metrics/test_diff.py index c88085d31ab..23adde515c5 100644 --- a/tests/func/metrics/test_diff.py +++ b/tests/func/metrics/test_diff.py @@ -1,6 +1,10 @@ import json +from os.path import join + +import pytest from dvc.cli import main +from dvc.utils import relpath def test_metrics_diff_simple(tmp_dir, scm, dvc, run_copy_metrics): @@ -206,3 +210,28 @@ def _gen(val): assert result == { "some_file.yaml": {"foo": {"old": 1, "new": 3, "diff": 2}} } + + +@pytest.mark.parametrize( + "dvcfile, metrics_file", + [ + ("dvc.yaml", "my_metrics.yaml"), + ("dir/dvc.yaml", "my_metrics.yaml"), + ("dir/dvc.yaml", join("..", "my_metrics.yaml")), + ], +) +def test_diff_top_level_metrics(tmp_dir, dvc, scm, dvcfile, metrics_file): + directory = (tmp_dir / dvcfile).parent + directory.mkdir(exist_ok=True) + (tmp_dir / dvcfile).dump({"metrics": [metrics_file]}) + + metrics_file = directory / metrics_file + metrics_file.dump({"foo": 3}) + scm.add_commit([metrics_file, tmp_dir / dvcfile], message="add metrics") + + metrics_file.dump({"foo": 5}) + assert dvc.metrics.diff() == { + relpath(directory / metrics_file): { + "foo": {"diff": 2, "new": 5, "old": 3} + } + } diff --git a/tests/func/params/test_diff.py b/tests/func/params/test_diff.py index f409e401245..ba181ba38b2 100644 --- a/tests/func/params/test_diff.py +++ b/tests/func/params/test_diff.py @@ -1,3 +1,5 @@ +from os.path import join + import pytest from dvc.utils import relpath @@ -241,3 +243,28 @@ def test_diff_without_targets_specified(tmp_dir, dvc, scm, file): "y": {"new": "100", "old": None}, } } + + +@pytest.mark.parametrize( + "dvcfile, params_file", + [ + ("dvc.yaml", "my_params.yaml"), + ("dir/dvc.yaml", "my_params.yaml"), + ("dir/dvc.yaml", join("..", "my_params.yaml")), + ], +) +def test_diff_top_level_params(tmp_dir, dvc, scm, dvcfile, params_file): + directory = (tmp_dir / dvcfile).parent + directory.mkdir(exist_ok=True) + (tmp_dir / dvcfile).dump({"params": [params_file]}) + + params_file = directory / params_file + params_file.dump({"foo": 3}) + scm.add_commit([params_file, tmp_dir / dvcfile], message="add params") + + params_file.dump({"foo": 5}) + assert dvc.params.diff() == { + relpath(directory / params_file): { + "foo": {"diff": 2, "new": 5, "old": 3} + } + }