test_cli_commands.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import platform
  2. from subprocess import STDOUT, PIPE
  3. from qmk.commands import run
  4. is_windows = 'windows' in platform.platform().lower()
  5. def check_subcommand(command, *args):
  6. cmd = ['bin/qmk', command, *args]
  7. result = run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=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) as my_file:
  13. cmd = ['bin/qmk', command, *args]
  14. result = run(cmd, stdin=my_file, stdout=PIPE, stderr=STDOUT, universal_newlines=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_cformat():
  25. result = check_subcommand('cformat', 'quantum/matrix.c')
  26. check_returncode(result)
  27. def test_compile():
  28. result = check_subcommand('compile', '-kb', 'handwired/pytest/basic', '-km', 'default', '-n')
  29. check_returncode(result)
  30. def test_compile_json():
  31. result = check_subcommand('compile', '-kb', 'handwired/pytest/basic', '-km', 'default_json', '-n')
  32. check_returncode(result)
  33. def test_flash():
  34. result = check_subcommand('flash', '-kb', 'handwired/pytest/basic', '-km', 'default', '-n')
  35. check_returncode(result)
  36. def test_flash_bootloaders():
  37. result = check_subcommand('flash', '-b')
  38. check_returncode(result, [1])
  39. def test_config():
  40. result = check_subcommand('config')
  41. check_returncode(result)
  42. assert 'general.color' in result.stdout
  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_pyformat():
  57. result = check_subcommand('pyformat')
  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' and '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' and 'default_json' in result.stdout
  74. def test_list_keymaps_community():
  75. result = check_subcommand('list-keymaps', '--keyboard', 'handwired/pytest/has_community')
  76. check_returncode(result)
  77. assert 'test' in result.stdout
  78. def test_list_keymaps_kb_only():
  79. result = check_subcommand('list-keymaps', '-kb', 'niu_mini')
  80. check_returncode(result)
  81. assert 'default' and 'via' in result.stdout
  82. def test_list_keymaps_vendor_kb():
  83. result = check_subcommand('list-keymaps', '-kb', 'ai03/lunar')
  84. check_returncode(result)
  85. assert 'default' and 'via' in result.stdout
  86. def test_list_keymaps_vendor_kb_rev():
  87. result = check_subcommand('list-keymaps', '-kb', 'kbdfans/kbd67/mkiirgb/v2')
  88. check_returncode(result)
  89. assert 'default' and 'via' in result.stdout
  90. def test_list_keymaps_no_keyboard_found():
  91. result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl')
  92. check_returncode(result, [1])
  93. assert 'does not exist' in result.stdout
  94. def test_json2c():
  95. result = check_subcommand('json2c', 'keyboards/handwired/pytest/has_template/keymaps/default_json/keymap.json')
  96. check_returncode(result)
  97. 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'
  98. def test_json2c_stdin():
  99. result = check_subcommand_stdin('keyboards/handwired/pytest/has_template/keymaps/default_json/keymap.json', 'json2c', '-')
  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_info():
  103. result = check_subcommand('info', '-kb', 'handwired/pytest/basic')
  104. check_returncode(result)
  105. assert 'Keyboard Name: handwired/pytest/basic' in result.stdout
  106. assert 'Processor: atmega32u4' in result.stdout
  107. assert 'Layout:' not in result.stdout
  108. assert 'k0' not in result.stdout
  109. def test_info_keyboard_render():
  110. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-l')
  111. check_returncode(result)
  112. assert 'Keyboard Name: handwired/pytest/basic' in result.stdout
  113. assert 'Processor: atmega32u4' in result.stdout
  114. assert 'Layouts:' in result.stdout
  115. assert 'k0' in result.stdout
  116. def test_info_keymap_render():
  117. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-km', 'default_json')
  118. check_returncode(result)
  119. assert 'Keyboard Name: handwired/pytest/basic' in result.stdout
  120. assert 'Processor: atmega32u4' in result.stdout
  121. if is_windows:
  122. assert '|A |' in result.stdout
  123. else:
  124. assert '│A │' in result.stdout
  125. def test_info_matrix_render():
  126. result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-m')
  127. check_returncode(result)
  128. assert 'Keyboard Name: handwired/pytest/basic' in result.stdout
  129. assert 'Processor: atmega32u4' in result.stdout
  130. assert 'LAYOUT_ortho_1x1' in result.stdout
  131. if is_windows:
  132. assert '|0A|' in result.stdout
  133. else:
  134. assert '│0A│' in result.stdout
  135. assert 'Matrix for "LAYOUT_ortho_1x1"' in result.stdout
  136. def test_c2json():
  137. result = check_subcommand("c2json", "-kb", "handwired/pytest/has_template", "-km", "default", "keyboards/handwired/pytest/has_template/keymaps/default/keymap.c")
  138. check_returncode(result)
  139. 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"]]}'
  140. def test_c2json_nocpp():
  141. result = check_subcommand("c2json", "--no-cpp", "-kb", "handwired/pytest/has_template", "-km", "default", "keyboards/handwired/pytest/has_template/keymaps/nocpp/keymap.c")
  142. check_returncode(result)
  143. 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"]]}'
  144. def test_c2json_stdin():
  145. result = check_subcommand_stdin("keyboards/handwired/pytest/has_template/keymaps/default/keymap.c", "c2json", "-kb", "handwired/pytest/has_template", "-km", "default", "-")
  146. check_returncode(result)
  147. 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"]]}'
  148. def test_c2json_nocpp_stdin():
  149. result = check_subcommand_stdin("keyboards/handwired/pytest/has_template/keymaps/nocpp/keymap.c", "c2json", "--no-cpp", "-kb", "handwired/pytest/has_template", "-km", "default", "-")
  150. check_returncode(result)
  151. 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"]]}'
  152. def test_clean():
  153. result = check_subcommand('clean', '-a')
  154. check_returncode(result)
  155. assert result.stdout.count('done') == 2
  156. def test_generate_rgb_breathe_table():
  157. result = check_subcommand("generate-rgb-breathe-table", "-c", "1.2", "-m", "127")
  158. check_returncode(result)
  159. assert 'Breathing center: 1.2' in result.stdout
  160. assert 'Breathing max: 127' in result.stdout