keyboard stuff
1import fnmatch
2import re
3from subprocess import DEVNULL
4
5from milc import cli
6
7from qmk.commands import find_make, get_make_parallel_args, build_environment
8
9
10@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
11@cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
12@cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
13@cli.argument('-l', '--list', arg_only=True, action='store_true', help='List available tests.')
14@cli.argument('-t', '--test', arg_only=True, action='append', default=[], help="Test to run from the available list. Supports wildcard globs. May be passed multiple times.")
15@cli.subcommand("QMK C Unit Tests.", hidden=False if cli.config.user.developer else True)
16def test_c(cli):
17 """Run native unit tests.
18 """
19 list_tests = cli.run([find_make(), 'list-tests', 'SILENT=true'])
20 available_tests = sorted(list_tests.stdout.strip().split())
21
22 if cli.args.list:
23 return print("\n".join(available_tests))
24
25 # expand any wildcards
26 filtered_tests = set()
27 for test in cli.args.test:
28 regex = re.compile(fnmatch.translate(test))
29 filtered_tests |= set(filter(regex.match, available_tests))
30
31 for invalid in filtered_tests - set(available_tests):
32 cli.log.warning(f'Invalid test provided: {invalid}')
33
34 # convert test names to build targets
35 targets = list(map(lambda x: f'test:{x}', filtered_tests or ['all']))
36
37 if cli.args.clean:
38 targets.insert(0, 'clean')
39
40 # Add in the environment vars
41 for key, value in build_environment(cli.args.env).items():
42 targets.append(f'{key}={value}')
43
44 command = [find_make(), *get_make_parallel_args(cli.config.test_c.parallel), *targets]
45
46 cli.log.info('Compiling tests with {fg_cyan}%s', ' '.join(command))
47 return cli.run(command, capture_output=False, stdin=DEVNULL).returncode