mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
Fix issue while sending analytics report (#6407)
* demonstrate issue while sending analytics report When sending analytics, we are not flushing the data to the temp file which causes failure at times, especially on Windows * close file before calling daemonized analytics send command This way, the contents bufferred will get a chance to be flushed.
This commit is contained in:
+1
-1
@@ -32,7 +32,7 @@ def collect_and_send_report(args=None, return_code=None):
|
||||
|
||||
with tempfile.NamedTemporaryFile(delete=False, mode="w") as fobj:
|
||||
json.dump(report, fobj)
|
||||
daemon(["analytics", fobj.name])
|
||||
daemon(["analytics", fobj.name])
|
||||
|
||||
|
||||
def is_enabled():
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import os
|
||||
from unittest import mock
|
||||
from unittest.mock import call
|
||||
|
||||
from dvc.analytics import _scm_in_use
|
||||
import pytest
|
||||
|
||||
from dvc.analytics import _scm_in_use, collect_and_send_report
|
||||
from dvc.main import main
|
||||
from dvc.repo import Repo
|
||||
|
||||
@@ -23,6 +26,40 @@ def test_main_analytics(mock_is_enabled, mock_report, tmp_dir, dvc):
|
||||
assert mock_report.called
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_daemon(mocker):
|
||||
def func(argv):
|
||||
return main(["daemon", *argv])
|
||||
|
||||
m = mocker.patch("dvc.daemon.daemon", mocker.MagicMock(side_effect=func))
|
||||
yield m
|
||||
|
||||
|
||||
class ANY:
|
||||
def __init__(self, expected_type):
|
||||
self.expected_type = expected_type
|
||||
|
||||
def __repr__(self):
|
||||
return "Any" + self.expected_type.__name__.capitalize()
|
||||
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, self.expected_type)
|
||||
|
||||
|
||||
@mock.patch("requests.post")
|
||||
def test_collect_and_send_report(mock_post, dvc, mock_daemon):
|
||||
collect_and_send_report()
|
||||
|
||||
assert mock_daemon.call_count == 1
|
||||
assert mock_post.call_count == 1
|
||||
assert mock_post.call_args == call(
|
||||
"https://analytics.dvc.org",
|
||||
json=ANY(dict),
|
||||
headers={"content-type": "application/json"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
|
||||
def test_scm_dvc_only(tmp_dir, dvc):
|
||||
scm = _scm_in_use()
|
||||
assert scm == "NoSCM"
|
||||
|
||||
Reference in New Issue
Block a user