test_cli_commands.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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', 'kle.txt', '-f')
  33. check_returncode(result)
  34. def test_doctor():
  35. result = check_subcommand('doctor', '-n')
  36. check_returncode(result)
  37. assert 'QMK Doctor is checking your environment.' in result.stdout
  38. assert 'QMK is ready to go' in result.stdout
  39. def test_hello():
  40. result = check_subcommand('hello')
  41. check_returncode(result)
  42. assert 'Hello,' in result.stdout
  43. def test_pyformat():
  44. result = check_subcommand('pyformat')
  45. check_returncode(result)
  46. assert 'Successfully formatted the python code' in result.stdout
  47. def test_list_keyboards():
  48. result = check_subcommand('list-keyboards')
  49. check_returncode(result)
  50. # check to see if a known keyboard is returned
  51. # this will fail if handwired/onekey/pytest is removed
  52. assert 'handwired/onekey/pytest' in result.stdout
  53. def test_list_keymaps():
  54. result = check_subcommand('list-keymaps', '-kb', 'handwired/onekey/pytest')
  55. check_returncode(result, 0)
  56. assert 'default' and 'test' in result.stdout
  57. def test_list_keymaps_long():
  58. result = check_subcommand('list-keymaps', '--keyboard', 'handwired/onekey/pytest')
  59. check_returncode(result, 0)
  60. assert 'default' and 'test' in result.stdout
  61. def test_list_keymaps_kb_only():
  62. result = check_subcommand('list-keymaps', '-kb', 'niu_mini')
  63. check_returncode(result, 0)
  64. assert 'default' and 'via' in result.stdout
  65. def test_list_keymaps_vendor_kb():
  66. result = check_subcommand('list-keymaps', '-kb', 'ai03/lunar')
  67. check_returncode(result, 0)
  68. assert 'default' and 'via' in result.stdout
  69. def test_list_keymaps_vendor_kb_rev():
  70. result = check_subcommand('list-keymaps', '-kb', 'kbdfans/kbd67/mkiirgb/v2')
  71. check_returncode(result, 0)
  72. assert 'default' and 'via' in result.stdout
  73. def test_list_keymaps_no_keyboard_found():
  74. result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl')
  75. check_returncode(result, 1)
  76. assert 'does not exist' in result.stdout
  77. def test_json2c():
  78. result = check_subcommand('json2c', 'keyboards/handwired/onekey/keymaps/default_json/keymap.json')
  79. check_returncode(result, 0)
  80. assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT(KC_A)};\n\n'
  81. def test_info():
  82. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest')
  83. check_returncode(result)
  84. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  85. assert 'Processor: STM32F303' in result.stdout
  86. assert 'Layout:' not in result.stdout
  87. assert 'k0' not in result.stdout
  88. def test_info_keyboard_render():
  89. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-l')
  90. check_returncode(result)
  91. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  92. assert 'Processor: STM32F303' in result.stdout
  93. assert 'Layout:' in result.stdout
  94. assert 'k0' in result.stdout
  95. def test_info_keymap_render():
  96. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-km', 'default_json')
  97. check_returncode(result)
  98. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  99. assert 'Processor: STM32F303' in result.stdout
  100. assert '│A │' in result.stdout
  101. def test_info_matrix_render():
  102. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-m')
  103. check_returncode(result)
  104. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  105. assert 'Processor: STM32F303' in result.stdout
  106. assert 'LAYOUT' in result.stdout
  107. assert '│0A│' in result.stdout
  108. assert 'Matrix for "LAYOUT"' in result.stdout