analytics: collect execution info only when available

This commit is contained in:
Mr. Outis
2019-12-07 21:14:31 -06:00
parent 2145daa130
commit c50bd17bb4
3 changed files with 33 additions and 11 deletions
+6 -7
View File
@@ -37,13 +37,12 @@ def collect_and_send_report(args=None, return_code=None):
"""
report = _runtime_info()
# Include command execution information on the report
report.update(
{
"cmd_class": args.func.__name__ if hasattr(args, "func") else None,
"cmd_return_code": return_code,
}
)
# Include command execution information on the report only when available.
if args and hasattr(args, "func"):
report.update({"cmd_class": args.func.__name__})
if return_code is not None:
report.update({"cmd_return_code": return_code})
with tempfile.NamedTemporaryFile(delete=False, mode="w") as fobj:
json.dump(report, fobj)
+2 -4
View File
@@ -12,11 +12,9 @@ def test_daemon_analytics(mock_send, tmp_path):
mock_send.assert_called_with(report)
@mock.patch("dvc.daemon._spawn")
@mock.patch("dvc.analytics.collect_and_send_report")
@mock.patch("dvc.analytics.is_enabled", return_value=True)
@mock.patch("dvc.analytics._runtime_info", return_value={})
def test_main_analytics(mock_is_enabled, mock_daemon, mock_report, dvc_repo):
def test_main_analytics(mock_is_enabled, mock_report, dvc_repo):
assert 0 == main(["add", "foo"])
assert mock_is_enabled.called
assert mock_report.called
assert mock_daemon.called
+25
View File
@@ -6,6 +6,7 @@ import json
from voluptuous import Schema, Any
from dvc import analytics
from dvc.cli import parse_args
from dvc.utils.compat import str, builtin_str
@@ -23,6 +24,30 @@ def tmp_global_config(tmp_path):
yield
@mock.patch("dvc.daemon._spawn")
@mock.patch("json.dump")
def test_collect_and_send_report(mock_json, mock_daemon, tmp_global_config):
analytics.collect_and_send_report()
report = mock_json.call_args[0][0]
with pytest.raises(KeyError):
report["cmd_class"]
with pytest.raises(KeyError):
report["cmd_return_code"]
args = parse_args(['add', 'foo'])
return_code = 0
analytics.collect_and_send_report(args, return_code)
report = mock_json.call_args[0][0]
assert report["cmd_class"] == "CmdAdd"
assert report["cmd_return_code"] == return_code
assert mock_daemon.call_count == 2
def test_runtime_info(tmp_global_config):
schema = Schema(
{