|
| 1 | +import sys |
| 2 | +from contextlib import redirect_stderr, redirect_stdout |
| 3 | +from subprocess import CalledProcessError |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +def run_ramalama_direct(args): |
| 9 | + """Run ramalama directly via Python import to avoid installation issues""" |
| 10 | + from ramalama.cli import main |
| 11 | + |
| 12 | + # Save original sys.argv |
| 13 | + original_argv = sys.argv[:] |
| 14 | + |
| 15 | + try: |
| 16 | + sys.argv = ["ramalama"] + args |
| 17 | + # Capture stdout by redirecting |
| 18 | + import io |
| 19 | + |
| 20 | + stdout_capture = io.StringIO() |
| 21 | + stderr_capture = io.StringIO() |
| 22 | + |
| 23 | + with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture): |
| 24 | + try: |
| 25 | + main() |
| 26 | + except SystemExit as e: |
| 27 | + # argparse calls sys.exit(), capture the output |
| 28 | + stdout_content = stdout_capture.getvalue() |
| 29 | + stderr_content = stderr_capture.getvalue() |
| 30 | + |
| 31 | + if e.code != 0: # argparse help exits with 0 |
| 32 | + # If there was an error, raise CalledProcessError |
| 33 | + raise CalledProcessError(e.code, args, stdout_content + stderr_content) |
| 34 | + |
| 35 | + return stdout_content |
| 36 | + |
| 37 | + # If no exception, return the captured output |
| 38 | + return stdout_capture.getvalue() |
| 39 | + |
| 40 | + finally: |
| 41 | + # Always restore original sys.argv |
| 42 | + sys.argv = original_argv |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.e2e |
| 46 | +def test_max_tokens_cli_argument_help(): |
| 47 | + """Test that --max-tokens argument appears in help for supported commands""" |
| 48 | + |
| 49 | + # Test commands that should have --max-tokens |
| 50 | + supported_commands = ["run", "serve", "perplexity"] |
| 51 | + |
| 52 | + for command in supported_commands: |
| 53 | + result = run_ramalama_direct([command, "--help"]) |
| 54 | + assert "--max-tokens" in result, f"--max-tokens should appear in {command} help" |
| 55 | + assert "maximum number of tokens to generate" in result, f"Help text should be present in {command}" |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.e2e |
| 59 | +def test_max_tokens_argument_parsing(): |
| 60 | + """Test that --max-tokens argument is properly parsed""" |
| 61 | + |
| 62 | + # Test that --max-tokens doesn't cause argument parsing errors |
| 63 | + # by checking help with the argument present |
| 64 | + try: |
| 65 | + result = run_ramalama_direct(["run", "--max-tokens", "512", "--help"]) |
| 66 | + # If we get here, the argument was parsed successfully |
| 67 | + assert "--max-tokens" in result |
| 68 | + except CalledProcessError as e: |
| 69 | + # Should not fail with "unrecognized arguments" for --max-tokens |
| 70 | + assert "unrecognized arguments: --max-tokens" not in str(e), f"Argument parsing failed: {e}" |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.e2e |
| 74 | +def test_max_tokens_valid_values(): |
| 75 | + """Test that max_tokens accepts valid integer values""" |
| 76 | + |
| 77 | + # Test with various valid integer values |
| 78 | + valid_values = ["0", "100", "1024", "4096"] |
| 79 | + |
| 80 | + for value in valid_values: |
| 81 | + try: |
| 82 | + result = run_ramalama_direct(["run", "--max-tokens", value, "--help"]) |
| 83 | + # Should not raise parsing errors |
| 84 | + assert "--max-tokens" in result |
| 85 | + except CalledProcessError as e: |
| 86 | + assert "unrecognized arguments" not in str(e), f"Should accept valid value {value}" |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.e2e |
| 90 | +def test_max_tokens_default_value(): |
| 91 | + """Test that max_tokens has a sensible default value""" |
| 92 | + |
| 93 | + result = run_ramalama_direct(["run", "--help"]) |
| 94 | + |
| 95 | + # Check that the default is mentioned in help (should show 0) |
| 96 | + # Look for the max-tokens line and check it shows default: 0 |
| 97 | + lines = result.split('\n') |
| 98 | + max_tokens_lines = [line for line in lines if '--max-tokens' in line or 'maximum number of tokens' in line] |
| 99 | + |
| 100 | + # Should have at least one line mentioning max-tokens |
| 101 | + assert max_tokens_lines, "Should have help text for --max-tokens" |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.e2e |
| 105 | +def test_max_tokens_invalid_value(): |
| 106 | + """Test that max_tokens rejects invalid values""" |
| 107 | + |
| 108 | + # Test with invalid string value (should be rejected by argparse type checking) |
| 109 | + try: |
| 110 | + run_ramalama_direct(["run", "--max-tokens", "invalid", "--help"]) |
| 111 | + # If no exception, this is unexpected but we'll allow it for now |
| 112 | + except CalledProcessError as e: |
| 113 | + # Should fail due to invalid type conversion, not unrecognized argument |
| 114 | + assert "unrecognized arguments: --max-tokens" not in str(e) |
| 115 | + # argparse should complain about invalid int conversion |
| 116 | + assert "invalid" in str(e) or "int" in str(e).lower() |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.e2e |
| 120 | +def test_max_tokens_negative_value(): |
| 121 | + """Test that max_tokens accepts negative values (though they may be treated as 0)""" |
| 122 | + |
| 123 | + # Negative values should be accepted by argparse (int type allows them) |
| 124 | + try: |
| 125 | + result = run_ramalama_direct(["run", "--max-tokens", "-1", "--help"]) |
| 126 | + # Should not raise parsing errors |
| 127 | + assert "--max-tokens" in result |
| 128 | + except CalledProcessError as e: |
| 129 | + # Should not fail with "unrecognized arguments" for --max-tokens |
| 130 | + assert "unrecognized arguments: --max-tokens" not in str(e) |
0 commit comments