diff options
author | Everett Hildenbrandt <hildenb2@illinois.edu> | 2018-05-29 03:09:17 +0800 |
---|---|---|
committer | Everett Hildenbrandt <hildenb2@illinois.edu> | 2018-05-31 22:37:30 +0800 |
commit | bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1 (patch) | |
tree | 26c10e46439e5c1459e195c4bfa7b20f4bc48f0d | |
parent | 5071d1dfba2f1effd3ef5e745a2ce16dcf9e4003 (diff) | |
download | dexon-tests-bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1.tar dexon-tests-bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1.tar.gz dexon-tests-bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1.tar.bz2 dexon-tests-bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1.tar.lz dexon-tests-bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1.tar.xz dexon-tests-bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1.tar.zst dexon-tests-bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1.zip |
test.py: add generic infrastructure, usage messages, JSON reading, format command
-rwxr-xr-x | test.py | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -25,3 +25,49 @@ # - GeneralStateTest filler # eg. stVMTests/vmArithmeticTest/add0Filler.json +import sys +import os +import json + +def _report(*msg): + print("== " + sys.argv[0] + ":", *msg, file=sys.stderr) + +def _die(*msg, exit_code=1): + _report(*msg) + _report("exiting...") + sys.exit(exit_code) + +def readJSONFile(fname): + if not os.path.isfile(fname): + _die("Not a file:", fname) + with open(fname, "r") as f: + fcontents = f.read() + return json.loads(fcontents) + +def writeJSONFile(fname, fcontents): + if not os.path.exists(os.path.dirname(fname)): + os.makedirs(os.path.dirname(fname)) + with open(fname, "w") as f: + f.write(json.dumps(fcontents, indent=4, sort_keys=True)) + +def _usage(): + usage_lines = [ "" + , " usage: " + sys.argv[0] + " format <TEST_FILE>" + , " where:" + , " format: command to format/sort the JSON file." + , " <TEST_FILE>: JSON test file/filler to read and write with sorted keys/standard formatting." + ] + _die("\n".join(usage_lines)) + +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)) + else: + _usage() + +if __name__ == "__main__": + main() |