Skip to content

Commit fbd984d

Browse files
committed
Add tests for commodore version
1 parent 7b73b6d commit fbd984d

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

tests/test_cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,8 @@ def test_package_update_command():
102102
def test_package_sync_command():
103103
exit_status = call("commodore package sync --help", shell=True)
104104
assert exit_status == 0
105+
106+
107+
def test_version_command():
108+
exit_status = call("commodore version --help", shell=True)
109+
assert exit_status == 0

tests/test_version.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import sys
2+
3+
import pytest
4+
5+
from unittest.mock import patch
6+
from importlib.metadata import version as pyversion
7+
8+
from conftest import RunnerFunc
9+
10+
from commodore import version
11+
12+
13+
def test_version_cli(cli_runner: RunnerFunc):
14+
result = cli_runner(["version"])
15+
# NOTE(sg): exit code is 0 if all external tools are available
16+
assert result.exit_code == 0
17+
assert result.output.startswith("Commodore ")
18+
assert "Core dependency versions" in result.output
19+
assert f"kapitan: {pyversion('kapitan')}" in result.output
20+
assert f"gojsonnet: {pyversion('gojsonnet')}" in result.output
21+
assert f"reclass-rs: {pyversion('reclass-rs')}" in result.output
22+
assert "External tool versions" in result.output
23+
assert "helm: " in result.output
24+
assert "jb: " in result.output
25+
assert "kustomize: " in result.output
26+
27+
28+
def test_version_cli_missing_external(cli_runner: RunnerFunc, fs):
29+
# allow access to the real Python prefix (system or virtualenv)
30+
fs.add_real_directory(sys.prefix)
31+
result = cli_runner(["version"])
32+
assert "helm: NOT FOUND IN PATH" in result.output
33+
assert "jb: NOT FOUND IN PATH" in result.output
34+
assert "kustomize: NOT FOUND IN PATH" in result.output
35+
# NOTE(sg): exit code is 127 if external tools are missing. We use pyfakefs
36+
# to hide the host fs except for the Python prefix.
37+
assert result.exit_code == 127
38+
39+
40+
def test_version_native_find_so_non_native():
41+
with pytest.raises(ValueError) as exc:
42+
version._native_find_so("kapitan")
43+
assert "Unable to parse build info for kapitan: no unique *.so found" in str(exc)
44+
45+
46+
def test_version_gojsonnet_buildinfo_no_unique_so():
47+
def mock_find_so(_dep):
48+
raise ValueError("Mock Error")
49+
50+
with patch.object(version, "_native_find_so") as native_find_so_mock:
51+
native_find_so_mock.side_effect = mock_find_so
52+
build_info = version._buildinfo["gojsonnet"]()
53+
assert build_info == "Mock Error"

0 commit comments

Comments
 (0)