test_cli_commands.py 6.2 KB

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