Unify metrics behavior with dirs (#3935)

* run: disable directories for -m/-M flags

* run: reformatted string literals

* run: simplified metrics check

* tests: func: run: dirs as metrics outs

* tests: func: run: reformatted string literals

* tests: use pytest

Co-authored-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
nik123
2020-06-04 23:32:00 +03:00
committed by GitHub
co-authored by Ruslan Kuprieiev
parent 74bd8137ba
commit 5bbaa28cae
4 changed files with 34 additions and 6 deletions
+2 -3
View File
@@ -133,7 +133,7 @@ def add_parser(subparsers, parent_parser):
"--metrics",
action="append",
default=[],
help="Declare output metric file or directory.",
help="Declare output metric file.",
metavar="<path>",
)
run_parser.add_argument(
@@ -141,8 +141,7 @@ def add_parser(subparsers, parent_parser):
"--metrics-no-cache",
action="append",
default=[],
help="Declare output metric file or directory "
"(do not put into DVC cache).",
help="Declare output metric file (do not put into DVC cache).",
metavar="<path>",
)
run_parser.add_argument(
+3 -2
View File
@@ -246,10 +246,11 @@ class BaseOutput:
self.ignore()
if self.metric or self.plot:
self.verify_metric()
if not self.use_cache:
self.info = self.save_info()
if self.metric or self.plot:
self.verify_metric()
if not self.IS_DEPENDENCY:
logger.debug(
"Output '%s' doesn't use cache. Skipping saving.", self
+1 -1
View File
@@ -81,7 +81,7 @@ class LocalOutput(BaseOutput):
name = "metrics" if self.metric else "plot"
if os.path.isdir(path):
msg = "directory '{}' cannot be used as {}."
raise DvcException(msg.format(self.path_info, name))
raise IsADirectoryError(msg.format(self.path_info, name))
if not istextfile(path):
msg = "binary file '{}' cannot be used as {}."
+28
View File
@@ -989,3 +989,31 @@ def test_should_raise_on_stage_output(tmp_dir, dvc, run_copy):
with pytest.raises(OutputIsStageFileError):
run_copy("foo", "name.dvc", single_stage=True)
class TestRunDirMetrics:
@pytest.fixture(autouse=True)
def setup(self, dvc):
with open("script.py", "w+") as fobj:
fobj.write("import sys\n")
fobj.write("import os\n")
fobj.write("os.makedirs(sys.argv[1])\n")
fobj.write(
"with open(os.path.join(sys.argv[1], "
"'metrics.json'), 'a+') as fobj:\n"
)
fobj.write(" fobj.write('foo')\n")
def test_metrics_dir_cached(self, dvc):
with pytest.raises(IsADirectoryError):
dvc.run(
cmd="python script.py dir", metrics=["dir"], single_stage=True,
)
def test_metrics_dir_not_cached(self, dvc):
with pytest.raises(IsADirectoryError):
dvc.run(
cmd="python script.py dir",
metrics_no_cache=["dir"],
single_stage=True,
)