logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

test_cli_commands.py (14254B)


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