8
8
import json
9
9
import glob
10
10
import shlex
11
+ import shutil
12
+ import tempfile
11
13
from shutil import copyfile
12
14
13
15
14
16
def print_usage (exit_code = 1 ):
15
- print ("Usage: makeStubs.py testDir stubDir\n " ,
17
+ print ("Usage: makeStubs.py testDir stubDir [pom.xml] \n " ,
16
18
"testDir: the directory containing the qltest to be stubbed.\n "
17
- " Should contain an `options0` file pointing to the jars to stub, and an `options1` file pointing to `stubdir`.\n "
18
- " These files should be in the same format as a normal `options` file.\n " ,
19
- "stubDir: the directory to output the generated stubs to" )
19
+ " Should contain an 'options' file pointing to 'stubdir'.\n "
20
+ " These files should be in the same format as a normal 'options' file.\n " ,
21
+ "stubDir: the directory to output the generated stubs to\n " ,
22
+ "pom.xml: a 'pom.xml' file that can be used to build the project\n " ,
23
+ " If the test can be extracted without a 'pom.xml', this argument can be ommitted." )
20
24
exit (exit_code )
21
25
22
26
23
27
if "--help" in sys .argv or "-h" in sys .argv :
24
28
print_usage (0 )
25
29
26
- if len (sys .argv ) != 3 :
30
+ if len (sys .argv ) not in [ 3 , 4 ] :
27
31
print_usage ()
28
32
29
33
testDir = sys .argv [1 ].rstrip ("/" )
30
34
stubDir = sys .argv [2 ].rstrip ("/" )
31
35
32
-
33
36
def check_dir_exists (path ):
34
37
if not os .path .isdir (path ):
35
38
print (path , "does not exist or is not a directory" )
@@ -46,11 +49,8 @@ def check_file_exists(path):
46
49
check_dir_exists (stubDir )
47
50
48
51
optionsFile = os .path .join (testDir , "options" )
49
- options0File = os .path .join (testDir , "options0" )
50
- options1File = os .path .join (testDir , "options1" )
51
52
52
- check_file_exists (options0File )
53
- check_file_exists (options1File )
53
+ check_file_exists (optionsFile )
54
54
55
55
# Does it contain a .ql file and a .java file?
56
56
@@ -67,12 +67,33 @@ def check_file_exists(path):
67
67
exit (1 )
68
68
69
69
70
+ workDir = tempfile .TemporaryDirectory ().name
71
+
72
+ print ("Created temporary directory '%s'" % workDir )
73
+
70
74
javaQueries = os .path .abspath (os .path .dirname (sys .argv [0 ]))
71
- outputBqrsFile = os .path .join (testDir , 'output.bqrs' )
72
- outputJsonFile = os .path .join (testDir , 'output.json' )
75
+ outputBqrsFile = os .path .join (workDir , 'output.bqrs' )
76
+ outputJsonFile = os .path .join (workDir , 'output.json' )
77
+
78
+ # Make a database that touches all types whose methods we want to test:
79
+ print ("Creating Maven project" )
80
+ projectDir = os .path .join (workDir , "mavenProject" )
81
+ os .makedirs (projectDir )
73
82
74
- dbDir = os .path .join (testDir , os .path .basename (testDir ) + ".testproj" )
83
+ if len (sys .argv ) == 4 :
84
+ projectTestPkgDir = os .path .join (projectDir , "src" , "main" , "java" , "test" )
85
+ shutil .copytree (testDir , projectTestPkgDir , ignore = shutil .ignore_patterns ('*.testproj' ))
75
86
87
+ try :
88
+ shutil .copyfile (sys .argv [3 ], os .path .join (projectDir , "pom.xml" ))
89
+ except Exception as e :
90
+ print ("Failed to read project POM %s: %s" % (sys .argv [2 ], e ), file = sys .stderr )
91
+ sys .exit (1 )
92
+ else :
93
+ # if `pom.xml` is omitted, simply copy the test directory to `projectDir`
94
+ shutil .copytree (testDir , projectDir , ignore = shutil .ignore_patterns ('*.testproj' ))
95
+
96
+ dbDir = os .path .join (workDir , "db" )
76
97
77
98
def print_javac_output ():
78
99
logFiles = glob .glob (os .path .join (dbDir , "log" , "javac-output*" ))
@@ -98,11 +119,9 @@ def run(cmd):
98
119
99
120
print ("Stubbing qltest in" , testDir )
100
121
101
- copyfile (options0File , optionsFile )
102
-
103
- if run (['codeql' , 'test' , 'run' , '--keep-databases' , testDir ]):
122
+ if run (['codeql' , 'database' , 'create' , '--language=java' , '--source-root=' + projectDir , dbDir ]):
104
123
print_javac_output ()
105
- print ("codeql test failed. Please fix up the test before proceeding." )
124
+ print ("codeql database create failed. Please fix up the test before proceeding." )
106
125
exit (1 )
107
126
108
127
if not os .path .isdir (dbDir ):
@@ -143,13 +162,13 @@ def run(cmd):
143
162
144
163
print ("Verifying stub correctness" )
145
164
146
- copyfile (options1File , optionsFile )
147
-
148
165
if run (['codeql' , 'test' , 'run' , testDir ]):
149
166
print_javac_output ()
150
167
print ('\n Test failed. You may need to fix up the generated stubs.' )
151
168
exit (1 )
152
169
170
+ os .rmdir (workDir )
171
+
153
172
print ("\n Stub generation successful!" )
154
173
155
174
exit (0 )
0 commit comments