Skip to content

Commit 5b7a5b2

Browse files
committed
Setup a python test framework
1 parent 4d339b7 commit 5b7a5b2

File tree

15 files changed

+130
-11
lines changed

15 files changed

+130
-11
lines changed

bin/qmk

+7-1
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,10 @@ else:
9494
exit(1)
9595

9696
if __name__ == '__main__':
97-
milc.cli()
97+
return_code = milc.cli()
98+
if return_code is False:
99+
exit(1)
100+
elif return_code is not True and isinstance(return_code, int) and return_code < 256:
101+
exit(return_code)
102+
else:
103+
exit(0)
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Copyright 2019
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#pragma once
18+
19+
#include "config_common.h"
20+
21+
#define MATRIX_COL_PINS { A3 }
22+
#define MATRIX_ROW_PINS { A2 }
23+
#define UNUSED_PINS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# PyTest onekey
2+
3+
This is used by the python test framework. It's probably not useful otherwise.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# MCU name
2+
MCU = STM32F303
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {__KEYMAP_GOES_HERE__};

lib/python/milc.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,7 @@ def __call__(self):
534534
if not self._inside_context_manager:
535535
# If they didn't use the context manager use it ourselves
536536
with self:
537-
self.__call__()
538-
return
537+
return self.__call__()
539538

540539
if not self._entrypoint:
541540
raise RuntimeError('No entrypoint provided!')

lib/python/qmk/cli/doctor.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
33
Check up for QMK environment.
44
"""
5-
import shutil
6-
import platform
75
import os
6+
import platform
7+
import shutil
8+
import subprocess
9+
from glob import glob
810

911
from milc import cli
1012

@@ -16,32 +18,44 @@ def main(cli):
1618
This is currently very simple, it just checks that all the expected binaries are on your system.
1719
1820
TODO(unclaimed):
19-
* [ ] Run the binaries to make sure they work
2021
* [ ] Compile a trivial program with each compiler
2122
* [ ] Check for udev entries on linux
2223
"""
2324

2425
binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc']
26+
binaries += glob('bin/qmk-*')
2527

26-
cli.log.info('QMK Doctor is Checking your environment')
28+
cli.log.info('QMK Doctor is checking your environment')
2729

2830
ok = True
2931
for binary in binaries:
3032
res = shutil.which(binary)
3133
if res is None:
32-
cli.log.error('{fg_red}QMK can\'t find ' + binary + ' in your path')
34+
cli.log.error("{fg_red}QMK can't find %s in your path", binary)
3335
ok = False
36+
else:
37+
try:
38+
subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, check=True)
39+
except subprocess.CalledProcessError:
40+
cli.log.error("{fg_red}Can't run `%s --version`", binary)
41+
ok = False
3442

3543
OS = platform.system()
3644
if OS == "Darwin":
3745
cli.log.info("Detected {fg_cyan}macOS")
3846
elif OS == "Linux":
3947
cli.log.info("Detected {fg_cyan}linux")
40-
test = 'systemctl list-unit-files | grep enabled | grep -i ModemManager'
41-
if os.system(test) == 0:
42-
cli.log.warn("{bg_yellow}Detected modem manager. Please disable it if you are using Pro Micros")
48+
if shutil.which('systemctl'):
49+
test = 'systemctl list-unit-files | grep enabled | grep -i ModemManager'
50+
if os.system(test) == 0:
51+
cli.log.warn("{bg_yellow}Detected modem manager. Please disable it if you are using Pro Micros")
52+
else:
53+
cli.log.warn("Can't find systemctl to check for ModemManager.")
4354
else:
4455
cli.log.info("Assuming {fg_cyan}Windows")
4556

4657
if ok:
4758
cli.log.info('{fg_green}QMK is ready to go')
59+
else:
60+
cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.')
61+
# FIXME(skullydazed): Link to a document about troubleshooting, or discord or something

lib/python/qmk/cli/nose2.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""QMK Python Unit Tests
2+
3+
QMK script to run unit and integration tests against our python code.
4+
"""
5+
from milc import cli
6+
7+
8+
@cli.entrypoint('QMK Python Unit Tests')
9+
def main(cli):
10+
"""Use nose2 to run unittests
11+
"""
12+
try:
13+
import nose2
14+
except ImportError:
15+
cli.log.error('Could not import nose2! Please install it with {fg_cyan}pip3 install nose2')
16+
return False
17+
18+
nose2.discover()

lib/python/qmk/tests/__init__.py

Whitespace-only changes.

lib/python/qmk/tests/attrdict.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class AttrDict(dict):
2+
"""A dictionary that can be accessed by attributes.
3+
4+
This should only be used to mock objects for unit testing. Please do not use this outside of qmk.tests.
5+
"""
6+
def __init__(self, *args, **kwargs):
7+
super(AttrDict, self).__init__(*args, **kwargs)
8+
self.__dict__ = self
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"keyboard":"handwired/onekey/pytest",
3+
"keymap":"pytest_unittest",
4+
"layout":"LAYOUT",
5+
"layers":[["KC_A"]]
6+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from qmk.errors import NoSuchKeyboardError
2+
3+
def test_NoSuchKeyboardError():
4+
try:
5+
raise(NoSuchKeyboardError("test message"))
6+
except NoSuchKeyboardError as e:
7+
assert e.message == 'test message'
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import qmk.keymap
2+
3+
def test_template_onekey_proton_c():
4+
templ = qmk.keymap.template('handwired/onekey/proton_c')
5+
assert templ == qmk.keymap.DEFAULT_KEYMAP_C
6+
7+
8+
def test_template_onekey_pytest():
9+
templ = qmk.keymap.template('handwired/onekey/pytest')
10+
assert templ == 'const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {__KEYMAP_GOES_HERE__};\n'
11+
12+
13+
def test_generate_onekey_pytest():
14+
templ = qmk.keymap.generate('handwired/onekey/pytest', 'LAYOUT', [['KC_A']])
15+
assert templ == 'const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { [0] = LAYOUT(KC_A)};\n'
16+
17+
18+
# FIXME(skullydazed): Add a test for qmk.keymap.write that mocks up an FD.

lib/python/qmk/tests/test_qmk_path.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
3+
import qmk.path
4+
5+
def test_keymap_onekey_pytest():
6+
path = qmk.path.keymap('handwired/onekey/pytest')
7+
assert path == 'keyboards/handwired/onekey/keymaps'
8+
9+
10+
def test_normpath():
11+
path = qmk.path.normpath('lib/python')
12+
assert path == os.environ['ORIG_CWD'] + '/lib/python'

nose2.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[unittest]
2+
start-dir = lib/python/qmk/tests

0 commit comments

Comments
 (0)