1
1
# Inspired by
2
2
# https://docs.pytest.org/en/stable/example/nonpython.html#yaml-plugin
3
3
4
+ import re
4
5
import json
5
6
from pathlib import Path
6
7
from typing import NamedTuple , Callable
@@ -46,6 +47,13 @@ def build_docker_language_runner(language) -> subprocess.CompletedProcess:
46
47
# environ.get('TMPDIR') or environ.get('TEMP') or environ.get('TMP') or
47
48
tempdir = Path ('.' ).joinpath ('_language_runner' )
48
49
tempdir .mkdir (exist_ok = True )
50
+ def clear (path : Path ):
51
+ # https://docs.python.org/3/library/pathlib.html#pathlib.Path.walk
52
+ for root , dirs , files in path .walk (top_down = False ):
53
+ for name in files :
54
+ (root / name ).unlink ()
55
+ for name in dirs :
56
+ (root / name ).rmdir ()
49
57
50
58
51
59
class ProjectItemSpec (NamedTuple ):
@@ -90,18 +98,26 @@ def compile_test_python(spec: ProjectItemSpec):
90
98
spec .exec_language (("python3" , "-m" , "py_compile" , path_code_file .name ))
91
99
92
100
93
- def compile_test_java (spec : ProjectItemSpec ):
94
- # java? rename file?
95
- pass
101
+ def get_java_main_classname (code : str ) -> str :
102
+ if match := re .search (r'class (\w+?) .*public static void main' , code , re .DOTALL ):
103
+ return match .group (1 )
104
+ raise Exception ('unable to find top level classname for filename' )
96
105
106
+ def compile_test_java (spec : ProjectItemSpec ):
107
+ path_code_file = tempdir .joinpath (get_java_main_classname (spec .code ) + ".java" )
108
+ path_code_file .write_text (spec .code )
109
+ spec .exec_language (("javac" , path_code_file .name ))
97
110
98
111
def compile_test_csharp (spec : ProjectItemSpec ):
99
112
# csharp create manifest?
100
113
pass
101
114
102
115
103
116
LANGUAGES : MappingProxyType [str , Callable ] = MappingProxyType (
104
- {"py" : compile_test_python }
117
+ {
118
+ "py" : compile_test_python ,
119
+ "java" : compile_test_java ,
120
+ }
105
121
)
106
122
107
123
@@ -127,9 +143,8 @@ def pytest_collect_file(parent: pytest.Dir, file_path: Path):
127
143
# build_docker_language_runner(language)
128
144
129
145
130
- # TODO: correct hook for teardown
131
- #def pytest_teardown():
132
- # tempdir.cleanup()
146
+ def pytest_sessionfinish (session : pytest .Session , exitstatus : int ):
147
+ clear (tempdir )
133
148
134
149
135
150
class ProjectFile (pytest .File ):
@@ -157,6 +172,7 @@ def runtest(self):
157
172
if self .spec .language not in LANGUAGES .keys ():
158
173
raise pytest .skip .Exception (f"Unsupported language { self .spec .language } " )
159
174
175
+ clear (tempdir )
160
176
LANGUAGES [self .spec .language ](self .spec )
161
177
162
178
# https://stackoverflow.com/questions/66037780/how-do-i-require-fixtures-in-a-pytest-plugin
0 commit comments