Files
Bear-mirror/test/bin/assert_compilation
2020-06-16 21:13:23 +10:00

83 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import argparse
import json
import logging
def main():
logging.basicConfig(format="assert_compilation: %(message)s", level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument(dest='input', type=argparse.FileType(mode='r'))
subparsers = parser.add_subparsers(dest='command')
count_parser = subparsers.add_parser('count').add_mutually_exclusive_group()
count_parser.add_argument('-eq', dest='eq', type=int)
count_parser.add_argument('-gt', dest='gt', type=int)
count_parser.add_argument('-ge', dest='ge', type=int)
count_parser.add_argument('-lt', dest='lt', type=int)
count_parser.add_argument('-le', dest='le', type=int)
entry_parser = subparsers.add_parser('contains')
entry_parser.add_argument('-file')
entry_parser.add_argument('-output')
entry_parser.add_argument('-directory')
entry_parser.add_argument('-arguments', nargs=argparse.REMAINDER)
args = parser.parse_args()
logging.debug(args)
commands = json.load(args.input)
if args.command == 'count':
count = len(commands)
logging.debug("%s has %d command entries", args.input.name, count)
if args.eq is not None and args.eq != count:
logging.info('failed: expected %d, but got %d', args.eq, count)
return 1
elif args.gt is not None and args.gt >= count:
logging.info('failed: expected greater than %d, but got %d', args.gt, count)
return 1
elif args.ge is not None and args.ge > count:
logging.info('failed: expected greater or equal %d, but got %d', args.ge, count)
return 1
elif args.lt is not None and args.lt <= count:
logging.info('failed: expected less than %d, but got %d', args.lt, count)
return 1
elif args.le is not None and args.le < count:
logging.info('failed: expected less or equal %d, but got %d', args.le, count)
return 1
elif args.command == 'contains':
logging.debug("%s has command entries: %s", args.input.name, commands)
check = filter_from(args)
count = len([cmd for cmd in commands if check(cmd)])
if count == 0:
request = to_string(args)
logging.info('failed: expected at least one entry, but found none: %s', request)
return 1
return 0
def filter_from(args):
def test(command):
p1 = True if args.file is None else command['file'] == args.file
p2 = True if args.output is None else command['output'] == args.output
p3 = True if args.directory is None else command['directory'] == args.directory
p4 = True if args.arguments is None else command['arguments'] == args.arguments
return p1 and p2 and p3 and p4
return test
def to_string(args):
p1 = '' if args.file is None else '-file {}'.format(args.file)
p2 = '' if args.output is None else '-output {}'.format(args.output)
p3 = '' if args.directory is None else '-directory {}'.format(args.directory)
p4 = '' if args.arguments is None else ' '.join(['-arguments'] + args.arguments)
return ' '.join([p1, p2, p3, p4])
if __name__ == "__main__":
sys.exit(main())