diff options
author | Zach White <skullydazed@gmail.com> | 2021-05-10 11:18:44 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-10 11:18:44 -0700 |
commit | a3e7f3e7c58ee98596ead5c213f3a9ed8340cd80 (patch) | |
tree | ef0cb85205fad7562045f23caa8a16384e43bbb5 /lib/python/qmk/cli/cformat.py | |
parent | 66ed80ad3a0edecd9d7abbef71fc2a6e3e59b541 (diff) | |
download | qmk_firmware-a3e7f3e7c58ee98596ead5c213f3a9ed8340cd80.tar.gz qmk_firmware-a3e7f3e7c58ee98596ead5c213f3a9ed8340cd80.zip |
Improve our CI tests (#11476)
* add a test and dry-run to qmk generate-api
* add a dry-run to qmk pyformat
* Add a --dry-run to qmk cformat
* reverse the order of nose2 and flake8 tests
* run CI test against cformat and pyformat
* fix programming errors
* tweak job name
* fix argument
* refine the files we select
* fix stack trace in --ci
* make cformat exit clean
* fix c file extensions
* decouple CI from pyformat
* remove --ci arg
* make ci happy
* use the environment var instead
* change output to text
* fix log message
* replace tabs
Diffstat (limited to 'lib/python/qmk/cli/cformat.py')
-rw-r--r-- | lib/python/qmk/cli/cformat.py | 119 |
1 files changed, 90 insertions, 29 deletions
diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index d0d3b3b0a..9333aaec4 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py | |||
@@ -1,6 +1,7 @@ | |||
1 | """Format C code according to QMK's style. | 1 | """Format C code according to QMK's style. |
2 | """ | 2 | """ |
3 | import subprocess | 3 | import subprocess |
4 | from os import path | ||
4 | from shutil import which | 5 | from shutil import which |
5 | 6 | ||
6 | from argcomplete.completers import FilesCompleter | 7 | from argcomplete.completers import FilesCompleter |
@@ -9,58 +10,118 @@ from milc import cli | |||
9 | from qmk.path import normpath | 10 | from qmk.path import normpath |
10 | from qmk.c_parse import c_source_files | 11 | from qmk.c_parse import c_source_files |
11 | 12 | ||
13 | c_file_suffixes = ('c', 'h', 'cpp') | ||
14 | core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms') | ||
15 | ignored = ('tmk_core/protocol/usb_hid', 'quantum/template', 'platforms/chibios') | ||
12 | 16 | ||
13 | def cformat_run(files, all_files): | 17 | |
14 | """Spawn clang-format subprocess with proper arguments | 18 | def find_clang_format(): |
19 | """Returns the path to clang-format. | ||
15 | """ | 20 | """ |
16 | # Determine which version of clang-format to use | ||
17 | clang_format = ['clang-format', '-i'] | ||
18 | for clang_version in range(20, 6, -1): | 21 | for clang_version in range(20, 6, -1): |
19 | binary = 'clang-format-%d' % clang_version | 22 | binary = f'clang-format-{clang_version}' |
23 | |||
20 | if which(binary): | 24 | if which(binary): |
21 | clang_format[0] = binary | 25 | return binary |
22 | break | 26 | |
27 | return 'clang-format' | ||
28 | |||
29 | |||
30 | def find_diffs(files): | ||
31 | """Run clang-format and diff it against a file. | ||
32 | """ | ||
33 | found_diffs = False | ||
34 | |||
35 | for file in files: | ||
36 | cli.log.debug('Checking for changes in %s', file) | ||
37 | clang_format = subprocess.Popen([find_clang_format(), file], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) | ||
38 | diff = cli.run(['diff', '-u', f'--label=a/{file}', f'--label=b/{file}', str(file), '-'], stdin=clang_format.stdout, capture_output=True) | ||
39 | |||
40 | if diff.returncode != 0: | ||
41 | print(diff.stdout) | ||
42 | found_diffs = True | ||
43 | |||
44 | return found_diffs | ||
45 | |||
46 | |||
47 | def cformat_run(files): | ||
48 | """Spawn clang-format subprocess with proper arguments | ||
49 | """ | ||
50 | # Determine which version of clang-format to use | ||
51 | clang_format = [find_clang_format(), '-i'] | ||
52 | |||
23 | try: | 53 | try: |
24 | if not files: | 54 | cli.run(clang_format + list(map(str, files)), check=True, capture_output=False) |
25 | cli.log.warn('No changes detected. Use "qmk cformat -a" to format all files') | ||
26 | return False | ||
27 | subprocess.run(clang_format + [file for file in files], check=True) | ||
28 | cli.log.info('Successfully formatted the C code.') | 55 | cli.log.info('Successfully formatted the C code.') |
56 | return True | ||
29 | 57 | ||
30 | except subprocess.CalledProcessError: | 58 | except subprocess.CalledProcessError as e: |
31 | cli.log.error('Error formatting C code!') | 59 | cli.log.error('Error formatting C code!') |
60 | cli.log.debug('%s exited with returncode %s', e.cmd, e.returncode) | ||
61 | cli.log.debug('STDOUT:') | ||
62 | cli.log.debug(e.stdout) | ||
63 | cli.log.debug('STDERR:') | ||
64 | cli.log.debug(e.stderr) | ||
32 | return False | 65 | return False |
33 | 66 | ||
34 | 67 | ||
35 | @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.') | 68 | def filter_files(files): |
69 | """Yield only files to be formatted and skip the rest | ||
70 | """ | ||
71 | for file in files: | ||
72 | if file.name.split('.')[-1] in c_file_suffixes: | ||
73 | yield file | ||
74 | else: | ||
75 | cli.log.debug('Skipping file %s', file) | ||
76 | |||
77 | |||
78 | @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.") | ||
36 | @cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.') | 79 | @cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.') |
37 | @cli.argument('files', nargs='*', arg_only=True, completer=FilesCompleter('.c'), help='Filename(s) to format.') | 80 | @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.') |
81 | @cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.') | ||
38 | @cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True) | 82 | @cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True) |
39 | def cformat(cli): | 83 | def cformat(cli): |
40 | """Format C code according to QMK's style. | 84 | """Format C code according to QMK's style. |
41 | """ | 85 | """ |
42 | # Empty array for files | ||
43 | files = [] | ||
44 | # Core directories for formatting | ||
45 | core_dirs = ['drivers', 'quantum', 'tests', 'tmk_core', 'platforms'] | ||
46 | ignores = ['tmk_core/protocol/usb_hid', 'quantum/template', 'platforms/chibios'] | ||
47 | # Find the list of files to format | 86 | # Find the list of files to format |
48 | if cli.args.files: | 87 | if cli.args.files: |
49 | files.extend(normpath(file) for file in cli.args.files) | 88 | files = list(filter_files(cli.args.files)) |
89 | |||
90 | if not files: | ||
91 | cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files))) | ||
92 | exit(0) | ||
93 | |||
50 | if cli.args.all_files: | 94 | if cli.args.all_files: |
51 | cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files))) | 95 | cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files))) |
52 | # If -a is specified | 96 | |
53 | elif cli.args.all_files: | 97 | elif cli.args.all_files: |
54 | all_files = c_source_files(core_dirs) | 98 | all_files = c_source_files(core_dirs) |
55 | # The following statement checks each file to see if the file path is in the ignored directories. | 99 | # The following statement checks each file to see if the file path is in the ignored directories. |
56 | files.extend(file for file in all_files if not any(i in str(file) for i in ignores)) | 100 | files = [file for file in all_files if not any(i in str(file) for i in ignored)] |
57 | # No files specified & no -a flag | 101 | |
58 | else: | 102 | else: |
59 | base_args = ['git', 'diff', '--name-only', cli.args.base_branch] | 103 | git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs] |
60 | out = subprocess.run(base_args + core_dirs, check=True, stdout=subprocess.PIPE) | 104 | git_diff = cli.run(git_diff_cmd) |
61 | changed_files = filter(None, out.stdout.decode('UTF-8').split('\n')) | 105 | |
62 | filtered_files = [normpath(file) for file in changed_files if not any(i in file for i in ignores)] | 106 | if git_diff.returncode != 0: |
63 | files.extend(file for file in filtered_files if file.exists() and file.suffix in ['.c', '.h', '.cpp']) | 107 | cli.log.error("Error running %s", git_diff_cmd) |
108 | print(git_diff.stderr) | ||
109 | return git_diff.returncode | ||
110 | |||
111 | files = [] | ||
112 | |||
113 | for file in git_diff.stdout.strip().split('\n'): | ||
114 | if not any([file.startswith(ignore) for ignore in ignored]): | ||
115 | if path.exists(file) and file.split('.')[-1] in c_file_suffixes: | ||
116 | files.append(file) | ||
117 | |||
118 | # Sanity check | ||
119 | if not files: | ||
120 | cli.log.error('No changed files detected. Use "qmk cformat -a" to format all files') | ||
121 | return False | ||
64 | 122 | ||
65 | # Run clang-format on the files we've found | 123 | # Run clang-format on the files we've found |
66 | cformat_run(files, cli.args.all_files) | 124 | if cli.args.dry_run: |
125 | return not find_diffs(files) | ||
126 | else: | ||
127 | return cformat_run(files) | ||