fix: awxkit help flags for detailed & general help

Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
This commit is contained in:
Adrià Sala
2026-02-27 13:32:15 +01:00
committed by Adrià Sala
parent 670dfeed25
commit 51ed59c506
5 changed files with 220 additions and 18 deletions

View File

@@ -1,5 +1,7 @@
import pytest
from requests.exceptions import ConnectionError
import sys
from unittest.mock import patch
from awxkit.cli import run, CLI
@@ -53,3 +55,136 @@ def test_list_resources(capfd, resource):
assert "usage:" in out
for snippet in ('--conf.host https://example.awx.org]', '-v, --verbose'):
assert snippet in out
class TestHelpHandling:
"""Test suite for improved help handling functionality"""
def test_get_non_option_args_basic(self):
"""Test _get_non_option_args extracts non-option arguments correctly"""
cli = CLI()
cli.argv = ['awx', 'users', 'list', '--verbose']
result = cli._get_non_option_args()
assert result == ['users', 'list']
def test_get_non_option_args_with_flags(self):
"""Test _get_non_option_args ignores option flags"""
cli = CLI()
cli.argv = ['awx', '--conf.host', 'example.com', 'jobs', 'create', '--name', 'test']
result = cli._get_non_option_args()
# Should include all non-option arguments (including flag values) except 'awx'
assert result == ['example.com', 'jobs', 'create', 'test']
def test_get_non_option_args_before_help(self):
"""Test _get_non_option_args with before_help=True stops at help flag"""
cli = CLI()
cli.argv = ['awx', 'users', '--help', 'extra', 'args']
result = cli._get_non_option_args(before_help=True)
assert result == ['users']
def test_get_non_option_args_before_help_short_flag(self):
"""Test _get_non_option_args with before_help=True stops at -h flag"""
cli = CLI()
cli.argv = ['awx', 'projects', '-h', 'should', 'not', 'appear']
result = cli._get_non_option_args(before_help=True)
assert result == ['projects']
def test_get_non_option_args_no_help_flag(self):
"""Test _get_non_option_args when help flag not present"""
cli = CLI()
cli.argv = ['awx', 'organizations', 'list']
result = cli._get_non_option_args(before_help=True)
assert result == ['organizations', 'list']
def test_is_main_help_request_true(self):
"""Test _is_main_help_request returns True for main CLI help"""
cli = CLI()
cli.argv = ['awx', '--help']
result = cli._is_main_help_request()
assert result is True
def test_is_main_help_request_short_flag(self):
"""Test _is_main_help_request returns True for main CLI help with -h"""
cli = CLI()
cli.argv = ['awx', '-h']
result = cli._is_main_help_request()
assert result is True
def test_is_main_help_request_false_subcommand(self):
"""Test _is_main_help_request returns False for subcommand help"""
cli = CLI()
cli.argv = ['awx', 'users', '--help']
result = cli._is_main_help_request()
assert result is False
def test_is_main_help_request_false_action(self):
"""Test _is_main_help_request returns False for action help"""
cli = CLI()
cli.argv = ['awx', 'jobs', 'create', '--help']
result = cli._is_main_help_request()
assert result is False
def test_is_main_help_request_false_no_help(self):
"""Test _is_main_help_request returns False when no help flag"""
cli = CLI()
cli.argv = ['awx', 'users', 'list']
result = cli._is_main_help_request()
assert result is False
def test_early_help_return_main_cli(self):
"""Test that main CLI help exits early without server connection"""
cli = CLI()
# Verify that _is_main_help_request works correctly
cli.argv = ['awx', '--help']
assert cli._is_main_help_request() is True
# Test that parse_args with main help flag should exit
with patch.object(sys, 'exit') as mock_exit:
cli.parse_args(['awx', '--help'])
mock_exit.assert_called_once_with(0)
def test_no_early_exit_for_subcommand_help(self):
"""Test that subcommand help does not exit early"""
with patch.object(sys, 'exit') as mock_exit:
cli = CLI()
# This should not exit early since it's subcommand help
cli.parse_args(['awx', 'users', '--help'])
mock_exit.assert_not_called()
def test_help_property_detection(self):
"""Test that help property correctly detects help flags"""
cli = CLI()
cli.argv = ['awx', '--help']
assert cli.help is True
cli.argv = ['awx', '-h']
assert cli.help is True
cli.argv = ['awx', 'users', '--help']
assert cli.help is True
cli.argv = ['awx', 'users', 'list']
assert cli.help is False
def test_short_help_flag_added(self):
"""Test that -h flag is properly added to argument parser"""
cli = CLI()
cli.parse_args(['awx'])
# Verify that both -h and --help are recognized
help_actions = [action for action in cli.parser._actions if '--help' in action.option_strings]
assert len(help_actions) == 1
assert '-h' in help_actions[0].option_strings
assert '--help' in help_actions[0].option_strings

View File

@@ -251,3 +251,24 @@ class TestSettingsOptions(unittest.TestCase):
out = StringIO()
self.parser.choices['modify'].print_help(out)
assert 'modify [-h] key value' in out.getvalue()
class TestHelpParameterChanges(unittest.TestCase):
"""Test that add_help parameter changes work correctly"""
def test_add_help_parameter_handling(self):
"""Test that add_help=True and add_help=False work as expected"""
# Test add_help=True (for action parsers like list, create)
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='action')
list_parser = subparsers.add_parser('list', help='', add_help=True)
out = StringIO()
list_parser.print_help(out)
help_text = out.getvalue()
assert 'show this help message and exit' in help_text
# Test add_help=False (for resource parsers like users, jobs)
resource_parser = subparsers.add_parser('users', help='', add_help=False)
help_actions = [action for action in resource_parser._actions if '--help' in action.option_strings]
assert len(help_actions) == 0 # Should be 0 because add_help=False