test_cli_commands.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import subprocess
  2. from qmk.commands import run
  3. def check_subcommand(command, *args):
  4. cmd = ['bin/qmk', command] + list(args)
  5. result = run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
  6. return result
  7. def check_returncode(result, expected=0):
  8. """Print stdout if `result.returncode` does not match `expected`.
  9. """
  10. if result.returncode != expected:
  11. print('`%s` stdout:' % ' '.join(result.args))
  12. print(result.stdout)
  13. print('returncode:', result.returncode)
  14. assert result.returncode == expected
  15. def test_cformat():
  16. result = check_subcommand('cformat', 'quantum/matrix.c')
  17. check_returncode(result)
  18. def test_compile():
  19. result = check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default', '-n')
  20. check_returncode(result)
  21. def test_flash():
  22. result = check_subcommand('flash', '-kb', 'handwired/onekey/pytest', '-km', 'default', '-n')
  23. check_returncode(result)
  24. def test_flash_bootloaders():
  25. result = check_subcommand('flash', '-b')
  26. check_returncode(result, 1)
  27. def test_config():
  28. result = check_subcommand('config')
  29. check_returncode(result)
  30. assert 'general.color' in result.stdout
  31. def test_kle2json():
  32. result = check_subcommand('kle2json', 'lib/python/qmk/tests/kle.txt', '-f')
  33. check_returncode(result)
  34. assert 'Wrote out' in result.stdout
  35. def test_doctor():
  36. result = check_subcommand('doctor', '-n')
  37. check_returncode(result)
  38. assert 'QMK Doctor is checking your environment.' in result.stdout
  39. assert 'QMK is ready to go' in result.stdout
  40. def test_hello():
  41. result = check_subcommand('hello')
  42. check_returncode(result)
  43. assert 'Hello,' in result.stdout
  44. def test_pyformat():
  45. result = check_subcommand('pyformat')
  46. check_returncode(result)
  47. assert 'Successfully formatted the python code' in result.stdout
  48. def test_list_keyboards():
  49. result = check_subcommand('list-keyboards')
  50. check_returncode(result)
  51. # check to see if a known keyboard is returned
  52. # this will fail if handwired/onekey/pytest is removed
  53. assert 'handwired/onekey/pytest' in result.stdout
  54. def test_list_keymaps():
  55. result = check_subcommand('list-keymaps', '-kb', 'handwired/onekey/pytest')
  56. check_returncode(result, 0)
  57. assert 'default' and 'test' in result.stdout
  58. def test_list_keymaps_long():
  59. result = check_subcommand('list-keymaps', '--keyboard', 'handwired/onekey/pytest')
  60. check_returncode(result, 0)
  61. assert 'default' and 'test' in result.stdout
  62. def test_list_keymaps_kb_only():
  63. result = check_subcommand('list-keymaps', '-kb', 'niu_mini')
  64. check_returncode(result, 0)
  65. assert 'default' and 'via' in result.stdout
  66. def test_list_keymaps_vendor_kb():
  67. result = check_subcommand('list-keymaps', '-kb', 'ai03/lunar')
  68. check_returncode(result, 0)
  69. assert 'default' and 'via' in result.stdout
  70. def test_list_keymaps_vendor_kb_rev():
  71. result = check_subcommand('list-keymaps', '-kb', 'kbdfans/kbd67/mkiirgb/v2')
  72. check_returncode(result, 0)
  73. assert 'default' and 'via' in result.stdout
  74. def test_list_keymaps_no_keyboard_found():
  75. result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl')
  76. check_returncode(result, 1)
  77. assert 'does not exist' in result.stdout
  78. def test_json2c():
  79. result = check_subcommand('json2c', 'keyboards/handwired/onekey/keymaps/default_json/keymap.json')
  80. check_returncode(result, 0)
  81. 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'
  82. def test_info():
  83. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest')
  84. check_returncode(result)
  85. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  86. assert 'Processor: STM32F303' in result.stdout
  87. assert 'Layout:' not in result.stdout
  88. assert 'k0' not in result.stdout
  89. def test_info_keyboard_render():
  90. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-l')
  91. check_returncode(result)
  92. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  93. assert 'Processor: STM32F303' in result.stdout
  94. assert 'Layouts:' in result.stdout
  95. assert 'k0' in result.stdout
  96. def test_info_keymap_render():
  97. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-km', 'default_json')
  98. check_returncode(result)
  99. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  100. assert 'Processor: STM32F303' in result.stdout
  101. assert '│A │' in result.stdout
  102. def test_info_matrix_render():
  103. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-m')
  104. check_returncode(result)
  105. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  106. assert 'Processor: STM32F303' in result.stdout
  107. assert 'LAYOUT_ortho_1x1' in result.stdout
  108. assert '│0A│' in result.stdout
  109. assert 'Matrix for "LAYOUT_ortho_1x1"' in result.stdout