|
| 1 | +from create_database_utils import * |
| 2 | + |
| 3 | +def check_extension(directory, expected_extension): |
| 4 | + if expected_extension == '.trap': |
| 5 | + # We start TRAP files with a comment |
| 6 | + expected_start = b'//' |
| 7 | + elif expected_extension == '.trap.gz': |
| 8 | + # The GZip magic numbers |
| 9 | + expected_start = b'\x1f\x8b' |
| 10 | + else: |
| 11 | + raise Exception('Unknown expected extension ' + expected_extension) |
| 12 | + count = check_extension_worker(directory, expected_extension, expected_start) |
| 13 | + if count != 1: |
| 14 | + raise Exception('Expected 1 relevant file, but found ' + str(count) + ' in ' + directory) |
| 15 | + |
| 16 | +def check_extension_worker(directory, expected_extension, expected_start): |
| 17 | + count = 0 |
| 18 | + for f in os.listdir(directory): |
| 19 | + x = os.path.join(directory, f) |
| 20 | + if os.path.isdir(x): |
| 21 | + count += check_extension_worker(x, expected_extension, expected_start) |
| 22 | + else: |
| 23 | + if f.startswith('test.kt') and not f.endswith('.set'): |
| 24 | + if f.endswith(expected_extension): |
| 25 | + with open(x, 'rb') as f_in: |
| 26 | + content = f_in.read() |
| 27 | + if content.startswith(expected_start): |
| 28 | + count += 1 |
| 29 | + else: |
| 30 | + raise Exception('Unexpected start to content of ' + x) |
| 31 | + else: |
| 32 | + raise Exception('Expected test.kt TRAP file to have extension ' + expected_extension + ', but found ' + x) |
| 33 | + return count |
| 34 | + |
| 35 | +run_codeql_database_create(['kotlinc test.kt'], test_db="default-db", db=None, lang="java") |
| 36 | +check_extension('default-db/trap', '.trap.gz') |
| 37 | +os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "nOnE" |
| 38 | +run_codeql_database_create(['kotlinc test.kt'], test_db="none-db", db=None, lang="java") |
| 39 | +check_extension('none-db/trap', '.trap') |
| 40 | +os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "gzip" |
| 41 | +run_codeql_database_create(['kotlinc test.kt'], test_db="gzip-db", db=None, lang="java") |
| 42 | +check_extension('gzip-db/trap', '.trap.gz') |
| 43 | +os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "brotli" |
| 44 | +run_codeql_database_create(['kotlinc test.kt'], test_db="brotli-db", db=None, lang="java") |
| 45 | +check_extension('brotli-db/trap', '.trap.gz') |
| 46 | +os.environ["CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"] = "invalidValue" |
| 47 | +run_codeql_database_create(['kotlinc test.kt'], test_db="invalid-db", db=None, lang="java") |
| 48 | +check_extension('invalid-db/trap', '.trap.gz') |
0 commit comments