Skip to content

Add --version flag to guidellm CLI #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/guidellm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,33 @@
from guidellm.utils import DefaultGroupHandler
from guidellm.utils import cli as cli_tools

# Import version information
try:
from guidellm.version import version
except ImportError:
version = "unknown"

STRATEGY_PROFILE_CHOICES = list(
set(list(get_args(ProfileType)) + list(get_args(StrategyType)))
)


def _version_callback(ctx: click.Context, _param: click.Parameter, value: bool) -> None:
"""Callback for --version flag."""
if value:
click.echo(f"guidellm version: {version}")
ctx.exit()


@click.group()
@click.option(
"--version",
is_flag=True,
expose_value=False,
is_eager=True,
help="Show the version and exit.",
callback=_version_callback,
)
def cli():
pass

Expand All @@ -51,7 +72,7 @@ def benchmark():
readable=True,
file_okay=True,
dir_okay=False,
path_type=Path, # type: ignore[type-var]
path_type=Path,
),
click.Choice(get_builtin_scenarios()),
),
Expand Down
103 changes: 103 additions & 0 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""
Unit tests for CLI functionality, specifically the version flag.
"""

import pytest
from click.testing import CliRunner

from guidellm.__main__ import cli


@pytest.mark.smoke
def test_version_flag_long():
"""Test that --version flag works correctly."""
runner = CliRunner()
result = runner.invoke(cli, ["--version"])

assert result.exit_code == 0
assert "guidellm version:" in result.output
assert result.output.strip().startswith("guidellm version:")


@pytest.mark.smoke
def test_version_flag_displays_actual_version():
"""Test that --version displays the actual version from version.py."""
runner = CliRunner()
result = runner.invoke(cli, ["--version"])

assert result.exit_code == 0
import re

version_pattern = r"guidellm version: \d+\.\d+"
assert re.search(version_pattern, result.output)


@pytest.mark.smoke
def test_version_flag_exits_cleanly():
"""Test that --version exits without processing other commands."""
runner = CliRunner()
result = runner.invoke(cli, ["--version", "benchmark"])

assert result.exit_code == 0
assert "guidellm version:" in result.output
assert "Commands to run a new benchmark" not in result.output


@pytest.mark.smoke
def test_help_shows_version_option():
"""Test that --help shows the --version option."""
runner = CliRunner()
result = runner.invoke(cli, ["--help"])

assert result.exit_code == 0
assert "--version" in result.output
assert "Show the version and exit" in result.output


@pytest.mark.smoke
def test_other_commands_still_work():
"""Test that other CLI commands still work after adding version flag."""
runner = CliRunner()
result = runner.invoke(cli, ["--help"])

assert result.exit_code == 0
assert "benchmark" in result.output
assert "config" in result.output
assert "preprocess" in result.output


@pytest.mark.smoke
def test_version_flag_case_sensitivity():
"""Test that --version flag is case sensitive."""
runner = CliRunner()

result = runner.invoke(cli, ["--version"])
assert result.exit_code == 0
assert "guidellm version:" in result.output

# --VERSION should not work
result = runner.invoke(cli, ["--VERSION"])
assert result.exit_code != 0
assert "No such option" in result.output


@pytest.mark.integration
def test_version_integration_with_actual_version():
"""Integration test to verify version matches what's in version.py."""
try:
from guidellm.version import (
version as actual_version,
)

runner = CliRunner()
result = runner.invoke(cli, ["--version"])

assert result.exit_code == 0
expected_output = f"guidellm version: {actual_version}"
assert expected_output in result.output
except ImportError:
runner = CliRunner()
result = runner.invoke(cli, ["--version"])

assert result.exit_code == 0
assert "guidellm version: unknown" in result.output
Loading