# Copyright (c) Advanced Micro Devices, Inc. # SPDX-License-Identifier: MIT """ Unit tests for rocprof-compute general CLI options. """ import argparse from pathlib import Path from unittest.mock import patch import pytest from common import SUPPORTED_ARCHS from argparser import omniarg_parser HOME = Path.cwd() VERSION = {"ver_pretty": "rocprof-compute (unit test)"} def build_args(argv, experimental=False): """Construct the argument parser.""" parser = argparse.ArgumentParser( prog="tool", usage="rocprof-compute [mode] [options]" ) omniarg_parser(parser, HOME, SUPPORTED_ARCHS, VERSION, experimental) return parser.parse_args(argv) # ============================================================================= # -v / --version # ============================================================================= @pytest.mark.parametrize("flag", ["-v", "--version"]) def test_version_success_exits_zero(flag, capsys): with pytest.raises(SystemExit) as exc: build_args([flag]) assert exc.value.code == 0 assert "unit test" in capsys.readouterr().out # ============================================================================= # -h / --help # ============================================================================= @pytest.mark.parametrize("flag", ["-h", "--help"]) def test_help_success_exits_zero(flag, capsys): with pytest.raises(SystemExit) as exc: build_args([flag]) assert exc.value.code == 0 assert "usage" in capsys.readouterr().out.lower() @pytest.mark.parametrize("flag", ["-h", "--help"]) def test_help_rejects_explicit_value(flag, capsys): with pytest.raises(SystemExit) as exc: build_args([f"{flag}=now"]) assert exc.value.code == 2 assert "--help" in capsys.readouterr().err # ============================================================================= # -V / --verbose # ============================================================================= @pytest.mark.parametrize("flag", ["-V", "--verbose"]) def test_verbose_success_counts(flag): assert build_args([]).verbose == 0 assert build_args([flag]).verbose == 1 assert build_args([flag, flag, flag]).verbose == 3 @pytest.mark.parametrize("flag", ["-V", "--verbose"]) def test_verbose_rejects_explicit_value(flag, capsys): with pytest.raises(SystemExit) as exc: build_args([f"{flag}=2"]) assert exc.value.code == 2 assert "--verbose" in capsys.readouterr().err # ============================================================================= # -q / --quiet # ============================================================================= @pytest.mark.parametrize("flag", ["-q", "--quiet"]) def test_quiet_success_sets_flag(flag): assert build_args([]).quiet is False assert build_args([flag]).quiet is True @pytest.mark.parametrize("flag", ["-q", "--quiet"]) def test_quiet_rejects_explicit_value(flag, capsys): with pytest.raises(SystemExit) as exc: build_args([f"{flag}=loud"]) assert exc.value.code == 2 assert "--quiet" in capsys.readouterr().err # ============================================================================= # --config-dir # ============================================================================= def test_config_dir_success_stores_value(): assert build_args(["--config-dir", "/tmp/cfg"]).config_dir == "/tmp/cfg" def test_config_dir_requires_value(capsys): with pytest.raises(SystemExit) as exc: build_args(["--config-dir"]) assert exc.value.code == 2 assert "--config-dir" in capsys.readouterr().err def test_pc_sampling_analyze_options(): """Defaults, overrides, and validation for the analyze PC sampling options.""" defaults = build_args(["analyze"]) assert defaults.pc_sampling_sorting_type == "count" assert defaults.pc_sampling_rows == 10 overrides = build_args([ "analyze", "--pc-sampling-sorting-type", "offset", "--pc-sampling-rows", "25", ]) assert overrides.pc_sampling_sorting_type == "offset" assert overrides.pc_sampling_rows == 25 # 0 is allowed and means "show all rows". assert build_args(["analyze", "--pc-sampling-rows", "0"]).pc_sampling_rows == 0 # Negative row counts trigger an argparse error. with patch.object( argparse.ArgumentParser, "error", side_effect=SystemExit(2) ) as mock_error: with pytest.raises(SystemExit): build_args(["analyze", "--pc-sampling-rows", "-1"]) mock_error.assert_called_once()