Standardize matching test files structure, add test_main.py and test_commands.py, and update GEMINI.md
This commit is contained in:
1
tests/env/clean_data.bin
vendored
Normal file
1
tests/env/clean_data.bin
vendored
Normal file
@@ -0,0 +1 @@
|
||||
this is a completely normal text file without flags.
|
||||
2
tests/env/flag_data.bin
vendored
2
tests/env/flag_data.bin
vendored
@@ -1 +1 @@
|
||||
garbage_data_here_flag{f0rens1cs_1s_fun}more_garbage
|
||||
random_data_here_flag{found_statically_in_file}more_data
|
||||
49
tests/test_commands.py
Normal file
49
tests/test_commands.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from pathlib import Path
|
||||
import toml
|
||||
from click.testing import CliRunner
|
||||
from ctf.commands import basic_group
|
||||
|
||||
TEST_ENV = Path("tests/env")
|
||||
|
||||
def test_basic_test_cli():
|
||||
"""
|
||||
Verifies that 'basic test' runs successfully and outputs 'hello from test'.
|
||||
"""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(basic_group, ["test"])
|
||||
assert result.exit_code == 0
|
||||
assert "hello from test" in result.output
|
||||
|
||||
def test_basic_greet_cli():
|
||||
"""
|
||||
Verifies that 'basic greet' runs successfully and greets the name argument.
|
||||
"""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(basic_group, ["greet", "Alice"])
|
||||
assert result.exit_code == 0
|
||||
assert "hello Alice" in result.output
|
||||
|
||||
def test_basic_set_flag_format_cli():
|
||||
"""
|
||||
Verifies that 'basic set-flag-format' CLI command writes the pattern to config.toml.
|
||||
"""
|
||||
from ctf.utils import load_config
|
||||
|
||||
runner = CliRunner()
|
||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
||||
|
||||
# Save the original config to restore later
|
||||
original_config = load_config(str(config_file))
|
||||
|
||||
try:
|
||||
result = runner.invoke(basic_group, ["set-flag-format", "TEST_FLAG{[a-z]+}"])
|
||||
assert result.exit_code == 0
|
||||
assert "Flag format set to" in result.output
|
||||
|
||||
# Verify it was written to config.toml
|
||||
updated_config = load_config(str(config_file))
|
||||
assert updated_config["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}"
|
||||
finally:
|
||||
# Restore original config
|
||||
from ctf.utils import write_config
|
||||
write_config(original_config, str(config_file))
|
||||
@@ -295,29 +295,3 @@ def test_forensics_flag_detect_cli_not_found():
|
||||
result = runner.invoke(forensics_group, ["flag-detect", str(test_file)])
|
||||
assert result.exit_code == 0
|
||||
assert "No flag patterns found" in result.output
|
||||
|
||||
def test_set_flag_format_cli():
|
||||
"""
|
||||
Verifies that 'set-flag-format' CLI command writes the pattern to config.toml.
|
||||
"""
|
||||
from ctf.commands import set_flag_format
|
||||
from ctf.utils import load_config
|
||||
|
||||
runner = CliRunner()
|
||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
||||
|
||||
# Save the original config to restore later
|
||||
original_config = load_config(str(config_file))
|
||||
|
||||
try:
|
||||
result = runner.invoke(set_flag_format, ["TEST_FLAG{[a-z]+}"])
|
||||
assert result.exit_code == 0
|
||||
assert "Flag format set to" in result.output
|
||||
|
||||
# Verify it was written to config.toml
|
||||
updated_config = load_config(str(config_file))
|
||||
assert updated_config["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}"
|
||||
finally:
|
||||
# Restore original config
|
||||
from ctf.utils import write_config
|
||||
write_config(original_config, str(config_file))
|
||||
|
||||
18
tests/test_main.py
Normal file
18
tests/test_main.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import sys
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
from ctf.main import main
|
||||
|
||||
def test_main_entry_point_help():
|
||||
"""
|
||||
Verifies that calling main() executes the Click CLI app and responds
|
||||
to '--help' by listing registration groups.
|
||||
"""
|
||||
# Mock sys.argv to simulate running 'ctf --help' from the shell
|
||||
with patch.object(sys, "argv", ["ctf", "--help"]):
|
||||
# Click calls sys.exit() after displaying help, raising SystemExit
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
# Verify it exits with a successful exit code (0)
|
||||
assert exc_info.value.code == 0
|
||||
Reference in New Issue
Block a user