test_cli_commands.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. from subprocess import STDOUT, PIPE
  2. from qmk.commands import run
  3. def check_subcommand(command, *args):
  4. cmd = ['bin/qmk', command] + list(args)
  5. result = run(cmd, stdout=PIPE, stderr=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_compile_json():
  22. result = check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default_json')
  23. check_returncode(result)
  24. def test_flash():
  25. result = check_subcommand('flash', '-kb', 'handwired/onekey/pytest', '-km', 'default', '-n')
  26. check_returncode(result)
  27. def test_flash_bootloaders():
  28. result = check_subcommand('flash', '-b')
  29. check_returncode(result, 1)
  30. def test_config():
  31. result = check_subcommand('config')
  32. check_returncode(result)
  33. assert 'general.color' in result.stdout
  34. def test_kle2json():
  35. result = check_subcommand('kle2json', 'lib/python/qmk/tests/kle.txt', '-f')
  36. check_returncode(result)
  37. assert 'Wrote out' in result.stdout
  38. def test_doctor():
  39. result = check_subcommand('doctor', '-n')
  40. check_returncode(result)
  41. assert 'QMK Doctor is checking your environment.' in result.stdout
  42. assert 'QMK is ready to go' in result.stdout
  43. def test_hello():
  44. result = check_subcommand('hello')
  45. check_returncode(result)
  46. assert 'Hello,' in result.stdout
  47. def test_pyformat():
  48. result = check_subcommand('pyformat')
  49. check_returncode(result)
  50. assert 'Successfully formatted the python code' in result.stdout
  51. def test_list_keyboards():
  52. result = check_subcommand('list-keyboards')
  53. check_returncode(result)
  54. # check to see if a known keyboard is returned
  55. # this will fail if handwired/onekey/pytest is removed
  56. assert 'handwired/onekey/pytest' in result.stdout
  57. def test_list_keymaps():
  58. result = check_subcommand('list-keymaps', '-kb', 'handwired/onekey/pytest')
  59. check_returncode(result, 0)
  60. assert 'default' and 'test' in result.stdout
  61. def test_list_keymaps_long():
  62. result = check_subcommand('list-keymaps', '--keyboard', 'handwired/onekey/pytest')
  63. check_returncode(result, 0)
  64. assert 'default' and 'test' in result.stdout
  65. def test_list_keymaps_kb_only():
  66. result = check_subcommand('list-keymaps', '-kb', 'niu_mini')
  67. check_returncode(result, 0)
  68. assert 'default' and 'via' in result.stdout
  69. def test_list_keymaps_vendor_kb():
  70. result = check_subcommand('list-keymaps', '-kb', 'ai03/lunar')
  71. check_returncode(result, 0)
  72. assert 'default' and 'via' in result.stdout
  73. def test_list_keymaps_vendor_kb_rev():
  74. result = check_subcommand('list-keymaps', '-kb', 'kbdfans/kbd67/mkiirgb/v2')
  75. check_returncode(result, 0)
  76. assert 'default' and 'via' in result.stdout
  77. def test_list_keymaps_no_keyboard_found():
  78. result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl')
  79. check_returncode(result, 1)
  80. assert 'does not exist' in result.stdout
  81. def test_json2c():
  82. result = check_subcommand('json2c', 'keyboards/handwired/onekey/keymaps/default_json/keymap.json')
  83. check_returncode(result, 0)
  84. 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'
  85. def test_info():
  86. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest')
  87. check_returncode(result)
  88. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  89. assert 'Processor: STM32F303' in result.stdout
  90. assert 'Layout:' not in result.stdout
  91. assert 'k0' not in result.stdout
  92. def test_info_keyboard_render():
  93. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-l')
  94. check_returncode(result)
  95. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  96. assert 'Processor: STM32F303' in result.stdout
  97. assert 'Layouts:' in result.stdout
  98. assert 'k0' in result.stdout
  99. def test_info_keymap_render():
  100. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-km', 'default_json')
  101. check_returncode(result)
  102. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  103. assert 'Processor: STM32F303' in result.stdout
  104. assert '│A │' in result.stdout
  105. def test_info_matrix_render():
  106. result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-m')
  107. check_returncode(result)
  108. assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
  109. assert 'Processor: STM32F303' in result.stdout
  110. assert 'LAYOUT_ortho_1x1' in result.stdout
  111. assert '│0A│' in result.stdout
  112. assert 'Matrix for "LAYOUT_ortho_1x1"' in result.stdout
  113. def test_c2json():
  114. result = check_subcommand("c2json", "-kb", "handwired/onekey/pytest", "-km", "default", "keyboards/handwired/onekey/keymaps/default/keymap.c")
  115. check_returncode(result)
  116. assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
  117. def test_c2json_nocpp():
  118. result = check_subcommand("c2json", "--no-cpp", "-kb", "handwired/onekey/pytest", "-km", "default", "keyboards/handwired/onekey/keymaps/pytest_nocpp/keymap.c")
  119. check_returncode(result)
  120. assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'