Skip to content

Commit

Permalink
consolidate config report cleanup fixture with conftest.py
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Martin <[email protected]>
  • Loading branch information
jmartin-tech committed Apr 26, 2024
1 parent 8ae224e commit a5af4ac
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 78 deletions.
20 changes: 1 addition & 19 deletions tests/analyze/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
temp_prefix = "_garak_internal_test_temp"


@pytest.fixture(scope="session", autouse=True)
@pytest.fixture(autouse=True)
def garak_tiny_run() -> None:
cli.main(["-m", "test.Blank", "-p", "test.Blank", "--report_prefix", temp_prefix])

Expand Down Expand Up @@ -41,21 +41,3 @@ def test_report_digest_runs():
check=True,
)
assert result.returncode == 0


@pytest.fixture(scope="session", autouse=True)
def cleanup(request):
"""Cleanup a testing directory once we are finished."""

def remove_logs():
logs = [
temp_prefix + ".report.jsonl",
temp_prefix + ".report.html",
]
for file in logs:
try:
os.remove(file)
except FileNotFoundError:
pass

request.addfinalizer(remove_logs)
4 changes: 1 addition & 3 deletions tests/buffs/test_buff_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ def remove_buff_reports():
f"{prefix}.hitlog.jsonl",
]
for file in files:
try:
if os.path.exists(file):
os.remove(file)
except FileNotFoundError:
pass

request.addfinalizer(remove_buff_reports)
21 changes: 0 additions & 21 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,6 @@
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")


@pytest.fixture(autouse=True)
def cleanup(request):
"""Cleanup a testing and report directory once we are finished."""

def remove_log_files():
files = []
if _config.transient.reportfile is not None:
_config.transient.reportfile.close()
report_html_file = _config.transient.report_filename.replace(
".jsonl", ".html"
)
files.append(_config.transient.report_filename)
files.append(report_html_file)

for file in files:
if os.path.exists(file):
os.remove(file)

request.addfinalizer(remove_log_files)


def test_version_command(capsys):
cli.main(["--version"])
result = capsys.readouterr()
Expand Down
28 changes: 28 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
import os
from garak import _config


@pytest.fixture(autouse=True)
def config_report_cleanup(request):
"""Cleanup a testing and report directory once we are finished."""

def remove_log_files():
files = []
if _config.transient.reportfile is not None:
_config.transient.reportfile.close()
report_html_file = _config.transient.report_filename.replace(
".jsonl", ".html"
)
hitlog_file = _config.transient.report_filename.replace(
".report.", ".hitlog."
)
files.append(_config.transient.report_filename)
files.append(report_html_file)
files.append(hitlog_file)

for file in files:
if os.path.exists(file):
os.remove(file)

request.addfinalizer(remove_log_files)
10 changes: 1 addition & 9 deletions tests/generators/test_ggml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@
model_name = None


@pytest.fixture(autouse=True)
def reset_env(request) -> None:
if stored_env is not None:
os.environ[garak.generators.ggml.ENV_VAR] = stored_env
model_name = tempfile.NamedTemporaryFile(suffix="_test_model.gguf")


@pytest.fixture(autouse=True)
def set_fake_env() -> None:
os.environ[garak.generators.ggml.ENV_VAR] = os.path.abspath(__file__)


def test_init_bad_app():
with pytest.raises(RuntimeError) as exc_info:
if os.getenv(garak.generators.ggml.ENV_VAR) is not None:
del os.environ[garak.generators.ggml.ENV_VAR]
del os.environ[garak.generators.ggml.ENV_VAR]
garak.generators.ggml.GgmlGenerator(model_name)
assert "not provided by environment" in str(exc_info.value)

Expand Down
26 changes: 0 additions & 26 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,29 +485,3 @@ def test_report_prefix_with_hitlog_no_explode():
assert os.path.isfile("kjsfhgkjahpsfdg.report.jsonl")
assert os.path.isfile("kjsfhgkjahpsfdg.report.html")
assert os.path.isfile("kjsfhgkjahpsfdg.hitlog.jsonl")


@pytest.fixture(autouse=True)
def cleanup(request):
"""Cleanup a testing and report directory once we are finished."""

def remove_log_files():
files = [
"laurelhurst.report.jsonl",
"kjsfhgkjahpsfdg.report.jsonl",
"kjsfhgkjahpsfdg.report.html",
"kjsfhgkjahpsfdg.hitlog.jsonl",
]
if _config.transient.reportfile is not None:
_config.transient.reportfile.close()
report_html_file = _config.transient.report_filename.replace(
".jsonl", ".html"
)
files.append(_config.transient.report_filename)
files.append(report_html_file)

for file in files:
if os.path.exists(file):
os.remove(file)

request.addfinalizer(remove_log_files)

0 comments on commit a5af4ac

Please sign in to comment.