Skip to content

Commit 71f1e3c

Browse files
Harshith-umeshkdeleesjmonson
authored
Add --version flag to guidellm CLI (#240)
Implements a --version flag for the GuideLLM CLI that displays the current version and exits cleanly. The `--version` flag is one of the most fundamental and expected features of any CLI tool. It's typically the first flag I try when exploring a new command-line application . ## Usage ```bash guidellm --version # Output: guidellm version: 0.3.0.dev40 ``` The version flag also appears in help output: ```bash guidellm --help ``` ## Testing Wrote 7 unit tests which pass successfully: ```bash pytest tests/unit/test_cli.py -v ``` --------- Signed-off-by: Harshith-umesh <[email protected]> Signed-off-by: Elijah DeLee <[email protected]> Signed-off-by: Samuel Monson <[email protected]> Co-authored-by: Elijah DeLee <[email protected]> Co-authored-by: Samuel Monson <[email protected]>
1 parent 20ce4d0 commit 71f1e3c

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

src/guidellm/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626

2727
@click.group()
28+
@click.version_option(package_name="guidellm", message="guidellm version: %(version)s")
2829
def cli():
2930
pass
3031

@@ -51,7 +52,7 @@ def benchmark():
5152
readable=True,
5253
file_okay=True,
5354
dir_okay=False,
54-
path_type=Path, # type: ignore[type-var]
55+
path_type=Path,
5556
),
5657
click.Choice(get_builtin_scenarios()),
5758
),

tests/unit/test_cli.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
Unit tests for CLI functionality, specifically the version flag.
3+
"""
4+
5+
import pytest
6+
from click.testing import CliRunner
7+
8+
from guidellm.__main__ import cli
9+
10+
11+
@pytest.mark.smoke
12+
def test_version_flag_long():
13+
"""Test that --version flag works correctly."""
14+
runner = CliRunner()
15+
result = runner.invoke(cli, ["--version"])
16+
17+
assert result.exit_code == 0
18+
assert "guidellm version:" in result.output
19+
assert result.output.strip().startswith("guidellm version:")
20+
21+
22+
@pytest.mark.smoke
23+
def test_version_flag_displays_actual_version():
24+
"""Test that --version displays the actual version from version.py."""
25+
runner = CliRunner()
26+
result = runner.invoke(cli, ["--version"])
27+
28+
assert result.exit_code == 0
29+
import re
30+
31+
version_pattern = r"guidellm version: \d+\.\d+"
32+
assert re.search(version_pattern, result.output)
33+
34+
35+
@pytest.mark.smoke
36+
def test_version_flag_exits_cleanly():
37+
"""Test that --version exits without processing other commands."""
38+
runner = CliRunner()
39+
result = runner.invoke(cli, ["--version", "benchmark"])
40+
41+
assert result.exit_code == 0
42+
assert "guidellm version:" in result.output
43+
assert "Commands to run a new benchmark" not in result.output
44+
45+
46+
@pytest.mark.smoke
47+
def test_help_shows_version_option():
48+
"""Test that --help shows the --version option."""
49+
runner = CliRunner()
50+
result = runner.invoke(cli, ["--help"])
51+
52+
assert result.exit_code == 0
53+
assert "--version" in result.output
54+
assert "Show the version and exit" in result.output
55+
56+
57+
@pytest.mark.smoke
58+
def test_other_commands_still_work():
59+
"""Test that other CLI commands still work after adding version flag."""
60+
runner = CliRunner()
61+
result = runner.invoke(cli, ["--help"])
62+
63+
assert result.exit_code == 0
64+
assert "benchmark" in result.output
65+
assert "config" in result.output
66+
assert "preprocess" in result.output
67+
68+
69+
@pytest.mark.smoke
70+
def test_version_flag_case_sensitivity():
71+
"""Test that --version flag is case sensitive."""
72+
runner = CliRunner()
73+
74+
result = runner.invoke(cli, ["--version"])
75+
assert result.exit_code == 0
76+
assert "guidellm version:" in result.output
77+
78+
# --VERSION should not work
79+
result = runner.invoke(cli, ["--VERSION"])
80+
assert result.exit_code != 0
81+
assert "No such option" in result.output
82+
83+
84+
@pytest.mark.integration
85+
def test_version_integration_with_actual_version():
86+
"""Integration test to verify version matches importlib.metadata."""
87+
import importlib.metadata
88+
89+
try:
90+
actual_version = importlib.metadata.version("guidellm")
91+
92+
runner = CliRunner()
93+
result = runner.invoke(cli, ["--version"])
94+
95+
assert result.exit_code == 0
96+
expected_output = f"guidellm version: {actual_version}"
97+
assert expected_output in result.output
98+
except importlib.metadata.PackageNotFoundError:
99+
# If package is not installed, the CLI should show an error
100+
# This is expected behavior when the package isn't properly installed
101+
runner = CliRunner()
102+
result = runner.invoke(cli, ["--version"])
103+
104+
# Click will handle the error when package is not found
105+
assert result.exit_code != 0

0 commit comments

Comments
 (0)