C#: Add nuget based stubbing script

This commit is contained in:
Tamas Vajk
2021-04-13 12:43:33 +02:00
parent fa215bcda5
commit bfa9bf33c0
3 changed files with 106 additions and 43 deletions

View File

@@ -0,0 +1,37 @@
import sys
import os
import subprocess
def run_cmd(cmd, msg="Failed to run command"):
print('Running ' + ' '.join(cmd))
if subprocess.check_call(cmd):
print(msg)
exit(1)
def get_argv(index, default):
if len(sys.argv) > index:
return sys.argv[index]
return default
def trim_output_file(file):
# Remove the leading and trailing bytes from the file
length = os.stat(file).st_size
if length < 20:
contents = b''
else:
f = open(file, "rb")
try:
pre = f.read(2)
print("Start characters in file skipped.", pre)
contents = f.read(length - 5)
post = f.read(3)
print("End characters in file skipped.", post)
finally:
f.close()
f = open(file, "wb")
f.write(contents)
f.close()

View File

@@ -11,6 +11,7 @@
import sys
import os
import subprocess
import helpers
print('Script to generate stub.cs files for C# qltest projects')
@@ -45,6 +46,7 @@ if not foundCS:
csharpQueries = os.path.abspath(os.path.dirname(sys.argv[0]))
outputFile = os.path.join(testDir, 'stubs.cs')
bqrsFile = os.path.join(testDir, 'stubs.bqrs')
print("Stubbing qltest in", testDir)
@@ -52,11 +54,8 @@ if os.path.isfile(outputFile):
os.remove(outputFile) # It would interfere with the test.
print("Removed previous", outputFile)
cmd = ['codeql', 'test', 'run', '--keep-databases', testDir]
print('Running ' + ' '.join(cmd))
if subprocess.check_call(cmd):
print("codeql test failed. Please fix up the test before proceeding.")
exit(1)
helpers.run_cmd(['codeql', 'test', 'run', '--keep-databases', testDir],
"codeql test failed. Please fix up the test before proceeding.")
dbDir = os.path.join(testDir, os.path.basename(testDir) + ".testproj")
@@ -64,47 +63,17 @@ if not os.path.isdir(dbDir):
print("Expected database directory " + dbDir + " not found.")
exit(1)
cmd = ['codeql', 'query', 'run', os.path.join(
csharpQueries, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', outputFile]
print('Running ' + ' '.join(cmd))
if subprocess.check_call(cmd):
print('Failed to run the query to generate output file.')
exit(1)
helpers.run_cmd(['codeql', 'query', 'run', os.path.join(
csharpQueries, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', bqrsFile], 'Failed to run the query to generate output file.')
# Remove the leading and trailing bytes from the file
length = os.stat(outputFile).st_size
if length < 20:
contents = b''
else:
f = open(outputFile, "rb")
try:
countTillSlash = 0
foundSlash = False
slash = f.read(1)
while slash != b'':
if slash == b'/':
foundSlash = True
break
countTillSlash += 1
slash = f.read(1)
helpers.run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output',
outputFile, '--format=text', '--no-titles'], 'Failed to run the query to generate output file.')
if not foundSlash:
countTillSlash = 0
helpers.trim_output_file(outputFile)
f.seek(0)
quote = f.read(countTillSlash)
print("Start characters in file skipped.", quote)
post = b'\x0e\x01\x08#select\x01\x01\x00s\x00'
contents = f.read(length - len(post) - countTillSlash)
quote = f.read(len(post))
if quote != post:
print("Unexpected end character in file.", quote)
finally:
f.close()
f = open(outputFile, "wb")
f.write(contents)
f.close()
if os.path.isfile(bqrsFile):
os.remove(bqrsFile) # Cleanup
print("Removed temp BQRS file", bqrsFile)
cmd = ['codeql', 'test', 'run', testDir]
print('Running ' + ' '.join(cmd))

View File

@@ -0,0 +1,57 @@
import sys
import os
import helpers
print('Script to generate stub.cs file from a nuget package')
print('Usage: python ' + sys.argv[0] +
' NUGET_PACKAGE_NAME [VERSION=latest] [WORK_DIR=tempDir] [OUTPUT_NAME=stub]')
if len(sys.argv) < 2:
print("\nPlease supply a nuget package name.")
exit(1)
thisScript = sys.argv[0]
thisDir = os.path.abspath(os.path.dirname(thisScript))
nuget = sys.argv[1]
workDir = os.path.abspath(helpers.get_argv(3, "tempDir"))
projectName = "proj"
projectDir = os.path.join(workDir, projectName)
dbName = 'db'
dbDir = os.path.join(workDir, dbName)
outputName = helpers.get_argv(4, "stub")
outputFile = os.path.join(workDir, outputName + '.cs')
bqrsFile = os.path.join(workDir, outputName + '.bqrs')
version = helpers.get_argv(2, "latest")
print("\nCreating new project")
helpers.run_cmd(['dotnet', 'new', 'classlib', "--language", "C#", '--name',
projectName, '--output', projectDir])
print("\nAdding reference")
cmd = ['dotnet', 'add', projectDir, 'package', nuget]
if (version != "latest"):
cmd.append('--version')
cmd.append(version)
helpers.run_cmd(cmd)
print("\nCreating DB")
helpers.run_cmd(['codeql', 'database', 'create', dbDir, '--language=csharp',
'--command', 'dotnet build /t:rebuild ' + projectDir])
if not os.path.isdir(dbDir):
print("Expected database directory " + dbDir + " not found.")
exit(1)
print("\nRunning query")
helpers.run_cmd(['codeql', 'query', 'run', os.path.join(
thisDir, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', bqrsFile])
helpers.run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output',
outputFile, '--format=text', '--no-titles'])
helpers.trim_output_file(outputFile)
print("\nOutput: " + outputFile)
exit(0)