Skip to content

Commit ccd90f2

Browse files
committed
Port posix/kotlin tests to pytest.
1 parent c2aff1e commit ccd90f2

File tree

7 files changed

+260
-185
lines changed

7 files changed

+260
-185
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from create_database_utils import *
1+
import runs_on
22

3-
os.mkdir('build')
4-
run_codeql_database_create(["kotlinc test.kt -d build", "javac User.java -cp build"], lang="java")
3+
4+
@runs_on.posix
5+
def test(codeql, java_full):
6+
codeql.database.create(command=["kotlinc test.kt -d build", "javac User.java -cp build"])
Lines changed: 176 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,178 @@
1-
from create_database_utils import *
21
import os
32
import os.path
4-
import subprocess
5-
6-
# Build a family of dependencies outside tracing, then refer to them from a traced build:
7-
8-
older_datetime = "202201010101"
9-
newer_datetime = "202202020202"
10-
11-
classpath_entries = dict()
12-
13-
extraction_orders = ["JavaSeesFirst", "KotlinSeesFirst"]
14-
jar_states = ["NoJar", "JarMtimesEqual", "JavaJarNewer", "KotlinJarNewer"]
15-
class_file_states = ["ClassFileMtimesEqual", "JavaClassFileNewer", "KotlinClassFileNewer"]
16-
17-
# Create test classes for each combination of which extractor will see the file first, the relative timestamps of the jar files seen by each, and the relative timestamps of the class file inside:
18-
19-
jobs = []
20-
21-
for first_extraction in extraction_orders:
22-
for jar_state in jar_states:
23-
for class_file_state in class_file_states:
24-
dep_dir = os.path.join(first_extraction, jar_state, class_file_state)
25-
dep_classname = "Dep___%s___%s___%s" % (first_extraction, jar_state, class_file_state)
26-
dep_seen_by_java_dir = os.path.join(dep_dir, "seen_by_java")
27-
dep_seen_by_kotlin_dir = os.path.join(dep_dir, "seen_by_kotlin")
28-
os.makedirs(dep_seen_by_java_dir)
29-
os.makedirs(dep_seen_by_kotlin_dir)
30-
dep_seen_by_java_sourcefile = os.path.join(dep_seen_by_java_dir, dep_classname + ".java")
31-
dep_seen_by_kotlin_sourcefile = os.path.join(dep_seen_by_kotlin_dir, dep_classname + ".java")
32-
with open(dep_seen_by_java_sourcefile, "w") as f:
33-
f.write("public class %s { }" % dep_classname)
34-
with open(dep_seen_by_kotlin_sourcefile, "w") as f:
35-
f.write("public class %s { void memberOnlySeenByKotlin() { } }" % dep_classname)
36-
jobs.append({
37-
"first_extraction": first_extraction,
38-
"jar_state": jar_state,
39-
"class_file_state": class_file_state,
40-
"dep_dir": dep_dir,
41-
"dep_classname": dep_classname,
42-
"dep_seen_by_java_dir": dep_seen_by_java_dir,
43-
"dep_seen_by_kotlin_dir": dep_seen_by_kotlin_dir,
44-
"dep_seen_by_java_sourcefile": dep_seen_by_java_sourcefile,
45-
"dep_seen_by_kotlin_sourcefile": dep_seen_by_kotlin_sourcefile
46-
})
47-
48-
# Compile all the test classes we just generated, in two commands (since javac objects to seeing the same class file twice in one run)
49-
50-
subprocess.check_call(["javac"] + [j["dep_seen_by_java_sourcefile"] for j in jobs])
51-
subprocess.check_call(["javac"] + [j["dep_seen_by_kotlin_sourcefile"] for j in jobs])
52-
53-
# Create jar files and set class and jar files' relative timestamps for each dependency the two extractors will see:
54-
55-
for j in jobs:
56-
os.remove(j["dep_seen_by_java_sourcefile"])
57-
os.remove(j["dep_seen_by_kotlin_sourcefile"])
58-
dep_seen_by_java_classfile = j["dep_seen_by_java_sourcefile"].replace(".java", ".class")
59-
dep_seen_by_kotlin_classfile = j["dep_seen_by_kotlin_sourcefile"].replace(".java", ".class")
60-
61-
subprocess.check_call(["touch", "-t", newer_datetime if j["class_file_state"] == "JavaClassFileNewer" else older_datetime, dep_seen_by_java_classfile])
62-
subprocess.check_call(["touch", "-t", newer_datetime if j["class_file_state"] == "KotlinClassFileNewer" else older_datetime, dep_seen_by_kotlin_classfile])
63-
64-
if j["jar_state"] != "NoJar":
65-
classfile_name = os.path.basename(dep_seen_by_java_classfile)
66-
jar_command = ["jar", "cf", "dep.jar", classfile_name]
67-
subprocess.check_call(jar_command, cwd = j["dep_seen_by_java_dir"])
68-
subprocess.check_call(jar_command, cwd = j["dep_seen_by_kotlin_dir"])
69-
jar_seen_by_java = os.path.join(j["dep_seen_by_java_dir"], "dep.jar")
70-
jar_seen_by_kotlin = os.path.join(j["dep_seen_by_kotlin_dir"], "dep.jar")
71-
subprocess.check_call(["touch", "-t", newer_datetime if j["jar_state"] == "JavaJarNewer" else older_datetime, jar_seen_by_java])
72-
subprocess.check_call(["touch", "-t", newer_datetime if j["jar_state"] == "KotlinJarNewer" else older_datetime, jar_seen_by_kotlin])
73-
j["javac_classpath_entry"] = jar_seen_by_java
74-
j["kotlinc_classpath_entry"] = jar_seen_by_kotlin
75-
else:
76-
# No jar file involved, just add the dependency build directory to the classpath:
77-
j["javac_classpath_entry"] = j["dep_seen_by_java_dir"]
78-
j["kotlinc_classpath_entry"] = j["dep_seen_by_kotlin_dir"]
79-
80-
# Create source files that instantiate each dependency type:
81-
82-
kotlin_first_jobs = [j for j in jobs if j["first_extraction"] == "KotlinSeesFirst"]
83-
java_first_jobs = [j for j in jobs if j["first_extraction"] == "JavaSeesFirst"]
84-
kotlin_first_classes = [j["dep_classname"] for j in kotlin_first_jobs]
85-
java_first_classes = [j["dep_classname"] for j in java_first_jobs]
86-
87-
kotlin_first_user = "kotlinFirstUser.kt"
88-
kotlin_second_user = "kotlinSecondUser.kt"
89-
java_first_user = "JavaFirstUser.java"
90-
java_second_user = "JavaSecondUser.java"
91-
92-
def kotlin_instantiate_classes(classes):
93-
return "; ".join(["noop(%s())" % c for c in classes])
94-
95-
def make_kotlin_user(user_filename, classes):
96-
with open(user_filename, "w") as f:
97-
f.write("fun noop(x: Any) { } fun user() { %s }" % kotlin_instantiate_classes(classes))
98-
99-
make_kotlin_user(kotlin_first_user, kotlin_first_classes)
100-
make_kotlin_user(kotlin_second_user, java_first_classes)
101-
102-
def java_instantiate_classes(classes):
103-
return " ".join(["noop(new %s());" % c for c in classes])
104-
105-
def make_java_user(user_filename, classes):
106-
with open(user_filename, "w") as f:
107-
f.write("public class %s { private static void noop(Object x) { } public static void user() { %s } }" % (user_filename.replace(".java", ""), java_instantiate_classes(classes)))
108-
109-
make_java_user(java_first_user, java_first_classes)
110-
make_java_user(java_second_user, kotlin_first_classes)
111-
112-
# Now finally make a database, including classes where Java sees them first followed by Kotlin and vice versa.
113-
# In all cases the Kotlin extraction should take precedence.
114-
115-
def make_classpath(jobs, entry_name):
116-
return ":".join([j[entry_name] for j in jobs])
117-
118-
kotlin_first_classpath = make_classpath(kotlin_first_jobs, "kotlinc_classpath_entry")
119-
java_first_classpath = make_classpath(java_first_jobs, "javac_classpath_entry")
120-
kotlin_second_classpath = make_classpath(java_first_jobs, "kotlinc_classpath_entry")
121-
java_second_classpath = make_classpath(kotlin_first_jobs, "javac_classpath_entry")
122-
123-
run_codeql_database_create([
124-
"kotlinc -cp %s %s" % (kotlin_first_classpath, kotlin_first_user),
125-
"javac -cp %s %s" % (java_first_classpath, java_first_user),
126-
"kotlinc -cp %s %s" % (kotlin_second_classpath, kotlin_second_user),
127-
"javac -cp %s %s" % (java_second_classpath, java_second_user)
128-
], lang="java")
3+
import commands
4+
import runs_on
5+
6+
7+
@runs_on.posix
8+
def test(codeql, java_full):
9+
# Build a family of dependencies outside tracing, then refer to them from a traced build:
10+
11+
older_datetime = "202201010101"
12+
newer_datetime = "202202020202"
13+
14+
classpath_entries = dict()
15+
16+
extraction_orders = ["JavaSeesFirst", "KotlinSeesFirst"]
17+
jar_states = ["NoJar", "JarMtimesEqual", "JavaJarNewer", "KotlinJarNewer"]
18+
class_file_states = ["ClassFileMtimesEqual", "JavaClassFileNewer", "KotlinClassFileNewer"]
19+
20+
# Create test classes for each combination of which extractor will see the file first, the relative timestamps of the jar files seen by each, and the relative timestamps of the class file inside:
21+
22+
jobs = []
23+
24+
for first_extraction in extraction_orders:
25+
for jar_state in jar_states:
26+
for class_file_state in class_file_states:
27+
dep_dir = os.path.join(first_extraction, jar_state, class_file_state)
28+
dep_classname = "Dep___%s___%s___%s" % (
29+
first_extraction,
30+
jar_state,
31+
class_file_state,
32+
)
33+
dep_seen_by_java_dir = os.path.join(dep_dir, "seen_by_java")
34+
dep_seen_by_kotlin_dir = os.path.join(dep_dir, "seen_by_kotlin")
35+
os.makedirs(dep_seen_by_java_dir)
36+
os.makedirs(dep_seen_by_kotlin_dir)
37+
dep_seen_by_java_sourcefile = os.path.join(
38+
dep_seen_by_java_dir, dep_classname + ".java"
39+
)
40+
dep_seen_by_kotlin_sourcefile = os.path.join(
41+
dep_seen_by_kotlin_dir, dep_classname + ".java"
42+
)
43+
with open(dep_seen_by_java_sourcefile, "w") as f:
44+
f.write("public class %s { }" % dep_classname)
45+
with open(dep_seen_by_kotlin_sourcefile, "w") as f:
46+
f.write("public class %s { void memberOnlySeenByKotlin() { } }" % dep_classname)
47+
jobs.append(
48+
{
49+
"first_extraction": first_extraction,
50+
"jar_state": jar_state,
51+
"class_file_state": class_file_state,
52+
"dep_dir": dep_dir,
53+
"dep_classname": dep_classname,
54+
"dep_seen_by_java_dir": dep_seen_by_java_dir,
55+
"dep_seen_by_kotlin_dir": dep_seen_by_kotlin_dir,
56+
"dep_seen_by_java_sourcefile": dep_seen_by_java_sourcefile,
57+
"dep_seen_by_kotlin_sourcefile": dep_seen_by_kotlin_sourcefile,
58+
}
59+
)
60+
61+
# Compile all the test classes we just generated, in two commands (since javac objects to seeing the same class file twice in one run)
62+
63+
commands.run(["javac"] + [j["dep_seen_by_java_sourcefile"] for j in jobs])
64+
commands.run(["javac"] + [j["dep_seen_by_kotlin_sourcefile"] for j in jobs])
65+
66+
# Create jar files and set class and jar files' relative timestamps for each dependency the two extractors will see:
67+
68+
for j in jobs:
69+
os.remove(j["dep_seen_by_java_sourcefile"])
70+
os.remove(j["dep_seen_by_kotlin_sourcefile"])
71+
dep_seen_by_java_classfile = j["dep_seen_by_java_sourcefile"].replace(".java", ".class")
72+
dep_seen_by_kotlin_classfile = j["dep_seen_by_kotlin_sourcefile"].replace(".java", ".class")
73+
74+
commands.run(
75+
[
76+
"touch",
77+
"-t",
78+
newer_datetime if j["class_file_state"] == "JavaClassFileNewer" else older_datetime,
79+
dep_seen_by_java_classfile,
80+
]
81+
)
82+
commands.run(
83+
[
84+
"touch",
85+
"-t",
86+
(
87+
newer_datetime
88+
if j["class_file_state"] == "KotlinClassFileNewer"
89+
else older_datetime
90+
),
91+
dep_seen_by_kotlin_classfile,
92+
]
93+
)
94+
95+
if j["jar_state"] != "NoJar":
96+
classfile_name = os.path.basename(dep_seen_by_java_classfile)
97+
jar_command = ["jar", "cf", "dep.jar", classfile_name]
98+
commands.run(jar_command, _cwd=j["dep_seen_by_java_dir"])
99+
commands.run(jar_command, _cwd=j["dep_seen_by_kotlin_dir"])
100+
jar_seen_by_java = os.path.join(j["dep_seen_by_java_dir"], "dep.jar")
101+
jar_seen_by_kotlin = os.path.join(j["dep_seen_by_kotlin_dir"], "dep.jar")
102+
commands.run(
103+
[
104+
"touch",
105+
"-t",
106+
newer_datetime if j["jar_state"] == "JavaJarNewer" else older_datetime,
107+
jar_seen_by_java,
108+
]
109+
)
110+
commands.run(
111+
[
112+
"touch",
113+
"-t",
114+
newer_datetime if j["jar_state"] == "KotlinJarNewer" else older_datetime,
115+
jar_seen_by_kotlin,
116+
]
117+
)
118+
j["javac_classpath_entry"] = jar_seen_by_java
119+
j["kotlinc_classpath_entry"] = jar_seen_by_kotlin
120+
else:
121+
# No jar file involved, just add the dependency build directory to the classpath:
122+
j["javac_classpath_entry"] = j["dep_seen_by_java_dir"]
123+
j["kotlinc_classpath_entry"] = j["dep_seen_by_kotlin_dir"]
124+
125+
# Create source files that instantiate each dependency type:
126+
127+
kotlin_first_jobs = [j for j in jobs if j["first_extraction"] == "KotlinSeesFirst"]
128+
java_first_jobs = [j for j in jobs if j["first_extraction"] == "JavaSeesFirst"]
129+
kotlin_first_classes = [j["dep_classname"] for j in kotlin_first_jobs]
130+
java_first_classes = [j["dep_classname"] for j in java_first_jobs]
131+
132+
kotlin_first_user = "kotlinFirstUser.kt"
133+
kotlin_second_user = "kotlinSecondUser.kt"
134+
java_first_user = "JavaFirstUser.java"
135+
java_second_user = "JavaSecondUser.java"
136+
137+
def kotlin_instantiate_classes(classes):
138+
return "; ".join(["noop(%s())" % c for c in classes])
139+
140+
def make_kotlin_user(user_filename, classes):
141+
with open(user_filename, "w") as f:
142+
f.write("fun noop(x: Any) { } fun user() { %s }" % kotlin_instantiate_classes(classes))
143+
144+
make_kotlin_user(kotlin_first_user, kotlin_first_classes)
145+
make_kotlin_user(kotlin_second_user, java_first_classes)
146+
147+
def java_instantiate_classes(classes):
148+
return " ".join(["noop(new %s());" % c for c in classes])
149+
150+
def make_java_user(user_filename, classes):
151+
with open(user_filename, "w") as f:
152+
f.write(
153+
"public class %s { private static void noop(Object x) { } public static void user() { %s } }"
154+
% (user_filename.replace(".java", ""), java_instantiate_classes(classes))
155+
)
156+
157+
make_java_user(java_first_user, java_first_classes)
158+
make_java_user(java_second_user, kotlin_first_classes)
159+
160+
# Now finally make a database, including classes where Java sees them first followed by Kotlin and vice versa.
161+
# In all cases the Kotlin extraction should take precedence.
162+
163+
def make_classpath(jobs, entry_name):
164+
return ":".join([j[entry_name] for j in jobs])
165+
166+
kotlin_first_classpath = make_classpath(kotlin_first_jobs, "kotlinc_classpath_entry")
167+
java_first_classpath = make_classpath(java_first_jobs, "javac_classpath_entry")
168+
kotlin_second_classpath = make_classpath(java_first_jobs, "kotlinc_classpath_entry")
169+
java_second_classpath = make_classpath(kotlin_first_jobs, "javac_classpath_entry")
170+
171+
codeql.database.create(
172+
command=[
173+
"kotlinc -cp %s %s" % (kotlin_first_classpath, kotlin_first_user),
174+
"javac -cp %s %s" % (java_first_classpath, java_first_user),
175+
"kotlinc -cp %s %s" % (kotlin_second_classpath, kotlin_second_user),
176+
"javac -cp %s %s" % (java_second_classpath, java_second_user),
177+
]
178+
)

java/ql/integration-tests/posix-only/kotlin/kotlin_double_interception/code/build.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)