Skip to content

Commit 79c7649

Browse files
authored
Enable windows tests as github action (#626)
* align path access for OS agnostic support * rely on os.sep when call in `split()` on path string * lean in on Path `/` operator when combining with a Path object Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * retrieve executable from runtime * avoid hard coded executbale path by asking runtime path * guard file removal during cleanup Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * force report digest output as `utf-8` Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * test updates for windows temp files Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * set encoding for all file operations without `binary` flag Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * portable temp file remove for 3.10+ Remove temp files manually for OS portablity in python 3.10+ `delete_on_close` for temp files is added in [3.12](python/cpython#97015) Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * add windows pytest Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * update ggml temp file Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * variable name precision Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> * manual windows testing only Signed-off-by: Jeffrey Martin <jemartin@nvidia.com> --------- Signed-off-by: Jeffrey Martin <jemartin@nvidia.com>
1 parent a36e276 commit 79c7649

27 files changed

Lines changed: 245 additions & 141 deletions

.github/workflows/test_windows.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Garak pytest - Windows
2+
3+
on: [workflow_dispatch]
4+
5+
jobs:
6+
build_windows:
7+
runs-on: windows-latest
8+
strategy:
9+
matrix:
10+
python-version: ["3.10","3.12"]
11+
steps:
12+
- uses: actions/checkout@v3
13+
with:
14+
path: garak
15+
16+
- name: Checkout ecoji for modified windows install
17+
uses: actions/checkout@v3
18+
with:
19+
repository: mecforlove/ecoji-py
20+
path: ecoji-py
21+
22+
- name: Set up Python ${{ matrix.python-version }}
23+
uses: actions/setup-python@v4
24+
with:
25+
python-version: ${{ matrix.python-version }}
26+
27+
- name: Install dependencies
28+
run: |
29+
python -m pip install --upgrade pip
30+
cd ecoji-py
31+
echo "mitigate" > README.md
32+
pip install setuptools
33+
python setup.py install
34+
cd ../garak
35+
pip install -r requirements.txt
36+
37+
- name: Test with pytest
38+
run: |
39+
cd garak
40+
python -m pytest tests/

garak/_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _store_config(settings_files) -> None:
114114

115115
def load_base_config() -> None:
116116
global loaded
117-
settings_files = [str(transient.basedir / "resources/garak.core.yaml")]
117+
settings_files = [str(transient.basedir / "resources" / "garak.core.yaml")]
118118
logging.debug("Loading configs from: %s", ",".join(settings_files))
119119
_store_config(settings_files=settings_files)
120120
loaded = True
@@ -127,7 +127,7 @@ def load_config(
127127
# and then not have cli be upset when these are not given as cli params
128128
global loaded
129129

130-
settings_files = [str(transient.basedir / "resources/garak.core.yaml")]
130+
settings_files = [str(transient.basedir / "resources" / "garak.core.yaml")]
131131

132132
fq_site_config_filename = str(transient.basedir / site_config_filename)
133133
if os.path.isfile(fq_site_config_filename):

garak/analyze/misp.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010

1111
from garak import _plugins
1212

13+
# does this utility really have access to _config?
14+
misp_resource_file = (
15+
garak._config.transient.basedir / "garak" / "resources" / "misp_descriptions.tsv"
16+
)
1317
misp_descriptions = {}
14-
if os.path.isfile("garak/resources/misp_descriptions.tsv"):
15-
with open("garak/resources/misp_descriptions.tsv", "r", encoding="utf-8") as f:
18+
if os.path.isfile(misp_resource_file):
19+
with open(misp_resource_file, "r", encoding="utf-8") as f:
1620
for line in f:
1721
key, title, descr = line.strip().split("\t")
1822
misp_descriptions[key] = (title, descr)

garak/analyze/report_avid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# load up a .jsonl output file, take in eval and config rows
3333
report_location = _config.args.report
3434
print(f"📜 Converting garak reports {report_location}")
35-
with open(report_location, "r") as reportfile:
35+
with open(report_location, "r", encoding="utf-8") as reportfile:
3636
for line in reportfile:
3737
record = json.loads(line.strip())
3838
if record["entry_type"] == "eval":
@@ -114,6 +114,6 @@
114114

115115
# save final output
116116
write_location = report_location.replace(".report", ".avid")
117-
with open(write_location, "w") as f:
117+
with open(write_location, "w", encoding="utf-8") as f:
118118
f.writelines(r.json() + "\n" for r in all_reports)
119119
print(f"📜 AVID reports generated at {write_location}")

garak/analyze/report_digest.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from garak import _config
1717

1818
templateLoader = jinja2.FileSystemLoader(
19-
searchpath=_config.transient.basedir / "analyze/templates/"
19+
searchpath=_config.transient.basedir / "analyze" / "templates"
2020
)
2121
templateEnv = jinja2.Environment(loader=templateLoader)
2222

@@ -28,13 +28,12 @@
2828
end_module = templateEnv.get_template("end_module.jinja")
2929

3030

31+
misp_resource_file = (
32+
_config.transient.basedir / "garak" / "resources" / "misp_descriptions.tsv"
33+
)
3134
misp_descriptions = {}
32-
if os.path.isfile(_config.transient.basedir / "garak/resources/misp_descriptions.tsv"):
33-
with open(
34-
_config.transient.basedir / "garak/resources/misp_descriptions.tsv",
35-
"r",
36-
encoding="utf-8",
37-
) as f:
35+
if os.path.isfile(misp_resource_file):
36+
with open(misp_resource_file, "r", encoding="utf-8") as f:
3837
for line in f:
3938
key, title, descr = line.strip().split("\t")
4039
misp_descriptions[key] = (title, descr)
@@ -70,7 +69,7 @@ def compile_digest(report_path, taxonomy=_config.reporting.taxonomy):
7069

7170
digest_content = header_template.render(
7271
{
73-
"reportfile": report_path.split("/")[-1],
72+
"reportfile": report_path.split(os.sep)[-1],
7473
"garak_version": garak_version,
7574
"start_time": start_time,
7675
"run_uuid": run_uuid,
@@ -223,4 +222,4 @@ def compile_digest(report_path, taxonomy=_config.reporting.taxonomy):
223222
if len(sys.argv) == 3:
224223
taxonomy = sys.argv[2]
225224
digest_content = compile_digest(report_path, taxonomy=taxonomy)
226-
print(digest_content)
225+
print(digest_content.encode("utf-8"))

garak/detectors/riskywords.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
from garak.detectors.base import StringDetector
1111

1212
surge_list = defaultdict(list)
13-
with open(_config.transient.basedir / "resources/profanity_en.csv", "r") as csvfile:
13+
with open(
14+
_config.transient.basedir / "resources" / "profanity_en.csv", "r", encoding="utf-8"
15+
) as csvfile:
1416
reader = csv.DictReader(csvfile)
1517
for row in reader:
1618
surge_list[row["category_1"]].append(row["text"])
@@ -22,7 +24,9 @@
2224
min_ofcom_strength = 2
2325
ofcom_list = defaultdict(list)
2426
with open(
25-
_config.transient.basedir / "resources/ofcom-potentially-offensive.txt", "r"
27+
_config.transient.basedir / "resources" / "ofcom-potentially-offensive.txt",
28+
"r",
29+
encoding="utf-8",
2630
) as csvfile:
2731
reader = csv.DictReader(csvfile, delimiter="\t")
2832
for row in reader:
@@ -279,7 +283,9 @@ class LDNOOBW(StringDetector):
279283
def __init__(self):
280284
substrings = []
281285
with open(
282-
_config.transient.basedir / "resources/ldnoobw-en.txt", "r"
286+
_config.transient.basedir / "resources" / "ldnoobw-en.txt",
287+
"r",
288+
encoding="utf-8",
283289
) as wordlist:
284290
for line in wordlist:
285291
if line.strip():

garak/detectors/specialwords.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
Matches terms in word lists
55
"""
66

7-
87
import json
98

109
from garak import _config
@@ -26,7 +25,7 @@ class SlursReclaimedSlurs(StringDetector):
2625
def __init__(self):
2726
self.substrings = set([])
2827
with open(
29-
_config.transient.basedir / "resources/slurprompts.jsonl",
28+
_config.transient.basedir / "resources" / "slurprompts.jsonl",
3029
"r",
3130
encoding="utf-8",
3231
) as slurfile:

garak/probes/continuation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self):
4343
# append these to each of the prefixes for that term
4444
slur_prefixes = defaultdict(set)
4545
with open(
46-
_config.transient.basedir / "resources/slurprompts_80.jsonl",
46+
_config.transient.basedir / "resources" / "slurprompts_80.jsonl",
4747
"r",
4848
encoding="utf-8",
4949
) as slurfile:

garak/probes/dan.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,11 @@ class AutoDANCached(Probe):
536536

537537
def __init__(
538538
self,
539-
autodan_prompt_location: str = str(
540-
garak._config.transient.basedir
541-
/ "resources/autodan/data/autodan_prompts.txt"
542-
),
539+
autodan_prompt_location: str = garak._config.transient.basedir
540+
/ "resources"
541+
/ "autodan"
542+
/ "data"
543+
/ "autodan_prompts.txt",
543544
):
544545
self.autodan_prompt_location = autodan_prompt_location
545546

@@ -668,7 +669,9 @@ def __init__(self):
668669
super().__init__()
669670

670671
inthewild_path = str(
671-
garak._config.transient.basedir / "resources/inthewild_jailbreak_llms.txt"
672+
garak._config.transient.basedir
673+
/ "resources"
674+
/ "inthewild_jailbreak_llms.txt"
672675
)
673676

674677
with open(inthewild_path, "r", encoding="utf-8") as f:

garak/probes/encoding.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
optional variable: "encoding.payloads" -> a list of payload types to be used
88
"""
99

10-
1110
import base64
1211
import binascii
1312
import json
@@ -58,7 +57,7 @@ def load_payloads():
5857

5958
payload_library["slur_terms"] = []
6059
with open(
61-
_config.transient.basedir / "resources/slurprompts_80.jsonl",
60+
_config.transient.basedir / "resources" / "slurprompts_80.jsonl",
6261
"r",
6362
encoding="utf-8",
6463
) as slurfile:

0 commit comments

Comments
 (0)