From 6ea80ef71b8d2975cdc840c3fe2c10979de8e4d4 Mon Sep 17 00:00:00 2001 From: Everett Hildenbrandt Date: Tue, 29 May 2018 13:03:41 -0600 Subject: test.py: more generic/common test listing infrastructure --- test.py | 63 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/test.py b/test.py index 0db989cb1..146537a5a 100755 --- a/test.py +++ b/test.py @@ -57,15 +57,20 @@ def writeJSONFile(fname, fcontents): with open(fname, "w") as f: f.write(json.dumps(fcontents, indent=4, sort_keys=True)) -def findTests(testDir="."): - return [ os.path.join(root, file) for root, _, files in os.walk(testDir) - for file in files - if file.endswith(".json") +def findTests(filePrefix=""): + return [ fullTest for fullTest in [ os.path.join(root, file) for root, _, files in os.walk(".") + for file in files + if file.endswith(".json") + ] + if fullTest.startswith(filePrefix) ] -def validateSchema(jsonFile, schemaFile): - _report("validating", jsonFile, "with", schemaFile) +def listTests(filePrefixes=[""]): + return [ test for fPrefix in filePrefixes + for test in findTests(filePrefix=fPrefix) + ] +def validateSchema(jsonFile, schemaFile): testSchema = readJSONFile(schemaFile) defSchema = readJSONFile("JSONSchema/definitions.json") schema = { "definitions" : dict(defSchema["definitions"], **testSchema["definitions"]) @@ -76,30 +81,24 @@ def validateSchema(jsonFile, schemaFile): jsonschema.validate(jsonInput, schema) def validateTestFile(jsonFile): - if jsonFile.startswith("src/GeneralStateTestsFiller/"): + elif jsonFile.startswith("./src/GeneralStateTestsFiller/"): validateSchema(jsonFile, "JSONSchema/st-filler-schema.json") - elif jsonFile.startswith("GeneralStateTests/"): + elif jsonFile.startswith("./GeneralStateTests/"): validateSchema(jsonFile, "JSONSchema/st-schema.json") - elif jsonFile.startswith("BlockchainTests/"): + elif jsonFile.startswith("./BlockchainTests/"): validateSchema(jsonFile, "JSONSchema/bc-schema.json") else: _die("Do not know how to validate file:", jsonFile) -def validateAllTests(): - for jsonFile in ( findTests(testDir="src/GeneralStateTestsFiller/") - + findTests(testDir="GeneralStateTests/") - + findTests(testDir="BlockchainTests/") - ): - validateTestFile(jsonFile) - def _usage(): usage_lines = [ "" - , " usage: " + sys.argv[0] + " format " - , " usage: " + sys.argv[0] + " validate [*]" + , " usage: " + sys.argv[0] + " [list|format|validate] [*]" , " where:" - , " format: command to format/sort the JSON file." - , " validate: command to check a file against the associated JSON schema (defaults to all files)." - , " : JSON test file/filler to read and write with sorted keys/standard formatting." + , " list: command to list the matching tests." + , " format: command to format/sort the JSON file." + , " validate: command to check a file against the associated JSON schema (defaults to all files)." + , " : file path prefix to search for tests with." + , " eg. './src/VMTestsFiller' './VMTests' for all VMTests and their fillers." ] _die("\n".join(usage_lines)) @@ -107,17 +106,23 @@ def main(): if len(sys.argv) < 2: _usage() test_command = sys.argv[1] - if test_command == "format": - file_name = sys.argv[2] - writeJSONFile(file_name, readJSONFile(file_name)) + if len(sys.argv) == 2: + testList = listTests() + else: + testList = listTests(filePrefixes=sys.argv[2:]) + + if test_command == "list": + testDo = lambda t: print(t) + elif test_command == "format": + testDo = lambda t: writeJSONFile(t, readJSONFile(t)) elif test_command == "validate": - if len(sys.argv) > 2: - for testFile in sys.argv[2:]: - validateTestFile(testFile) - else: - validateAllTests() + testDo = validateTestFile else: _usage() + for test in testList: + _report(test_command + ":", test) + testDo(test) + if __name__ == "__main__": main() -- cgit v1.2.3