From 6501377070ff20bd061ea85c7ae5517652b6478b Mon Sep 17 00:00:00 2001 From: Erovia Date: Tue, 26 May 2020 17:43:33 +0200 Subject: [PATCH] CLI: fix `json2c` subcommand and add/fix tests (#9206) Co-authored-by: Zach White --- .../onekey/keymaps/default_json/keymap.json | 9 +++++++++ .../handwired/onekey/pytest/templates/keymap.c | 1 + lib/python/qmk/cli/json2c.py | 14 +++++++------- lib/python/qmk/tests/test_cli_commands.py | 6 ++++++ lib/python/qmk/tests/test_qmk_keymap.py | 4 ++-- 5 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 keyboards/handwired/onekey/keymaps/default_json/keymap.json diff --git a/keyboards/handwired/onekey/keymaps/default_json/keymap.json b/keyboards/handwired/onekey/keymaps/default_json/keymap.json new file mode 100644 index 0000000000..cf30f37b79 --- /dev/null +++ b/keyboards/handwired/onekey/keymaps/default_json/keymap.json @@ -0,0 +1,9 @@ +{ + "keyboard":"handwired/onekey/pytest", + "keymap":"default_json", + "layout":"LAYOUT", + "layers":[["KC_A"]], + "author":"qmk", + "notes":"This file is a keymap.json file for handwired/onekey/pytest", + "version":1 +} diff --git a/keyboards/handwired/onekey/pytest/templates/keymap.c b/keyboards/handwired/onekey/pytest/templates/keymap.c index d355210c47..6cb25d5226 100644 --- a/keyboards/handwired/onekey/pytest/templates/keymap.c +++ b/keyboards/handwired/onekey/pytest/templates/keymap.c @@ -1 +1,2 @@ +#include QMK_KEYBOARD_H const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {__KEYMAP_GOES_HERE__}; diff --git a/lib/python/qmk/cli/json2c.py b/lib/python/qmk/cli/json2c.py index 5218405070..af0d80a9ac 100755 --- a/lib/python/qmk/cli/json2c.py +++ b/lib/python/qmk/cli/json2c.py @@ -18,19 +18,19 @@ def json2c(cli): This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided. """ # Error checking - if not cli.args.filename.exists(): - cli.log.error('JSON file does not exist!') - cli.print_usage() - exit(1) - - if cli.args.filename.name == '-': + if cli.args.filename and cli.args.filename.name == '-': # TODO(skullydazed/anyone): Read file contents from STDIN cli.log.error('Reading from STDIN is not (yet) supported.') cli.print_usage() exit(1) + if not cli.args.filename.exists(): + cli.log.error('JSON file does not exist!') + cli.print_usage() + exit(1) + # Environment processing - if cli.args.output.name == ('-'): + if cli.args.output and cli.args.output.name == '-': cli.args.output = None # Parse the configurator json diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index a93587150c..768929de1d 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -84,3 +84,9 @@ def test_list_keymaps_no_keyboard_found(): result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl') assert result.returncode == 0 assert 'does not exist' in result.stdout + + +def test_json2c(): + result = check_subcommand('json2c', 'keyboards/handwired/onekey/keymaps/default_json/keymap.json') + assert result.returncode == 0 + assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT(KC_A)};\n\n' diff --git a/lib/python/qmk/tests/test_qmk_keymap.py b/lib/python/qmk/tests/test_qmk_keymap.py index 2db625600e..d8669e5498 100644 --- a/lib/python/qmk/tests/test_qmk_keymap.py +++ b/lib/python/qmk/tests/test_qmk_keymap.py @@ -8,12 +8,12 @@ def test_template_onekey_proton_c(): def test_template_onekey_pytest(): templ = qmk.keymap.template('handwired/onekey/pytest') - assert templ == 'const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {__KEYMAP_GOES_HERE__};\n' + assert templ == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {__KEYMAP_GOES_HERE__};\n' def test_generate_onekey_pytest(): templ = qmk.keymap.generate('handwired/onekey/pytest', 'LAYOUT', [['KC_A']]) - assert templ == 'const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { [0] = LAYOUT(KC_A)};\n' + assert templ == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT(KC_A)};\n' # FIXME(skullydazed): Add a test for qmk.keymap.write that mocks up an FD.