analytics: send remote types and git-remote's path hashes (#10816)

This commit is contained in:
skshetry
2025-07-28 15:07:41 +05:45
committed by GitHub
parent ba1281a41c
commit dc13186759
4 changed files with 135 additions and 25 deletions
+17 -6
View File
@@ -2,6 +2,7 @@ import os
import pytest
from dvc import __version__, env
from dvc.analytics import _scm_in_use, collect_and_send_report
from dvc.cli import main
from dvc.repo import Repo
@@ -33,7 +34,8 @@ def mock_daemon(mocker):
return mocker.patch("dvc.daemon.daemon", mocker.MagicMock(side_effect=func))
def test_collect_and_send_report(mocker, dvc, mock_daemon):
def test_collect_and_send_report(monkeypatch, mocker, dvc, mock_daemon):
monkeypatch.delenv(env.DVC_ANALYTICS_ENDPOINT, raising=False)
mock_post = mocker.patch("requests.post")
collect_and_send_report()
@@ -41,19 +43,28 @@ def test_collect_and_send_report(mocker, dvc, mock_daemon):
assert mock_post.call_count == 1
assert mock_post.call_args == mocker.call(
"https://analytics.dvc.org",
json=ANY(dict),
json={
"dvc_version": __version__,
"scm_class": type(dvc.scm).__name__,
"is_binary": False,
"system_info": ANY(dict),
"user_id": ANY(str),
"group_id": mocker.ANY,
"remotes": ANY(list),
"git_remote_hash": None,
},
headers={"content-type": "application/json"},
timeout=5,
)
def test_scm_dvc_only(tmp_dir, dvc):
scm = _scm_in_use()
scm = _scm_in_use(dvc.scm)
assert scm == "NoSCM"
def test_scm_git(tmp_dir, scm, dvc):
scm = _scm_in_use()
scm = _scm_in_use(scm)
assert scm == "Git"
@@ -62,7 +73,7 @@ def test_scm_subrepo(tmp_dir, scm):
subdir.mkdir()
with subdir.chdir():
Repo.init(subdir=True)
scm = _scm_in_use()
repo = Repo.init(subdir=True)
scm = _scm_in_use(repo.scm)
assert scm == "Git"
+41 -2
View File
@@ -1,10 +1,11 @@
import hashlib
import json
import platform
import pytest
from voluptuous import Any, Schema
from dvc import analytics
from dvc import analytics, env
from dvc.cli import parse_args
@@ -50,6 +51,8 @@ def test_runtime_info(tmp_global_dir):
"user_id": str,
"system_info": dict,
"group_id": Any(str, None),
"remotes": Any(list, None),
"git_remote_hash": Any(str, None),
},
required=True,
)
@@ -57,7 +60,8 @@ def test_runtime_info(tmp_global_dir):
assert schema(analytics._runtime_info())
def test_send(mocker, tmp_path):
def test_send(monkeypatch, mocker, tmp_path):
monkeypatch.delenv(env.DVC_ANALYTICS_ENDPOINT, raising=False)
mock_post = mocker.patch("requests.post")
import requests
@@ -158,3 +162,38 @@ def test_system_info():
)
assert schema(analytics._system_info())
@pytest.mark.parametrize(
"git_remote",
[
"git://github.com/iterative/dvc.git",
"[email protected]:iterative/dvc.git",
"http://github.com/iterative/dvc.git",
"https://github.com/iterative/dvc.git",
"ssh://[email protected]/iterative/dvc.git",
],
)
def test_git_remote_hash(mocker, git_remote):
m = mocker.patch("dvc.analytics._git_remote_url", return_value=git_remote)
expected = hashlib.md5(b"iterative/dvc.git").hexdigest()
assert analytics._git_remote_path_hash(None) == expected
m.assert_called_once_with(None)
@pytest.mark.parametrize(
"git_remote",
[
"C:\\Users\\user\\dvc.git",
"/home/user/dvc.git",
"file:///home/user/dvc.git",
"./dvc.git",
],
)
def test_git_remote_hash_local(mocker, git_remote):
m = mocker.patch("dvc.analytics._git_remote_url", return_value=git_remote)
expected = hashlib.md5(git_remote.encode("utf-8")).hexdigest()
assert analytics._git_remote_path_hash(None) == expected
m.assert_called_once_with(None)