test_cli_commands.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. assert 'via' 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. assert 'via' in result.stdout
  89. def test_list_keymaps_vendor_kb_rev():
  90. result = check_subcommand('list-keymaps', '-kb', 'kbdfans/kbd67/mkiirgb/v2')
  91. check_returncode(result)
  92. assert 'default' in result.stdout
  93. assert 'via' in result.stdout
  94. def test_list_keymaps_no_keyboard_found():
  95. result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl')
  96. check_returncode(result, [2])
  97. assert 'invalid keyboard_folder value' in result.stdout
  98. def test_json2c():
  99. result = check_subcommand('json2c', 'keyboards/handwired/pytest/has_template/keymaps/default_json/keymap.json')
  100. check_returncode(result)
  101. assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT_ortho_1x1(KC_A)};\n\n'
  102. def test_json2c_macros():
  103. result = check_subcommand("json2c", 'keyboards/handwired/pytest/macro/keymaps/default/keymap.json')
  104. check_returncode(result)
  105. assert 'LAYOUT_ortho_1x1(MACRO_0)' in result.stdout
  106. assert 'case MACRO_0:' in result.stdout
  107. assert 'SEND_STRING("Hello, World!"SS_TAP(X_ENTER));' in result.stdout
  108. def test_json2c_stdin():
  109. result = check_subcommand_stdin('keyboards/handwired/pytest/has_template/keymaps/default_json/keymap.json', 'json2c', '-')
  110. check_returncode(result)
  111. assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT_ortho_1x1(KC_A)};\n\n'
  112. def test_json2c_wrong_json():
  113. result = check_subcommand('json2c', 'keyboards/handwired/pytest/info.json')
  114. check_returncode(result, [1])
  115. assert 'Invalid JSON keymap' in result.stdout
  116. def test_json2c_no_json():
  117. result = check_subcommand('json2c', 'keyboards/handwired/pytest/pytest.h')
  118. check_returncode(result, [1])
  119. assert 'Invalid JSON encountered' in result.stdout
  120. def test_info():
  121. result = check_subcommand('info', '-kb', 'handwired/pytest/basic')
  122. check_returncode(result)
  123. assert 'Keyboard Name: pytest' in result.stdout
  124. assert 'Processor: atmega32u4' in result.stdout
  125. assert 'Layout:' not in result.stdout
  126. assert 'k0' not in result.stdout
  127. def test_info_keyboard_render():
  128. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-l')
  129. check_returncode(result)
  130. assert 'Keyboard Name: pytest' in result.stdout
  131. assert 'Processor: atmega32u4' in result.stdout
  132. assert 'Layouts:' in result.stdout
  133. assert 'k0' in result.stdout
  134. def test_info_keymap_render():
  135. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-km', 'default_json')
  136. check_returncode(result)
  137. assert 'Keyboard Name: pytest' in result.stdout
  138. assert 'Processor: atmega32u4' in result.stdout
  139. if is_windows:
  140. assert '|A |' in result.stdout
  141. else:
  142. assert '│A │' in result.stdout
  143. def test_info_matrix_render():
  144. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-m')
  145. check_returncode(result)
  146. assert 'Keyboard Name: pytest' in result.stdout
  147. assert 'Processor: atmega32u4' in result.stdout
  148. assert 'LAYOUT_ortho_1x1' in result.stdout
  149. if is_windows:
  150. assert '|0A|' in result.stdout
  151. else:
  152. assert '│0A│' in result.stdout
  153. assert 'Matrix for "LAYOUT_ortho_1x1"' in result.stdout
  154. def test_c2json():
  155. result = check_subcommand("c2json", "-kb", "handwired/pytest/has_template", "-km", "default", "keyboards/handwired/pytest/has_template/keymaps/default/keymap.c")
  156. check_returncode(result)
  157. assert result.stdout.strip() == '{"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
  158. def test_c2json_nocpp():
  159. result = check_subcommand("c2json", "--no-cpp", "-kb", "handwired/pytest/has_template", "-km", "default", "keyboards/handwired/pytest/has_template/keymaps/nocpp/keymap.c")
  160. check_returncode(result)
  161. assert result.stdout.strip() == '{"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
  162. def test_c2json_stdin():
  163. result = check_subcommand_stdin("keyboards/handwired/pytest/has_template/keymaps/default/keymap.c", "c2json", "-kb", "handwired/pytest/has_template", "-km", "default", "-")
  164. check_returncode(result)
  165. assert result.stdout.strip() == '{"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
  166. def test_c2json_nocpp_stdin():
  167. result = check_subcommand_stdin("keyboards/handwired/pytest/has_template/keymaps/nocpp/keymap.c", "c2json", "--no-cpp", "-kb", "handwired/pytest/has_template", "-km", "default", "-")
  168. check_returncode(result)
  169. assert result.stdout.strip() == '{"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
  170. def test_clean():
  171. result = check_subcommand('clean', '-a')
  172. check_returncode(result)
  173. assert result.stdout.count('done') == 2
  174. def test_generate_api():
  175. result = check_subcommand('generate-api', '--dry-run', '--filter', 'handwired/pytest')
  176. check_returncode(result)
  177. def test_generate_rgb_breathe_table():
  178. result = check_subcommand("generate-rgb-breathe-table", "-c", "1.2", "-m", "127")
  179. check_returncode(result)
  180. assert 'Breathing center: 1.2' in result.stdout
  181. assert 'Breathing max: 127' in result.stdout
  182. def test_generate_config_h():
  183. result = check_subcommand('generate-config-h', '-kb', 'handwired/pytest/basic')
  184. check_returncode(result)
  185. assert '# define DEVICE_VER 0x0001' in result.stdout
  186. assert '# define DIODE_DIRECTION COL2ROW' in result.stdout
  187. assert '# define MANUFACTURER none' in result.stdout
  188. assert '# define PRODUCT pytest' in result.stdout
  189. assert '# define PRODUCT_ID 0x6465' in result.stdout
  190. assert '# define VENDOR_ID 0xFEED' in result.stdout
  191. assert '# define MATRIX_COLS 1' in result.stdout
  192. assert '# define MATRIX_COL_PINS { F4 }' in result.stdout
  193. assert '# define MATRIX_ROWS 1' in result.stdout
  194. assert '# define MATRIX_ROW_PINS { F5 }' in result.stdout
  195. def test_generate_rules_mk():
  196. result = check_subcommand('generate-rules-mk', '-kb', 'handwired/pytest/basic')
  197. check_returncode(result)
  198. assert 'BOOTLOADER ?= atmel-dfu' in result.stdout
  199. assert 'MCU ?= atmega32u4' in result.stdout
  200. def test_generate_version_h():
  201. result = check_subcommand('generate-version-h')
  202. check_returncode(result)
  203. assert '#define QMK_VERSION' in result.stdout
  204. def test_generate_layouts():
  205. result = check_subcommand('generate-layouts', '-kb', 'handwired/pytest/basic')
  206. check_returncode(result)
  207. assert '#define LAYOUT_custom(k0A) {' in result.stdout
  208. def test_format_json_keyboard():
  209. result = check_subcommand('format-json', '--format', 'keyboard', 'lib/python/qmk/tests/minimal_info.json')
  210. check_returncode(result)
  211. 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'
  212. def test_format_json_keymap():
  213. result = check_subcommand('format-json', '--format', 'keymap', 'lib/python/qmk/tests/minimal_keymap.json')
  214. check_returncode(result)
  215. 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'
  216. def test_format_json_keyboard_auto():
  217. result = check_subcommand('format-json', '--format', 'auto', 'lib/python/qmk/tests/minimal_info.json')
  218. check_returncode(result)
  219. 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'
  220. def test_format_json_keymap_auto():
  221. result = check_subcommand('format-json', '--format', 'auto', 'lib/python/qmk/tests/minimal_keymap.json')
  222. check_returncode(result)
  223. 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'