Skip to content

Commit ed9502f

Browse files
committed
Kotlin: Enhance the TRAP compression test
1 parent adb4739 commit ed9502f

File tree

1 file changed

+43
-32
lines changed
  • java/ql/integration-tests/all-platforms/kotlin/trap_compression

1 file changed

+43
-32
lines changed
Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
11
from create_database_utils import *
22

3-
def check_extension(directory, expected_extension):
3+
def check_extensions(directory, counts):
44
if platform.system() == 'Windows':
55
# It's important that the path is a Unicode path on Windows, so
66
# that the right system calls get used.
77
directory = u'' + directory
88
if not directory.startswith("\\\\?\\"):
99
directory = "\\\\?\\" + os.path.abspath(directory)
1010

11-
if expected_extension == '.trap':
12-
# We start TRAP files with a comment
13-
expected_start = b'//'
14-
elif expected_extension == '.trap.gz':
15-
# The GZip magic numbers
16-
expected_start = b'\x1f\x8b'
17-
else:
18-
raise Exception('Unknown expected extension ' + expected_extension)
19-
count = check_extension_worker(directory, expected_extension, expected_start)
20-
if count != 1:
21-
raise Exception('Expected 1 relevant file, but found ' + str(count) + ' in ' + directory)
11+
check_extensions_worker(counts, directory)
12+
check_counts('non-compressed', counts.expected_none, counts.count_none)
13+
check_counts('gzipped', counts.expected_gzip, counts.count_gzip)
2214

23-
def check_extension_worker(directory, expected_extension, expected_start):
24-
count = 0
15+
def check_counts(name, expected, count):
16+
if expected == -1:
17+
if count < 10:
18+
raise Exception('Expected lots of ' + name + ' files, but got ' + str(count))
19+
elif expected != count:
20+
raise Exception('Expected ' + str(expected) + ' ' + name + ' files, but got ' + str(count))
21+
22+
class Counts:
23+
def __init__(self, expected_none, expected_gzip):
24+
self.expected_none = expected_none
25+
self.expected_gzip = expected_gzip
26+
self.count_none = 0
27+
self.count_gzip = 0
28+
29+
def check_extensions_worker(counts, directory):
2530
for f in os.listdir(directory):
2631
x = os.path.join(directory, f)
2732
if os.path.isdir(x):
28-
count += check_extension_worker(x, expected_extension, expected_start)
29-
else:
30-
if f.startswith('test.kt') and not f.endswith('.set'):
31-
if f.endswith(expected_extension):
32-
with open(x, 'rb') as f_in:
33-
content = f_in.read()
34-
if content.startswith(expected_start):
35-
count += 1
36-
else:
37-
raise Exception('Unexpected start to content of ' + x)
38-
else:
39-
raise Exception('Expected test.kt TRAP file to have extension ' + expected_extension + ', but found ' + x)
40-
return count
33+
check_extensions_worker(counts, x)
34+
elif f.endswith('.trap'):
35+
counts.count_none += 1
36+
if not startsWith(x, b'//'): # We start TRAP files with a comment
37+
raise Exception("TRAP file that doesn't start with a comment: " + f)
38+
elif f.endswith('.trap.gz'):
39+
counts.count_gzip += 1
40+
if not startsWith(x, b'\x1f\x8b'): # The GZip magic numbers
41+
raise Exception("GZipped TRAP file that doesn't start with GZip magic numbers: " + f)
42+
43+
def startsWith(f, b):
44+
with open(f, 'rb') as f_in:
45+
content = f_in.read()
46+
return content.startswith(b)
4147

48+
# In the counts, we expect lots of files of the compression type chosen
49+
# (so expected count is -1), but the diagnostic TRAP files will always
50+
# be uncompressed (so count_none is always 1 or -1) and the
51+
# sourceLocationPrefix TRAP file is always gzipped (so count_gzip is
52+
# always 1 or -1).
4253
run_codeql_database_create(['kotlinc test.kt'], test_db="default-db", db=None, lang="java")
43-
check_extension('default-db/trap', '.trap.gz')
54+
check_extensions('default-db/trap', Counts(1, -1))
4455
os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "nOnE"
4556
run_codeql_database_create(['kotlinc test.kt'], test_db="none-db", db=None, lang="java")
46-
check_extension('none-db/trap', '.trap')
57+
check_extensions('none-db/trap', Counts(-1, 1))
4758
os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "gzip"
4859
run_codeql_database_create(['kotlinc test.kt'], test_db="gzip-db", db=None, lang="java")
49-
check_extension('gzip-db/trap', '.trap.gz')
60+
check_extensions('gzip-db/trap', Counts(1, -1))
5061
os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "brotli"
5162
run_codeql_database_create(['kotlinc test.kt'], test_db="brotli-db", db=None, lang="java")
52-
check_extension('brotli-db/trap', '.trap.gz')
63+
check_extensions('brotli-db/trap', Counts(1, -1))
5364
os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "invalidValue"
5465
run_codeql_database_create(['kotlinc test.kt'], test_db="invalid-db", db=None, lang="java")
55-
check_extension('invalid-db/trap', '.trap.gz')
66+
check_extensions('invalid-db/trap', Counts(1, -1))

0 commit comments

Comments
 (0)