[GEMINI] Refactor test suite structure for new helpers and config modules

This commit is contained in:
venus
2026-07-19 00:53:00 -05:00
parent 8b82058cc6
commit 4bddd4a49b
6 changed files with 59 additions and 39 deletions

View File

@@ -27,7 +27,7 @@ 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
from ctf.config import load_config
runner = CliRunner()
config_file = Path("/home/venus/code/ctf/config.toml")
@@ -45,14 +45,14 @@ def test_basic_set_flag_format_cli():
assert updated_config["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}"
finally:
# Restore original config
from ctf.utils import write_config
from ctf.config import write_config
write_config(original_config, str(config_file))
def test_basic_set_flag_format_original_option_cli():
"""
Verifies that 'basic set-flag-format' with --original/-o option prompts for selection and writes it.
"""
from ctf.utils import load_config
from ctf.config import load_config
runner = CliRunner()
config_file = Path("/home/venus/code/ctf/config.toml")
@@ -78,7 +78,7 @@ def test_basic_set_flag_format_original_option_cli():
assert len(selected_pattern) > 0
finally:
# Restore original config
from ctf.utils import write_config
from ctf.config import write_config
write_config(original_config, str(config_file))
def test_basic_set_flag_format_validation_cli():
@@ -109,7 +109,7 @@ def test_basic_set_competition_cli():
"""
Verifies that 'basic set-competition' updates the competition name in config.toml.
"""
from ctf.utils import load_config
from ctf.config import load_config
runner = CliRunner()
config_file = Path("/home/venus/code/ctf/config.toml")
@@ -123,7 +123,7 @@ def test_basic_set_competition_cli():
updated_config = load_config(str(config_file))
assert updated_config["Competition"]["competition"] == "CyberCTF2026"
finally:
from ctf.utils import write_config
from ctf.config import write_config
write_config(original_config, str(config_file))
def test_basic_set_flag_format_warning_cli():
@@ -131,7 +131,7 @@ def test_basic_set_flag_format_warning_cli():
Verifies that if competition name is set and not present in the example flag,
set-flag-format outputs a warning.
"""
from ctf.utils import load_config, write_config
from ctf.config import load_config, write_config
runner = CliRunner()
config_file = Path("/home/venus/code/ctf/config.toml")

33
tests/test_config.py Normal file
View File

@@ -0,0 +1,33 @@
# tests/test_config.py
import toml
from pathlib import Path
from ctf.config import load_config, write_config
TEST_ENV = Path("tests/env")
# {{{ test_load_config_static
def test_load_config_static():
"""Verifies load_config using the persistent test file."""
config_file = TEST_ENV / "config.toml"
test_data = {
"Competition": {"name": "PersistentComp"},
"Enviroment": {"data_dir": str(TEST_ENV.absolute())}
}
config_file.parent.mkdir(parents=True, exist_ok=True)
with open(config_file, "w") as f:
toml.dump(test_data, f)
loaded_data = load_config(str(config_file))
assert loaded_data["Competition"]["name"] == "PersistentComp"
# }}}
# {{{ test_write_config_static
def test_write_config_static():
"""Verifies write_config by writing and reloading."""
config_file = TEST_ENV / "test_write_config.toml"
test_data = {"TestKey": "TestVal"}
write_config(test_data, str(config_file))
loaded = load_config(str(config_file))
assert loaded == test_data
# }}}

View File

@@ -133,20 +133,5 @@ def test_try_decode_metadata_rot13_unique_only():
assert "rot13" not in res_duplicate
# }}}
# {{{ test_check_for_flag
def test_check_for_flag():
"""
Verifies that check_for_flag extracts multiple matching flags or returns empty list.
"""
from ctf.decoding import check_for_flag
# Default fallback - matches multiple
res = check_for_flag("flag{one} and flag{two}")
assert set(res) == {"flag{one}", "flag{two}"}
# Custom format
res_custom = check_for_flag("custom{first} custom{second}", r"custom\{[a-z]+\}")
assert set(res_custom) == {"custom{first}", "custom{second}"}
# No matches
assert check_for_flag("no flag matches here") == []
# }}}

View File

@@ -4,7 +4,8 @@ import os
import stat
import sys
from click.testing import CliRunner
from ctf.utils import load_config, active_categories
from ctf.config import load_config
from ctf.utils import active_categories
from ctf.forensics import get_metadata
from ctf.cli_forensics import forensics_group, metadata
@@ -46,19 +47,6 @@ TEST_ENV = Path("tests/env")
# where POSIX ownership resolution or xattr is not native.
# =====================================================================
def test_load_config_static():
"""Verifies load_config using the persistent test file."""
config_file = TEST_ENV / "config.toml"
test_data = {
"Competition": {"name": "PersistentComp"},
"Enviroment": {"data_dir": str(TEST_ENV.absolute())}
}
config_file.parent.mkdir(parents=True, exist_ok=True)
with open(config_file, "w") as f:
toml.dump(test_data, f)
loaded_data = load_config(str(config_file))
assert loaded_data["Competition"]["name"] == "PersistentComp"
def test_active_categories_static():
"""Verifies active_categories using the pre-created 'comp1' folder."""

14
tests/test_helpers.py Normal file
View File

@@ -0,0 +1,14 @@
# tests/test_helpers.py
from ctf.helpers import check_for_flag
# {{{ test_check_for_flag
def test_check_for_flag():
"""
Verifies that check_for_flag extracts multiple matching flags or returns empty list.
"""
res = check_for_flag("flag{one} and flag{two}")
assert set(res) == {"flag{one}", "flag{two}"}
res_custom = check_for_flag("custom{first} custom{second}", r"custom\{[a-z]+\}")
assert set(res_custom) == {"custom{first}", "custom{second}"}
assert check_for_flag("no flag matches here") == []
# }}}

View File

@@ -66,7 +66,7 @@ def test_flag_detector_stream_persistence():
"""
from io import StringIO
from ctf.main import FlagDetectorStream
from ctf.utils import load_config, write_config
from ctf.config import load_config, write_config
from pathlib import Path
config_file = Path("/home/venus/code/ctf/config.toml")
@@ -89,7 +89,7 @@ def test_flag_cmd_cli():
"""
from click.testing import CliRunner
from ctf.main import cli
from ctf.utils import load_config, write_config
from ctf.config import load_config, write_config
from pathlib import Path
config_file = Path("/home/venus/code/ctf/config.toml")
@@ -123,7 +123,7 @@ def test_flag_cmd_exemption(capsys):
Verifies that running ctf flag is exempted from triggering the stdout flag warning box.
"""
from ctf.main import main
from ctf.utils import load_config, write_config
from ctf.config import load_config, write_config
from pathlib import Path
import sys
from unittest.mock import patch