|
| 1 | +import os |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from ddtrace.sourcecode._utils import get_commit_sha |
| 5 | +from ddtrace.sourcecode._utils import get_repository_url |
| 6 | + |
| 7 | + |
| 8 | +class TestSourceCodeEnvVars: |
| 9 | + def test_get_commit_sha_uses_env_var_when_present(self): |
| 10 | + test_sha = "abc123def456" |
| 11 | + with mock.patch.dict(os.environ, {"DD_GIT_COMMIT_SHA": test_sha}): |
| 12 | + with mock.patch("ddtrace.sourcecode._utils._query_git") as mock_git: |
| 13 | + result = get_commit_sha() |
| 14 | + assert result == test_sha |
| 15 | + mock_git.assert_not_called() |
| 16 | + |
| 17 | + def test_get_commit_sha_calls_git_when_env_var_not_present(self): |
| 18 | + test_sha = "git_result_sha" |
| 19 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 20 | + with mock.patch("ddtrace.sourcecode._utils._query_git", return_value=test_sha) as mock_git: |
| 21 | + result = get_commit_sha() |
| 22 | + assert result == test_sha |
| 23 | + mock_git.assert_called_once_with(["rev-parse", "HEAD"]) |
| 24 | + |
| 25 | + def test_get_commit_sha_calls_git_when_env_var_empty(self): |
| 26 | + test_sha = "git_result_sha" |
| 27 | + with mock.patch.dict(os.environ, {"DD_GIT_COMMIT_SHA": ""}): |
| 28 | + with mock.patch("ddtrace.sourcecode._utils._query_git", return_value=test_sha) as mock_git: |
| 29 | + result = get_commit_sha() |
| 30 | + assert result == test_sha |
| 31 | + mock_git.assert_called_once_with(["rev-parse", "HEAD"]) |
| 32 | + |
| 33 | + def test_get_repository_url_uses_env_var_when_present(self): |
| 34 | + test_url = "https://github.com/user/repo.git" |
| 35 | + with mock.patch.dict(os.environ, {"DD_GIT_REPOSITORY_URL": test_url}): |
| 36 | + with mock.patch("ddtrace.sourcecode._utils._query_git") as mock_git: |
| 37 | + result = get_repository_url() |
| 38 | + assert result == test_url |
| 39 | + mock_git.assert_not_called() |
| 40 | + |
| 41 | + def test_get_repository_url_calls_git_when_env_var_not_present(self): |
| 42 | + test_url = "git_result_url" |
| 43 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 44 | + with mock.patch("ddtrace.sourcecode._utils._query_git", return_value=test_url) as mock_git: |
| 45 | + result = get_repository_url() |
| 46 | + assert result == test_url |
| 47 | + mock_git.assert_called_once_with(["config", "--get", "remote.origin.url"]) |
| 48 | + |
| 49 | + def test_get_repository_url_calls_git_when_env_var_empty(self): |
| 50 | + test_url = "git_result_url" |
| 51 | + with mock.patch.dict(os.environ, {"DD_GIT_REPOSITORY_URL": ""}): |
| 52 | + with mock.patch("ddtrace.sourcecode._utils._query_git", return_value=test_url) as mock_git: |
| 53 | + result = get_repository_url() |
| 54 | + assert result == test_url |
| 55 | + mock_git.assert_called_once_with(["config", "--get", "remote.origin.url"]) |
0 commit comments