|
11 | 11 | import sys |
12 | 12 | import os |
13 | 13 | import subprocess |
| 14 | +import helpers |
14 | 15 |
|
15 | 16 | print('Script to generate stub.cs files for C# qltest projects') |
16 | 17 |
|
|
45 | 46 |
|
46 | 47 | csharpQueries = os.path.abspath(os.path.dirname(sys.argv[0])) |
47 | 48 | outputFile = os.path.join(testDir, 'stubs.cs') |
| 49 | +bqrsFile = os.path.join(testDir, 'stubs.bqrs') |
48 | 50 |
|
49 | 51 | print("Stubbing qltest in", testDir) |
50 | 52 |
|
51 | 53 | if os.path.isfile(outputFile): |
52 | 54 | os.remove(outputFile) # It would interfere with the test. |
53 | 55 | print("Removed previous", outputFile) |
54 | 56 |
|
55 | | -cmd = ['codeql', 'test', 'run', '--keep-databases', testDir] |
56 | | -print('Running ' + ' '.join(cmd)) |
57 | | -if subprocess.check_call(cmd): |
58 | | - print("codeql test failed. Please fix up the test before proceeding.") |
59 | | - exit(1) |
| 57 | +helpers.run_cmd(['codeql', 'test', 'run', '--keep-databases', testDir], |
| 58 | + "codeql test failed. Please fix up the test before proceeding.") |
60 | 59 |
|
61 | 60 | dbDir = os.path.join(testDir, os.path.basename(testDir) + ".testproj") |
62 | 61 |
|
63 | 62 | if not os.path.isdir(dbDir): |
64 | 63 | print("Expected database directory " + dbDir + " not found.") |
65 | 64 | exit(1) |
66 | 65 |
|
67 | | -cmd = ['codeql', 'query', 'run', os.path.join( |
68 | | - csharpQueries, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', outputFile] |
69 | | -print('Running ' + ' '.join(cmd)) |
70 | | -if subprocess.check_call(cmd): |
71 | | - print('Failed to run the query to generate output file.') |
72 | | - exit(1) |
| 66 | +helpers.run_cmd(['codeql', 'query', 'run', os.path.join( |
| 67 | + csharpQueries, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', bqrsFile], 'Failed to run the query to generate output file.') |
| 68 | + |
| 69 | +helpers.run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output', |
| 70 | + outputFile, '--format=text', '--no-titles'], 'Failed to run the query to generate output file.') |
| 71 | + |
| 72 | +helpers.trim_output_file(outputFile) |
73 | 73 |
|
74 | | -# Remove the leading and trailing bytes from the file |
75 | | -length = os.stat(outputFile).st_size |
76 | | -if length < 20: |
77 | | - contents = b'' |
78 | | -else: |
79 | | - f = open(outputFile, "rb") |
80 | | - try: |
81 | | - countTillSlash = 0 |
82 | | - foundSlash = False |
83 | | - slash = f.read(1) |
84 | | - while slash != b'': |
85 | | - if slash == b'/': |
86 | | - foundSlash = True |
87 | | - break |
88 | | - countTillSlash += 1 |
89 | | - slash = f.read(1) |
90 | | - |
91 | | - if not foundSlash: |
92 | | - countTillSlash = 0 |
93 | | - |
94 | | - f.seek(0) |
95 | | - quote = f.read(countTillSlash) |
96 | | - print("Start characters in file skipped.", quote) |
97 | | - post = b'\x0e\x01\x08#select\x01\x01\x00s\x00' |
98 | | - contents = f.read(length - len(post) - countTillSlash) |
99 | | - quote = f.read(len(post)) |
100 | | - if quote != post: |
101 | | - print("Unexpected end character in file.", quote) |
102 | | - finally: |
103 | | - f.close() |
104 | | - |
105 | | -f = open(outputFile, "wb") |
106 | | -f.write(contents) |
107 | | -f.close() |
| 74 | +if os.path.isfile(bqrsFile): |
| 75 | + os.remove(bqrsFile) # Cleanup |
| 76 | + print("Removed temp BQRS file", bqrsFile) |
108 | 77 |
|
109 | 78 | cmd = ['codeql', 'test', 'run', testDir] |
110 | 79 | print('Running ' + ' '.join(cmd)) |
|
0 commit comments