@@ -21,40 +21,56 @@ def test_function_eligible_for_optimization() -> None:
21
21
return a**2
22
22
"""
23
23
functions_found = {}
24
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
25
- f .write (function )
26
- f .flush ()
27
- functions_found = find_all_functions_in_file (Path (f .name ))
28
- assert functions_found [Path (f .name )][0 ].function_name == "test_function_eligible_for_optimization"
24
+ with tempfile .TemporaryDirectory () as temp_dir :
25
+ temp_dir_path = Path (temp_dir )
26
+ file_path = temp_dir_path / "test_function.py"
27
+
28
+ with file_path .open ("w" ) as f :
29
+ f .write (function )
30
+
31
+ functions_found = find_all_functions_in_file (file_path )
32
+ assert functions_found [file_path ][0 ].function_name == "test_function_eligible_for_optimization"
29
33
30
34
# Has no return statement
31
35
function = """def test_function_not_eligible_for_optimization():
32
36
a = 5
33
37
print(a)
34
38
"""
35
39
functions_found = {}
36
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
37
- f .write (function )
38
- f .flush ()
39
- functions_found = find_all_functions_in_file (Path (f .name ))
40
- assert len (functions_found [Path (f .name )]) == 0
40
+ with tempfile .TemporaryDirectory () as temp_dir :
41
+ temp_dir_path = Path (temp_dir )
42
+ file_path = temp_dir_path / "test_function.py"
43
+
44
+ with file_path .open ("w" ) as f :
45
+ f .write (function )
46
+
47
+ functions_found = find_all_functions_in_file (file_path )
48
+ assert len (functions_found [file_path ]) == 0
41
49
42
50
43
51
# we want to trigger an error in the function discovery
44
52
function = """def test_invalid_code():"""
45
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
46
- f .write (function )
47
- f .flush ()
48
- functions_found = find_all_functions_in_file (Path (f .name ))
53
+ with tempfile .TemporaryDirectory () as temp_dir :
54
+ temp_dir_path = Path (temp_dir )
55
+ file_path = temp_dir_path / "test_function.py"
56
+
57
+ with file_path .open ("w" ) as f :
58
+ f .write (function )
59
+
60
+ functions_found = find_all_functions_in_file (file_path )
49
61
assert functions_found == {}
50
62
51
63
52
64
53
65
54
66
def test_find_top_level_function_or_method ():
55
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
56
- f .write (
57
- """def functionA():
67
+ with tempfile .TemporaryDirectory () as temp_dir :
68
+ temp_dir_path = Path (temp_dir )
69
+ file_path = temp_dir_path / "test_function.py"
70
+
71
+ with file_path .open ("w" ) as f :
72
+ f .write (
73
+ """def functionA():
58
74
def functionB():
59
75
return 5
60
76
class E:
@@ -76,42 +92,48 @@ def functionE(cls, num):
76
92
def non_classmethod_function(cls, name):
77
93
return cls.name
78
94
"""
79
- )
80
- f .flush ()
81
- path_obj_name = Path (f .name )
82
- assert inspect_top_level_functions_or_methods (path_obj_name , "functionA" ).is_top_level
83
- assert not inspect_top_level_functions_or_methods (path_obj_name , "functionB" ).is_top_level
84
- assert inspect_top_level_functions_or_methods (path_obj_name , "functionC" , class_name = "A" ).is_top_level
85
- assert not inspect_top_level_functions_or_methods (path_obj_name , "functionD" , class_name = "A" ).is_top_level
86
- assert not inspect_top_level_functions_or_methods (path_obj_name , "functionF" , class_name = "E" ).is_top_level
87
- assert not inspect_top_level_functions_or_methods (path_obj_name , "functionA" ).has_args
95
+ )
96
+
97
+ assert inspect_top_level_functions_or_methods (file_path , "functionA" ).is_top_level
98
+ assert not inspect_top_level_functions_or_methods (file_path , "functionB" ).is_top_level
99
+ assert inspect_top_level_functions_or_methods (file_path , "functionC" , class_name = "A" ).is_top_level
100
+ assert not inspect_top_level_functions_or_methods (file_path , "functionD" , class_name = "A" ).is_top_level
101
+ assert not inspect_top_level_functions_or_methods (file_path , "functionF" , class_name = "E" ).is_top_level
102
+ assert not inspect_top_level_functions_or_methods (file_path , "functionA" ).has_args
88
103
staticmethod_func = inspect_top_level_functions_or_methods (
89
- path_obj_name , "handle_record_counts" , class_name = None , line_no = 15
104
+ file_path , "handle_record_counts" , class_name = None , line_no = 15
90
105
)
91
106
assert staticmethod_func .is_staticmethod
92
107
assert staticmethod_func .staticmethod_class_name == "AirbyteEntrypoint"
93
108
assert inspect_top_level_functions_or_methods (
94
- path_obj_name , "functionE" , class_name = "AirbyteEntrypoint"
109
+ file_path , "functionE" , class_name = "AirbyteEntrypoint"
95
110
).is_classmethod
96
111
assert not inspect_top_level_functions_or_methods (
97
- path_obj_name , "non_classmethod_function" , class_name = "AirbyteEntrypoint"
112
+ file_path , "non_classmethod_function" , class_name = "AirbyteEntrypoint"
98
113
).is_top_level
99
114
# needed because this will be traced with a class_name being passed
100
115
101
116
# we want to write invalid code to ensure that the function discovery does not crash
102
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
103
- f .write (
104
- """def functionA():
117
+ with tempfile .TemporaryDirectory () as temp_dir :
118
+ temp_dir_path = Path (temp_dir )
119
+ file_path = temp_dir_path / "test_function.py"
120
+
121
+ with file_path .open ("w" ) as f :
122
+ f .write (
123
+ """def functionA():
105
124
"""
106
- )
107
- f .flush ()
108
- path_obj_name = Path (f .name )
109
- assert not inspect_top_level_functions_or_methods (path_obj_name , "functionA" )
125
+ )
126
+
127
+ assert not inspect_top_level_functions_or_methods (file_path , "functionA" )
110
128
111
129
def test_class_method_discovery ():
112
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
113
- f .write (
114
- """class A:
130
+ with tempfile .TemporaryDirectory () as temp_dir :
131
+ temp_dir_path = Path (temp_dir )
132
+ file_path = temp_dir_path / "test_function.py"
133
+
134
+ with file_path .open ("w" ) as f :
135
+ f .write (
136
+ """class A:
115
137
def functionA():
116
138
return True
117
139
def functionB():
@@ -123,21 +145,20 @@ def functionB():
123
145
return False
124
146
def functionA():
125
147
return True"""
126
- )
127
- f . flush ()
148
+ )
149
+
128
150
test_config = TestConfig (
129
151
tests_root = "tests" , project_root_path = "." , test_framework = "pytest" , tests_project_rootdir = Path ()
130
152
)
131
- path_obj_name = Path (f .name )
132
153
functions , functions_count , _ = get_functions_to_optimize (
133
154
optimize_all = None ,
134
155
replay_test = None ,
135
- file = path_obj_name ,
156
+ file = file_path ,
136
157
only_get_this_function = "A.functionA" ,
137
158
test_cfg = test_config ,
138
159
ignore_paths = [Path ("/bruh/" )],
139
- project_root = path_obj_name .parent ,
140
- module_root = path_obj_name .parent ,
160
+ project_root = file_path .parent ,
161
+ module_root = file_path .parent ,
141
162
)
142
163
assert len (functions ) == 1
143
164
for file in functions :
@@ -148,12 +169,12 @@ def functionA():
148
169
functions , functions_count , _ = get_functions_to_optimize (
149
170
optimize_all = None ,
150
171
replay_test = None ,
151
- file = path_obj_name ,
172
+ file = file_path ,
152
173
only_get_this_function = "X.functionA" ,
153
174
test_cfg = test_config ,
154
175
ignore_paths = [Path ("/bruh/" )],
155
- project_root = path_obj_name .parent ,
156
- module_root = path_obj_name .parent ,
176
+ project_root = file_path .parent ,
177
+ module_root = file_path .parent ,
157
178
)
158
179
assert len (functions ) == 1
159
180
for file in functions :
@@ -164,12 +185,12 @@ def functionA():
164
185
functions , functions_count , _ = get_functions_to_optimize (
165
186
optimize_all = None ,
166
187
replay_test = None ,
167
- file = path_obj_name ,
188
+ file = file_path ,
168
189
only_get_this_function = "functionA" ,
169
190
test_cfg = test_config ,
170
191
ignore_paths = [Path ("/bruh/" )],
171
- project_root = path_obj_name .parent ,
172
- module_root = path_obj_name .parent ,
192
+ project_root = file_path .parent ,
193
+ module_root = file_path .parent ,
173
194
)
174
195
assert len (functions ) == 1
175
196
for file in functions :
@@ -178,8 +199,12 @@ def functionA():
178
199
179
200
180
201
def test_nested_function ():
181
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
182
- f .write (
202
+ with tempfile .TemporaryDirectory () as temp_dir :
203
+ temp_dir_path = Path (temp_dir )
204
+ file_path = temp_dir_path / "test_function.py"
205
+
206
+ with file_path .open ("w" ) as f :
207
+ f .write (
183
208
"""
184
209
import copy
185
210
@@ -223,57 +248,63 @@ def traverse(node_id):
223
248
traverse(source_node_id)
224
249
return modified_nodes
225
250
"""
226
- )
227
- f . flush ()
251
+ )
252
+
228
253
test_config = TestConfig (
229
254
tests_root = "tests" , project_root_path = "." , test_framework = "pytest" , tests_project_rootdir = Path ()
230
255
)
231
- path_obj_name = Path (f .name )
232
256
functions , functions_count , _ = get_functions_to_optimize (
233
257
optimize_all = None ,
234
258
replay_test = None ,
235
- file = path_obj_name ,
259
+ file = file_path ,
236
260
test_cfg = test_config ,
237
261
only_get_this_function = None ,
238
262
ignore_paths = [Path ("/bruh/" )],
239
- project_root = path_obj_name .parent ,
240
- module_root = path_obj_name .parent ,
263
+ project_root = file_path .parent ,
264
+ module_root = file_path .parent ,
241
265
)
242
266
243
267
assert len (functions ) == 1
244
268
assert functions_count == 1
245
269
246
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
247
- f .write (
270
+ with tempfile .TemporaryDirectory () as temp_dir :
271
+ temp_dir_path = Path (temp_dir )
272
+ file_path = temp_dir_path / "test_function.py"
273
+
274
+ with file_path .open ("w" ) as f :
275
+ f .write (
248
276
"""
249
277
def outer_function():
250
278
def inner_function():
251
279
pass
252
280
253
281
return inner_function
254
282
"""
255
- )
256
- f . flush ()
283
+ )
284
+
257
285
test_config = TestConfig (
258
286
tests_root = "tests" , project_root_path = "." , test_framework = "pytest" , tests_project_rootdir = Path ()
259
287
)
260
- path_obj_name = Path (f .name )
261
288
functions , functions_count , _ = get_functions_to_optimize (
262
289
optimize_all = None ,
263
290
replay_test = None ,
264
- file = path_obj_name ,
291
+ file = file_path ,
265
292
test_cfg = test_config ,
266
293
only_get_this_function = None ,
267
294
ignore_paths = [Path ("/bruh/" )],
268
- project_root = path_obj_name .parent ,
269
- module_root = path_obj_name .parent ,
295
+ project_root = file_path .parent ,
296
+ module_root = file_path .parent ,
270
297
)
271
298
272
299
assert len (functions ) == 1
273
300
assert functions_count == 1
274
301
275
- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as f :
276
- f .write (
302
+ with tempfile .TemporaryDirectory () as temp_dir :
303
+ temp_dir_path = Path (temp_dir )
304
+ file_path = temp_dir_path / "test_function.py"
305
+
306
+ with file_path .open ("w" ) as f :
307
+ f .write (
277
308
"""
278
309
def outer_function():
279
310
def inner_function():
@@ -283,21 +314,20 @@ def another_inner_function():
283
314
pass
284
315
return inner_function, another_inner_function
285
316
"""
286
- )
287
- f . flush ()
317
+ )
318
+
288
319
test_config = TestConfig (
289
320
tests_root = "tests" , project_root_path = "." , test_framework = "pytest" , tests_project_rootdir = Path ()
290
321
)
291
- path_obj_name = Path (f .name )
292
322
functions , functions_count , _ = get_functions_to_optimize (
293
323
optimize_all = None ,
294
324
replay_test = None ,
295
- file = path_obj_name ,
325
+ file = file_path ,
296
326
test_cfg = test_config ,
297
327
only_get_this_function = None ,
298
328
ignore_paths = [Path ("/bruh/" )],
299
- project_root = path_obj_name .parent ,
300
- module_root = path_obj_name .parent ,
329
+ project_root = file_path .parent ,
330
+ module_root = file_path .parent ,
301
331
)
302
332
303
333
assert len (functions ) == 1
0 commit comments