11import json
2- import tempfile
3- from contextlib import contextmanager
42from pathlib import Path
3+ from typing import Callable
54
65import pytest
76
2120}
2221
2322
24- @contextmanager
25- def init_job_directory (input_obj : dict | None = None ):
26- """Context manager that yields a temporary job directory path, then cleans up."""
27- temp_dir = tempfile .TemporaryDirectory ()
28- job_dir = Path (temp_dir .name )
23+ @pytest .fixture (scope = "function" )
24+ def job_directory (tmp_path : Path ) -> Path :
25+ """Fixture to initialize the actual test job's directory."""
2926
30- try :
31- input_dir = job_dir / "input"
32- input_dir .mkdir ()
27+ job_dir = tmp_path
3328
34- if input_obj is None :
35- input_obj = DEFAULT_INPUT
29+ input_dir = job_dir / "input"
30+ input_dir . mkdir ()
3631
37- with open (input_dir / "input.json" , "w" ) as f :
32+ with open (input_dir / "input.json" , "w" ) as f :
33+ json .dump (DEFAULT_INPUT , f )
34+
35+ output_dir = job_dir / "output"
36+ output_dir .mkdir ()
37+
38+ print (job_dir )
39+ return job_dir
40+
41+
42+ @pytest .fixture
43+ def add_job_input (job_directory : Path ) -> Callable [[dict ], None ]:
44+ """Factory fixture to specify a custom input.json for the actual test job."""
45+
46+ def _add_job_input (input_obj : dict ):
47+
48+ with open (job_directory / "input" / "input.json" , "w" ) as f :
49+ print (f .name )
3850 json .dump (input_obj , f )
3951
40- output_dir = job_dir / "output"
41- output_dir .mkdir ()
52+ return _add_job_input
4253
43- yield job_dir
4454
45- finally :
46- temp_dir .cleanup ()
55+ @pytest .fixture
56+ def load_job_output (job_directory : Path ) -> Callable [[], dict ]:
57+ """Factory fixture to load the actual test job's output.json."""
4758
59+ def _load_job_output () -> dict :
60+ with open (job_directory / "output" / "output.json" ) as output_file :
61+ output = json .load (output_file )
62+ return output
4863
49- def load_output (job_directory : Path ) -> dict :
50- with open (job_directory / "output" / "output.json" ) as output_file :
51- output = json .load (output_file )
52- return output
64+ return _load_job_output
5365
5466
55- def test_simple_analyzer_success ():
56- with init_job_directory () as job_directory :
57- Analyzer (job_directory ).report ({})
58- output = load_output (job_directory )
67+ def test_simple_analyzer_success (job_directory , load_job_output ):
68+ Analyzer (job_directory ).report ({})
69+ output = load_job_output ()
5970 assert output == DEFAULT_OUTPUT
6071
6172
62- def test_simple_analyzer_error ():
63-
73+ def test_simple_analyzer_error (job_directory , load_job_output ):
6474 error_msg = "test analyzer error"
65- with init_job_directory () as job_directory :
66- with pytest .raises (SystemExit ):
67- Analyzer (job_directory ).error (error_msg )
68- output = load_output (job_directory )
75+ with pytest .raises (SystemExit ):
76+ Analyzer (job_directory ).error (error_msg )
6977
78+ output = load_job_output ()
7079 assert output == {
7180 "success" : False ,
7281 "input" : DEFAULT_INPUT ,
7382 "errorMessage" : error_msg ,
7483 }
7584
7685
77- def test_analyzer_report_with_summary_and_taxonomies ():
86+ def test_analyzer_report_with_summary_and_taxonomies (job_directory , load_job_output ):
7887 class TestAnalyzer (Analyzer ):
7988 def summary (self , raw ):
8089 taxonomies = []
@@ -89,10 +98,9 @@ def summary(self, raw):
8998 )
9099 return {"taxonomies" : taxonomies , "raw" : raw }
91100
92- with init_job_directory () as job_directory :
93- full_report = {}
94- TestAnalyzer (job_directory ).report (full_report )
95- output = load_output (job_directory )
101+ full_report = {}
102+ TestAnalyzer (job_directory ).report (full_report )
103+ output = load_job_output ()
96104
97105 assert output == {
98106 ** DEFAULT_OUTPUT ,
@@ -111,22 +119,21 @@ def summary(self, raw):
111119 }
112120
113121
114- def test_analyzer_report_with_operations ():
122+ def test_analyzer_report_with_operations (job_directory , load_job_output ):
115123 class TestAnalyzer (Analyzer ):
116124 def operations (self , raw ):
117125 return [self .build_operation (op_type = "DummyOperation" , dummy = "parameter" )]
118126
119- with init_job_directory () as job_directory :
120- TestAnalyzer (job_directory ).report ({})
121- output = load_output (job_directory )
127+ TestAnalyzer (job_directory ).report ({})
128+ output = load_job_output ()
122129
123130 assert output == {
124131 ** DEFAULT_OUTPUT ,
125132 "operations" : [{"type" : "DummyOperation" , "dummy" : "parameter" }],
126133 }
127134
128135
129- def test_analyzer_report_with_extractable_artifacts ():
136+ def test_analyzer_report_with_extractable_artifacts (job_directory , load_job_output ):
130137 string_ip_artifact = "11.22.33.44"
131138 list_ip_artifacts = ["10.20.30.40" , "20.30.40.50" ]
132139 dict_item_ip_artifact = "100.100.100.100"
@@ -137,9 +144,8 @@ def test_analyzer_report_with_extractable_artifacts():
137144 "dict-with-ip" : {"just-an-ip" : dict_item_ip_artifact },
138145 }
139146
140- with init_job_directory () as job_directory :
141- Analyzer (job_directory ).report (report )
142- output = load_output (job_directory )
147+ Analyzer (job_directory ).report (report )
148+ output = load_job_output ()
143149
144150 assert output == {
145151 ** DEFAULT_OUTPUT ,
@@ -151,13 +157,15 @@ def test_analyzer_report_with_extractable_artifacts():
151157 }
152158
153159
154- def test_analyzer_error_for_invalid_input ():
160+ def test_analyzer_error_for_invalid_input (
161+ job_directory , add_job_input , load_job_output
162+ ):
155163
156164 empty_input = {}
157- with init_job_directory (empty_input ) as job_directory :
158- with pytest .raises (SystemExit ):
159- Analyzer (job_directory )
160- output = load_output ( job_directory )
165+ add_job_input (empty_input )
166+ with pytest .raises (SystemExit ):
167+ Analyzer (job_directory )
168+ output = load_job_output ( )
161169
162170 assert output == {
163171 "success" : False ,
@@ -166,10 +174,10 @@ def test_analyzer_error_for_invalid_input():
166174 }
167175
168176 generic_input_without_data = {"dataType" : "ip" }
169- with init_job_directory (generic_input_without_data ) as job_directory :
170- with pytest .raises (SystemExit ):
171- Analyzer (job_directory ).report ({})
172- output = load_output ( job_directory )
177+ add_job_input (generic_input_without_data )
178+ with pytest .raises (SystemExit ):
179+ Analyzer (job_directory ).report ({})
180+ output = load_job_output ( )
173181
174182 assert output == {
175183 "success" : False ,
@@ -178,10 +186,10 @@ def test_analyzer_error_for_invalid_input():
178186 }
179187
180188 file_input_without_filename = {"dataType" : "file" }
181- with init_job_directory (file_input_without_filename ) as job_directory :
182- with pytest .raises (SystemExit ):
183- Analyzer (job_directory ).report ({})
184- output = load_output ( job_directory )
189+ add_job_input (file_input_without_filename )
190+ with pytest .raises (SystemExit ):
191+ Analyzer (job_directory ).report ({})
192+ output = load_job_output ( )
185193
186194 assert output == {
187195 "success" : False ,
0 commit comments