Skip to content

Commit feb5fb4

Browse files
authored
Merge pull request #10801 from Shivansh-007/invalid-git-version-tests
2 parents 9c8a2e4 + 5086e57 commit feb5fb4

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

tests/functional/test_vcs_git.py

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""
22
Contains functional tests of the Git class.
33
"""
4+
import logging
45
import os
56
import pathlib
67
from typing import List, Optional, Tuple
7-
from unittest.mock import patch
8+
from unittest.mock import Mock, patch
89

910
import pytest
1011

@@ -305,7 +306,7 @@ def _initialize_clonetest_server(
305306
repo_path.mkdir()
306307
script.run("git", "init", cwd=str(repo_path))
307308
repo_file = repo_path / "file.txt"
308-
repo_file.write_text(u".")
309+
repo_file.write_text(".")
309310
script.run("git", "add", "file.txt", cwd=str(repo_path))
310311
script.run("git", "commit", "-m", "initial commit", cwd=str(repo_path))
311312

@@ -319,6 +320,34 @@ def _initialize_clonetest_server(
319320
return repo_file
320321

321322

323+
@pytest.mark.parametrize(
324+
"version_out, expected_message",
325+
(
326+
("git version -2.25.1", "Can't parse git version: git version -2.25.1"),
327+
("git version 2.a.1", "Can't parse git version: git version 2.a.1"),
328+
("git ver. 2.25.1", "Can't parse git version: git ver. 2.25.1"),
329+
),
330+
)
331+
@patch("pip._internal.vcs.versioncontrol.VersionControl.run_command")
332+
def test_git_parse_fail_warning(
333+
mock_run_command: Mock,
334+
caplog: pytest.LogCaptureFixture,
335+
version_out: str,
336+
expected_message: str,
337+
) -> None:
338+
"""Test invalid git version logs adds an explicit warning log."""
339+
mock_run_command.return_value = version_out
340+
341+
caplog.set_level(logging.WARNING)
342+
343+
git_tuple = Git().get_git_version()
344+
# Returns an empty tuple if it is an invalid git version
345+
assert git_tuple == ()
346+
347+
# Check for warning log
348+
assert expected_message in caplog.text.strip()
349+
350+
322351
@pytest.mark.skipif(Git().get_git_version() < (2, 17), reason="git too old")
323352
def test_partial_clone(script: PipTestEnvironment, tmp_path: pathlib.Path) -> None:
324353
"""Test partial clone w/ a git-server that supports it"""
@@ -347,7 +376,7 @@ def test_partial_clone(script: PipTestEnvironment, tmp_path: pathlib.Path) -> No
347376
)
348377

349378
# Write some additional stuff to git pull
350-
repo_file.write_text(u"..")
379+
repo_file.write_text("..")
351380
script.run("git", "commit", "-am", "second commit", cwd=str(repo_path))
352381

353382
# Make sure git pull works - with server supporting filtering
@@ -391,7 +420,7 @@ def test_partial_clone_without_server_support(
391420
)
392421

393422
# Write some additional stuff to git pull
394-
repo_file.write_text(u"..")
423+
repo_file.write_text("..")
395424
script.run("git", "commit", "-am", "second commit", cwd=str(repo_path))
396425

397426
# Make sure git pull works - even though server doesn't support filtering
@@ -424,7 +453,7 @@ def test_clone_without_partial_clone_support(
424453
verbosity=0,
425454
)
426455

427-
repo_file.write_text(u"...")
456+
repo_file.write_text("...")
428457
script.run("git", "commit", "-am", "third commit", cwd=str(repo_path))
429458

430459
# Should work fine w/o attempting to use `--filter` args

0 commit comments

Comments
 (0)