|
1 | 1 | from create_database_utils import *
|
2 | 2 |
|
3 |
| -def check_extension(directory, expected_extension): |
| 3 | +def check_extensions(directory, counts): |
4 | 4 | if platform.system() == 'Windows':
|
5 | 5 | # It's important that the path is a Unicode path on Windows, so
|
6 | 6 | # that the right system calls get used.
|
7 | 7 | directory = u'' + directory
|
8 | 8 | if not directory.startswith("\\\\?\\"):
|
9 | 9 | directory = "\\\\?\\" + os.path.abspath(directory)
|
10 | 10 |
|
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) |
22 | 14 |
|
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): |
25 | 30 | for f in os.listdir(directory):
|
26 | 31 | x = os.path.join(directory, f)
|
27 | 32 | 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) |
41 | 47 |
|
| 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). |
42 | 53 | 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)) |
44 | 55 | os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "nOnE"
|
45 | 56 | 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)) |
47 | 58 | os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "gzip"
|
48 | 59 | 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)) |
50 | 61 | os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "brotli"
|
51 | 62 | 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)) |
53 | 64 | os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "invalidValue"
|
54 | 65 | 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