test_cli_commands.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. from typing import Sequence
  2. import platform
  3. from subprocess import DEVNULL
  4. from milc import cli
  5. is_windows = 'windows' in platform.platform().lower()
  6. def check_subcommand(command, *args):
  7. cmd = ['qmk', command, *args]
  8. result = cli.run(cmd, stdin=DEVNULL, combined_output=True)
  9. return result
  10. def check_subcommand_stdin(file_to_read, command, *args):
  11. """Pipe content of a file to a command and return output.
  12. """
  13. with open(file_to_read, encoding='utf-8') as my_file:
  14. cmd = ['qmk', command, *args]
  15. result = cli.run(cmd, stdin=my_file, combined_output=True)
  16. return result
  17. def check_returncode(result, expected: Sequence[int] = [0]):
  18. """Print stdout if `result.returncode` does not match `expected`.
  19. """
  20. if result.returncode not in expected:
  21. print('`%s` stdout:' % ' '.join(result.args))
  22. print(result.stdout)
  23. print('returncode:', result.returncode)
  24. assert result.returncode in expected
  25. def test_format_c():
  26. result = check_subcommand('format-c', '-n', 'quantum/matrix.c')
  27. check_returncode(result)
  28. def test_format_c_all():
  29. result = check_subcommand('format-c', '-n', '-a')
  30. check_returncode(result, [0, 1])
  31. def test_compile():
  32. result = check_subcommand('compile', '-kb', 'handwired/pytest/basic', '-km', 'default', '-n')
  33. check_returncode(result)
  34. def test_compile_json():
  35. result = check_subcommand('compile', '-kb', 'handwired/pytest/basic', '-km', 'default_json', '-n')
  36. check_returncode(result)
  37. def test_flash():
  38. result = check_subcommand('flash', '-kb', 'handwired/pytest/basic', '-km', 'default', '-n')
  39. check_returncode(result)
  40. def test_flash_bootloaders():
  41. result = check_subcommand('flash', '-b')
  42. check_returncode(result, [1])
  43. def test_kle2json():
  44. result = check_subcommand('kle2json', 'lib/python/qmk/tests/kle.txt', '-f')
  45. check_returncode(result)
  46. assert 'Wrote out' in result.stdout
  47. def test_doctor():
  48. result = check_subcommand('doctor', '-n')
  49. check_returncode(result, [0, 1])
  50. assert 'QMK Doctor is checking your environment.' in result.stdout
  51. assert 'QMK is ready to go' in result.stdout
  52. def test_hello():
  53. result = check_subcommand('hello')
  54. check_returncode(result)
  55. assert 'Hello,' in result.stdout
  56. def test_format_python():
  57. result = check_subcommand('format-python', '-n', '-a')
  58. check_returncode(result)
  59. assert 'Successfully formatted the python code.' in result.stdout
  60. def test_list_keyboards():
  61. result = check_subcommand('list-keyboards')
  62. check_returncode(result)
  63. # check to see if a known keyboard is returned
  64. # this will fail if handwired/pytest/basic is removed
  65. assert 'handwired/pytest/basic' in result.stdout
  66. def test_list_keymaps():
  67. result = check_subcommand('list-keymaps', '-kb', 'handwired/pytest/basic')
  68. check_returncode(result)
  69. assert 'default' in result.stdout
  70. assert 'default_json' in result.stdout
  71. def test_list_keymaps_long():
  72. result = check_subcommand('list-keymaps', '--keyboard', 'handwired/pytest/basic')
  73. check_returncode(result)
  74. assert 'default' in result.stdout
  75. assert 'default_json' in result.stdout
  76. def test_list_keymaps_community():
  77. result = check_subcommand('list-keymaps', '--keyboard', 'handwired/pytest/has_community')
  78. check_returncode(result)
  79. assert 'test' in result.stdout
  80. def test_list_keymaps_kb_only():
  81. result = check_subcommand('list-keymaps', '-kb', 'contra')
  82. check_returncode(result)
  83. assert 'default' in result.stdout
  84. def test_list_keymaps_vendor_kb():
  85. result = check_subcommand('list-keymaps', '-kb', 'ai03/lunar')
  86. check_returncode(result)
  87. assert 'default' in result.stdout
  88. def test_list_keymaps_vendor_kb_rev():
  89. result = check_subcommand('list-keymaps', '-kb', 'kbdfans/kbd67/mkiirgb/v2')
  90. check_returncode(result)
  91. assert 'default' in result.stdout
  92. def test_list_keymaps_no_keyboard_found():
  93. result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl')
  94. check_returncode(result, [2])
  95. assert 'invalid keyboard_folder value' in result.stdout
  96. def test_json2c():
  97. result = check_subcommand('json2c', 'keyboards/handwired/pytest/basic/keymaps/default_json/keymap.json')
  98. check_returncode(result)
  99. assert result.stdout == """#include QMK_KEYBOARD_H
  100. #if __has_include("keymap.h")
  101. # include "keymap.h"
  102. #endif
  103. /* THIS FILE WAS GENERATED!
  104. *
  105. * This file was generated by qmk json2c. You may or may not want to
  106. * edit it directly.
  107. */
  108. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  109. [0] = LAYOUT_ortho_1x1(KC_A)
  110. };
  111. #ifdef OTHER_KEYMAP_C
  112. # include OTHER_KEYMAP_C
  113. #endif // OTHER_KEYMAP_C
  114. """
  115. def test_json2c_macros():
  116. result = check_subcommand("json2c", 'keyboards/handwired/pytest/macro/keymaps/default/keymap.json')
  117. check_returncode(result)
  118. assert 'LAYOUT_ortho_1x1(QK_MACRO_0)' in result.stdout
  119. assert 'case QK_MACRO_0:' in result.stdout
  120. assert 'SEND_STRING("Hello, World!"SS_TAP(X_ENTER));' in result.stdout
  121. def test_json2c_stdin():
  122. result = check_subcommand_stdin('keyboards/handwired/pytest/basic/keymaps/default_json/keymap.json', 'json2c', '-')
  123. check_returncode(result)
  124. assert result.stdout == """#include QMK_KEYBOARD_H
  125. #if __has_include("keymap.h")
  126. # include "keymap.h"
  127. #endif
  128. /* THIS FILE WAS GENERATED!
  129. *
  130. * This file was generated by qmk json2c. You may or may not want to
  131. * edit it directly.
  132. */
  133. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  134. [0] = LAYOUT_ortho_1x1(KC_A)
  135. };
  136. #ifdef OTHER_KEYMAP_C
  137. # include OTHER_KEYMAP_C
  138. #endif // OTHER_KEYMAP_C
  139. """
  140. def test_json2c_no_json():
  141. result = check_subcommand('json2c', 'keyboards/handwired/pytest/basic/keymaps/default/keymap.c')
  142. check_returncode(result, [1])
  143. assert 'Invalid JSON encountered' in result.stdout
  144. def test_info():
  145. result = check_subcommand('info', '-kb', 'handwired/pytest/basic')
  146. check_returncode(result)
  147. assert 'Keyboard Name: pytest' in result.stdout
  148. assert 'Processor: atmega32u4' in result.stdout
  149. assert 'Layout:' not in result.stdout
  150. assert 'k0' not in result.stdout
  151. def test_info_keyboard_render():
  152. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-l')
  153. check_returncode(result)
  154. assert 'Keyboard Name: pytest' in result.stdout
  155. assert 'Processor: atmega32u4' in result.stdout
  156. assert 'Layouts:' in result.stdout
  157. if is_windows:
  158. assert '| |' in result.stdout
  159. else:
  160. assert '│ │' in result.stdout
  161. def test_info_keymap_render():
  162. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-km', 'default_json')
  163. check_returncode(result)
  164. assert 'Keyboard Name: pytest' in result.stdout
  165. assert 'Processor: atmega32u4' in result.stdout
  166. if is_windows:
  167. assert '|A |' in result.stdout
  168. else:
  169. assert '│A │' in result.stdout
  170. def test_info_matrix_render():
  171. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-m')
  172. check_returncode(result)
  173. assert 'Keyboard Name: pytest' in result.stdout
  174. assert 'Processor: atmega32u4' in result.stdout
  175. assert 'LAYOUT_ortho_1x1' in result.stdout
  176. if is_windows:
  177. assert '|0A|' in result.stdout
  178. else:
  179. assert '│0A│' in result.stdout
  180. assert 'Matrix for "LAYOUT_ortho_1x1"' in result.stdout
  181. def test_c2json():
  182. result = check_subcommand("c2json", "-kb", "handwired/pytest/basic", "-km", "default", "keyboards/handwired/pytest/basic/keymaps/default/keymap.c")
  183. check_returncode(result)
  184. assert result.stdout.strip() == '{"keyboard": "handwired/pytest/basic", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
  185. def test_c2json_stdin():
  186. result = check_subcommand_stdin("keyboards/handwired/pytest/basic/keymaps/default/keymap.c", "c2json", "-kb", "handwired/pytest/basic", "-km", "default", "-")
  187. check_returncode(result)
  188. assert result.stdout.strip() == '{"keyboard": "handwired/pytest/basic", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
  189. def test_clean():
  190. result = check_subcommand('clean', '-a')
  191. check_returncode(result)
  192. assert (result.stdout.count('done') == 2 and 'userspace' not in result.stdout) or (result.stdout.count('done') == 3 and 'userspace' in result.stdout)
  193. def test_generate_api():
  194. result = check_subcommand('generate-api', '--dry-run', '--filter', 'handwired/pytest')
  195. check_returncode(result)
  196. def test_generate_rgb_breathe_table():
  197. result = check_subcommand("generate-rgb-breathe-table", "-c", "1.2", "-m", "127")
  198. check_returncode(result)
  199. assert 'Breathing center: 1.2' in result.stdout
  200. assert 'Breathing max: 127' in result.stdout
  201. def test_generate_config_h():
  202. result = check_subcommand('generate-config-h', '-kb', 'handwired/pytest/basic')
  203. check_returncode(result)
  204. assert '# define DEVICE_VER 0x0001' in result.stdout
  205. assert '# define DIODE_DIRECTION COL2ROW' in result.stdout
  206. assert '# define MANUFACTURER "none"' in result.stdout
  207. assert '# define PRODUCT "pytest"' in result.stdout
  208. assert '# define PRODUCT_ID 0x6465' in result.stdout
  209. assert '# define VENDOR_ID 0xFEED' in result.stdout
  210. assert '# define MATRIX_COLS 1' in result.stdout
  211. assert '# define MATRIX_COL_PINS { F4 }' in result.stdout
  212. assert '# define MATRIX_ROWS 1' in result.stdout
  213. assert '# define MATRIX_ROW_PINS { F5 }' in result.stdout
  214. def test_generate_rules_mk():
  215. result = check_subcommand('generate-rules-mk', '-kb', 'handwired/pytest/basic')
  216. check_returncode(result)
  217. assert 'BOOTLOADER ?= atmel-dfu' in result.stdout
  218. assert 'MCU ?= atmega32u4' in result.stdout
  219. def test_generate_version_h():
  220. result = check_subcommand('generate-version-h')
  221. check_returncode(result)
  222. assert '#define QMK_VERSION' in result.stdout
  223. def test_format_json_keyboard():
  224. result = check_subcommand('format-json', '--format', 'keyboard', 'lib/python/qmk/tests/minimal_info.json')
  225. check_returncode(result)
  226. assert result.stdout == '{\n "keyboard_name": "tester",\n "maintainer": "qmk",\n "layouts": {\n "LAYOUT": {\n "layout": [\n {"label": "KC_A", "matrix": [0, 0], "x": 0, "y": 0}\n ]\n }\n }\n}\n'
  227. def test_format_json_keymap():
  228. result = check_subcommand('format-json', '--format', 'keymap', 'lib/python/qmk/tests/minimal_keymap.json')
  229. check_returncode(result)
  230. assert result.stdout == '{\n "version": 1,\n "keyboard": "handwired/pytest/basic",\n "keymap": "test",\n "layout": "LAYOUT_ortho_1x1",\n "layers": [\n [\n "KC_A"\n ]\n ]\n}\n'
  231. def test_format_json_keyboard_auto():
  232. result = check_subcommand('format-json', '--format', 'auto', 'lib/python/qmk/tests/minimal_info.json')
  233. check_returncode(result)
  234. assert result.stdout == '{\n "keyboard_name": "tester",\n "maintainer": "qmk",\n "layouts": {\n "LAYOUT": {\n "layout": [\n {"label": "KC_A", "matrix": [0, 0], "x": 0, "y": 0}\n ]\n }\n }\n}\n'
  235. def test_format_json_keymap_auto():
  236. result = check_subcommand('format-json', '--format', 'auto', 'lib/python/qmk/tests/minimal_keymap.json')
  237. check_returncode(result)
  238. assert result.stdout == '{\n "keyboard": "handwired/pytest/basic",\n "keymap": "test",\n "layers": [\n ["KC_A"]\n ],\n "layout": "LAYOUT_ortho_1x1",\n "version": 1\n}\n'
  239. def test_find_exists():
  240. result = check_subcommand('find', '-f', 'exists(rgb_matrix.split_count)', '-p', 'rgb_matrix.split_count')
  241. check_returncode(result)
  242. values = [s for s in result.stdout.splitlines() if 'rgb_matrix.split_count=' in s]
  243. assert len(values) > 0
  244. for s in values:
  245. assert '=None' not in s
  246. assert '=[' in s
  247. def test_find_absent():
  248. result = check_subcommand('find', '-f', 'absent(rgb_matrix.split_count)', '-p', 'rgb_matrix.split_count')
  249. check_returncode(result)
  250. values = [s for s in result.stdout.splitlines() if 'rgb_matrix.split_count=' in s]
  251. assert len(values) > 0
  252. for s in values:
  253. assert '=None' in s
  254. assert '=[' not in s
  255. def test_find_length():
  256. result = check_subcommand('find', '-f', 'length(matrix_pins.cols, 6)', '-p', 'matrix_pins.cols')
  257. check_returncode(result)
  258. values = [s for s in result.stdout.splitlines() if 'matrix_pins.cols=' in s]
  259. assert len(values) > 0
  260. for s in values:
  261. assert s.count(',') == 5
  262. def test_find_contains():
  263. result = check_subcommand('find', '-f', 'contains(matrix_pins.cols, B1)', '-p', 'matrix_pins.cols')
  264. check_returncode(result)
  265. values = [s for s in result.stdout.splitlines() if 'matrix_pins.cols=' in s]
  266. assert len(values) > 0
  267. for s in values:
  268. assert "'B1'" in s
  269. def test_find_multiple_conditions():
  270. # this is intended to match at least 'crkbd/rev1'
  271. result = check_subcommand(
  272. 'find', '-f', 'exists(rgb_matrix.split_count)', '-f', 'contains(matrix_pins.cols, B1)', '-f', 'length(matrix_pins.cols, 6)', '-f', 'absent(eeprom.driver)', '-f', 'ws2812.pin == D3', '-p', 'rgb_matrix.split_count', '-p', 'matrix_pins.cols', '-p',
  273. 'eeprom.driver', '-p', 'ws2812.pin'
  274. )
  275. check_returncode(result)
  276. rgb_matrix_split_count_values = [s for s in result.stdout.splitlines() if 'rgb_matrix.split_count=' in s]
  277. assert len(rgb_matrix_split_count_values) > 0
  278. for s in rgb_matrix_split_count_values:
  279. assert '=None' not in s
  280. assert '=[' in s
  281. matrix_pins_cols_values = [s for s in result.stdout.splitlines() if 'matrix_pins.cols=' in s]
  282. assert len(matrix_pins_cols_values) > 0
  283. for s in matrix_pins_cols_values:
  284. assert s.count(',') == 5
  285. assert "'B1'" in s
  286. eeprom_driver_values = [s for s in result.stdout.splitlines() if 'eeprom.driver=' in s]
  287. assert len(eeprom_driver_values) > 0
  288. for s in eeprom_driver_values:
  289. assert '=None' in s
  290. ws2812_pin_values = [s for s in result.stdout.splitlines() if 'ws2812.pin=' in s]
  291. assert len(ws2812_pin_values) > 0
  292. for s in ws2812_pin_values:
  293. assert '=D3' in s