77
88import pytest
99
10+ from libvcs ._vendor .version import Version
1011from libvcs .cmd import git
1112
1213
@@ -28,7 +29,11 @@ def test_version_basic(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path)
2829 monkeypatch .setattr (git_cmd , "run" , lambda * args , ** kwargs : "git version 2.43.0" )
2930
3031 result = git_cmd .version ()
31- assert result == "git version 2.43.0"
32+ assert isinstance (result , Version )
33+ assert result .major == 2
34+ assert result .minor == 43
35+ assert result .micro == 0
36+ assert str (result ) == "2.43.0"
3237
3338
3439def test_version_with_build_options (
@@ -53,7 +58,11 @@ def mock_run(cmd_args: list[str], **kwargs: t.Any) -> str:
5358 monkeypatch .setattr (git_cmd , "run" , mock_run )
5459
5560 result = git_cmd .version (build_options = True )
56- assert result == sample_output
61+ assert isinstance (result , Version )
62+ assert result .major == 2
63+ assert result .minor == 43
64+ assert result .micro == 0
65+ assert str (result ) == "2.43.0"
5766
5867
5968def test_build_options (monkeypatch : pytest .MonkeyPatch , tmp_path : pathlib .Path ) -> None :
@@ -67,9 +76,12 @@ def test_build_options(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path)
6776sizeof-size_t: 8
6877shell-path: /bin/sh"""
6978
70- # We need to fix the parsing for "no commit associated with this build"
71- # in the build_options() method
72- monkeypatch .setattr (git_cmd , "version" , lambda ** kwargs : sample_output )
79+ # Mock run() directly instead of version()
80+ def mock_run (cmd_args : list [str ], ** kwargs : t .Any ) -> str :
81+ assert cmd_args == ["version" , "--build-options" ]
82+ return sample_output
83+
84+ monkeypatch .setattr (git_cmd , "run" , mock_run )
7385
7486 result = git_cmd .build_options ()
7587
@@ -89,12 +101,54 @@ def test_build_options_invalid_version(
89101 """Test build_options() with invalid version string."""
90102 git_cmd = git .Git (path = tmp_path )
91103
92- sample_output = "git version development"
104+ sample_output = """git version development
105+ cpu: x86_64
106+ commit: abcdef123456
107+ sizeof-long: 8
108+ sizeof-size_t: 8
109+ shell-path: /bin/sh"""
110+
111+ def mock_run (cmd_args : list [str ], ** kwargs : t .Any ) -> str :
112+ assert cmd_args == ["version" , "--build-options" ]
113+ return sample_output
93114
94- monkeypatch .setattr (git_cmd , "version " , lambda ** kwargs : sample_output )
115+ monkeypatch .setattr (git_cmd , "run " , mock_run )
95116
96117 result = git_cmd .build_options ()
97118
98119 assert isinstance (result , git .GitVersionInfo )
99120 assert result .version == "development"
100121 assert result .version_info is None
122+ assert result .commit == "abcdef123456"
123+
124+
125+ def test_version_invalid_format (
126+ monkeypatch : pytest .MonkeyPatch , tmp_path : pathlib .Path
127+ ) -> None :
128+ """Test version() with invalid output format."""
129+ git_cmd = git .Git (path = tmp_path )
130+
131+ invalid_output = "not a git version format"
132+
133+ monkeypatch .setattr (git_cmd , "run" , lambda * args , ** kwargs : invalid_output )
134+
135+ with pytest .raises (git .InvalidVersion ) as excinfo :
136+ git_cmd .version ()
137+
138+ assert "Unexpected git version output format" in str (excinfo .value )
139+
140+
141+ def test_build_options_invalid_format (
142+ monkeypatch : pytest .MonkeyPatch , tmp_path : pathlib .Path
143+ ) -> None :
144+ """Test build_options() with invalid output format."""
145+ git_cmd = git .Git (path = tmp_path )
146+
147+ invalid_output = "not a git version format"
148+
149+ monkeypatch .setattr (git_cmd , "run" , lambda * args , ** kwargs : invalid_output )
150+
151+ with pytest .raises (git .InvalidBuildOptions ) as excinfo :
152+ git_cmd .build_options ()
153+
154+ assert "Unexpected git version output format" in str (excinfo .value )
0 commit comments