input-remapper/tests/unit/test_migrations.py

544 lines
18 KiB
Python
Raw Normal View History

#
2022-01-01 12:00:49 +00:00
# input-remapper is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2022-01-01 12:00:49 +00:00
# input-remapper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2022-01-01 12:00:49 +00:00
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
2022-11-23 21:55:28 +00:00
2022-12-15 13:43:03 +00:00
2022-11-23 21:55:28 +00:00
from tests.lib.cleanup import quick_cleanup
from tests.lib.tmp import tmp
2022-12-15 13:43:03 +00:00
from tests.lib.fixtures import get_combination_config
import os
import unittest
import shutil
import json
2022-05-08 09:03:04 +00:00
import pkg_resources
2022-04-17 10:19:23 +00:00
from evdev.ecodes import (
EV_KEY,
EV_ABS,
ABS_HAT0X,
ABS_X,
ABS_Y,
ABS_RX,
ABS_RY,
EV_REL,
REL_X,
REL_Y,
REL_WHEEL_HI_RES,
REL_HWHEEL_HI_RES,
)
2022-12-15 13:43:03 +00:00
from inputremapper.configs.mapping import UIMapping
2022-01-31 19:58:37 +00:00
from inputremapper.configs.migrations import migrate, config_version
from inputremapper.configs.preset import Preset
from inputremapper.configs.global_config import global_config
from inputremapper.configs.paths import touch, CONFIG_PATH, mkdir, get_preset_path
2022-04-17 10:19:23 +00:00
from inputremapper.logger import IS_BETA
2022-12-15 13:43:03 +00:00
from inputremapper.configs.input_config import InputCombination, InputConfig
2022-01-01 12:00:49 +00:00
from inputremapper.user import HOME
2022-01-01 12:00:49 +00:00
from inputremapper.logger import VERSION
class TestMigrations(unittest.TestCase):
def tearDown(self):
quick_cleanup()
2022-01-31 19:58:37 +00:00
self.assertEqual(len(global_config.iterate_autoload_presets()), 0)
def test_migrate_suffix(self):
old = os.path.join(CONFIG_PATH, "config")
new = os.path.join(CONFIG_PATH, "config.json")
2022-01-01 12:00:49 +00:00
try:
os.remove(new)
except FileNotFoundError:
pass
2022-01-01 12:00:49 +00:00
touch(old)
with open(old, "w") as f:
f.write("{}")
2022-01-01 12:00:49 +00:00
migrate()
self.assertTrue(os.path.exists(new))
self.assertFalse(os.path.exists(old))
def test_rename_config(self):
old = os.path.join(HOME, ".config", "key-mapper")
2022-04-17 10:19:23 +00:00
if IS_BETA:
new = os.path.join(*os.path.split(CONFIG_PATH)[:-1])
else:
new = CONFIG_PATH
2022-01-01 12:00:49 +00:00
# we are not destroying our actual config files with this test
2022-05-08 09:03:04 +00:00
self.assertTrue(new.startswith(tmp), f'Expected "{new}" to start with "{tmp}"')
2022-01-01 12:00:49 +00:00
try:
shutil.rmtree(new)
2022-01-01 12:00:49 +00:00
except FileNotFoundError:
pass
old_config_json = os.path.join(old, "config.json")
touch(old_config_json)
with open(old_config_json, "w") as f:
f.write('{"foo":"bar"}')
migrate()
2022-01-01 12:00:49 +00:00
self.assertTrue(os.path.exists(new))
self.assertFalse(os.path.exists(old))
2022-01-01 12:00:49 +00:00
new_config_json = os.path.join(new, "config.json")
with open(new_config_json, "r") as f:
moved_config = json.loads(f.read())
self.assertEqual(moved_config["foo"], "bar")
def test_wont_migrate_suffix(self):
old = os.path.join(CONFIG_PATH, "config")
new = os.path.join(CONFIG_PATH, "config.json")
touch(new)
with open(new, "w") as f:
f.write("{}")
touch(old)
with open(old, "w") as f:
f.write("{}")
migrate()
self.assertTrue(os.path.exists(new))
self.assertTrue(os.path.exists(old))
def test_migrate_preset(self):
2022-04-17 10:19:23 +00:00
if os.path.exists(CONFIG_PATH):
shutil.rmtree(CONFIG_PATH)
2022-04-17 10:19:23 +00:00
p1 = os.path.join(CONFIG_PATH, "foo1", "bar1.json")
p2 = os.path.join(CONFIG_PATH, "foo2", "bar2.json")
touch(p1)
touch(p2)
with open(p1, "w") as f:
f.write("{}")
with open(p2, "w") as f:
f.write("{}")
migrate()
2022-04-17 10:19:23 +00:00
self.assertFalse(os.path.exists(os.path.join(CONFIG_PATH, "foo1", "bar1.json")))
self.assertFalse(os.path.exists(os.path.join(CONFIG_PATH, "foo2", "bar2.json")))
self.assertTrue(
os.path.exists(os.path.join(CONFIG_PATH, "presets", "foo1", "bar1.json")),
)
self.assertTrue(
os.path.exists(os.path.join(CONFIG_PATH, "presets", "foo2", "bar2.json")),
)
def test_wont_migrate_preset(self):
2022-04-17 10:19:23 +00:00
if os.path.exists(CONFIG_PATH):
shutil.rmtree(CONFIG_PATH)
2022-04-17 10:19:23 +00:00
p1 = os.path.join(CONFIG_PATH, "foo1", "bar1.json")
p2 = os.path.join(CONFIG_PATH, "foo2", "bar2.json")
touch(p1)
touch(p2)
with open(p1, "w") as f:
f.write("{}")
with open(p2, "w") as f:
f.write("{}")
# already migrated
2022-04-17 10:19:23 +00:00
mkdir(os.path.join(CONFIG_PATH, "presets"))
migrate()
2022-04-17 10:19:23 +00:00
self.assertTrue(os.path.exists(os.path.join(CONFIG_PATH, "foo1", "bar1.json")))
self.assertTrue(os.path.exists(os.path.join(CONFIG_PATH, "foo2", "bar2.json")))
self.assertFalse(
os.path.exists(os.path.join(CONFIG_PATH, "presets", "foo1", "bar1.json")),
)
self.assertFalse(
os.path.exists(os.path.join(CONFIG_PATH, "presets", "foo2", "bar2.json")),
)
2022-01-14 17:50:57 +00:00
def test_migrate_mappings(self):
"""Test if mappings are migrated correctly
2022-01-14 17:50:57 +00:00
mappings like
2022-04-17 10:19:23 +00:00
{(type, code): symbol} or {(type, code, value): symbol} should migrate
2022-12-15 13:43:03 +00:00
to {InputCombination: {target: target, symbol: symbol, ...}}
2022-01-14 17:50:57 +00:00
"""
2022-04-17 10:19:23 +00:00
path = os.path.join(CONFIG_PATH, "presets", "Foo Device", "test.json")
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as file:
json.dump(
{
"mapping": {
2022-01-14 17:50:57 +00:00
f"{EV_KEY},1": "a",
f"{EV_KEY}, 2, 1": "BTN_B", # can be mapped to "gamepad"
f"{EV_KEY}, 3, 1": "BTN_1", # can not be mapped
f"{EV_ABS},{ABS_HAT0X},-1": "b",
f"{EV_ABS},1,1+{EV_ABS},2,-1+{EV_ABS},3,1": "c",
2022-04-17 10:19:23 +00:00
f"{EV_KEY}, 4, 1": ("d", "keyboard"),
f"{EV_KEY}, 5, 1": ("e", "foo"), # unknown target
f"{EV_KEY}, 6, 1": ("key(a, b)", "keyboard"), # broken macro
# ignored because broken
f"3,1,1,2": "e",
f"3": "e",
f",,+3,1,2": "g",
f"": "h",
}
},
file,
)
migrate()
2022-04-17 10:19:23 +00:00
# use UIMapping to also load invalid mappings
preset = Preset(get_preset_path("Foo Device", "test"), UIMapping)
preset.load()
2022-01-14 17:50:57 +00:00
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=1))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=1)),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="a",
),
2022-01-31 19:58:37 +00:00
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=2))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=2)),
2022-04-17 10:19:23 +00:00
target_uinput="gamepad",
output_symbol="BTN_B",
),
2022-01-31 19:58:37 +00:00
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=3))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=3)),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="BTN_1\n# Broken mapping:\n# No target can handle all specified keycodes",
2022-01-14 17:50:57 +00:00
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=4))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=4)),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="d",
),
2022-01-14 17:50:57 +00:00
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(
InputCombination(
InputConfig(type=EV_ABS, code=ABS_HAT0X, analog_threshold=-1)
)
),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_HAT0X, analog_threshold=-1)
),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="b",
),
2022-01-31 19:58:37 +00:00
)
self.assertEqual(
2022-04-17 10:19:23 +00:00
preset.get_mapping(
2022-12-15 13:43:03 +00:00
InputCombination(
get_combination_config(
(EV_ABS, 1, 1), (EV_ABS, 2, -1), (EV_ABS, 3, 1)
)
),
2022-04-17 10:19:23 +00:00
),
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
get_combination_config(
(EV_ABS, 1, 1), (EV_ABS, 2, -1), (EV_ABS, 3, 1)
),
2022-04-17 10:19:23 +00:00
),
target_uinput="keyboard",
output_symbol="c",
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=5))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=5)),
2022-04-17 10:19:23 +00:00
target_uinput="foo",
output_symbol="e",
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=6))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=6)),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="key(a, b)",
2022-01-31 19:58:37 +00:00
),
)
2022-04-17 10:19:23 +00:00
self.assertEqual(8, len(preset))
def test_migrate_otherwise(self):
2022-04-17 10:19:23 +00:00
path = os.path.join(CONFIG_PATH, "presets", "Foo Device", "test.json")
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as file:
json.dump(
{
"mapping": {
f"{EV_KEY},1,1": ("otherwise + otherwise", "keyboard"),
f"{EV_KEY},2,1": ("bar($otherwise)", "keyboard"),
f"{EV_KEY},3,1": ("foo(otherwise=qux)", "keyboard"),
f"{EV_KEY},4,1": ("qux(otherwise).bar(otherwise = 1)", "foo"),
f"{EV_KEY},5,1": ("foo(otherwise1=2qux)", "keyboard"),
}
},
file,
)
migrate()
2022-04-17 10:19:23 +00:00
preset = Preset(get_preset_path("Foo Device", "test"), UIMapping)
preset.load()
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=1))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=1)),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="otherwise + otherwise",
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=2))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=2)),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="bar($otherwise)",
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=3))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=3)),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="foo(else=qux)",
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=4))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=4)),
2022-04-17 10:19:23 +00:00
target_uinput="foo",
output_symbol="qux(otherwise).bar(else=1)",
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_KEY, code=5))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(InputConfig(type=EV_KEY, code=5)),
2022-04-17 10:19:23 +00:00
target_uinput="keyboard",
output_symbol="foo(otherwise1=2qux)",
),
)
def test_add_version(self):
path = os.path.join(CONFIG_PATH, "config.json")
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as file:
file.write("{}")
migrate()
2022-05-08 09:03:04 +00:00
self.assertEqual(pkg_resources.parse_version(VERSION), config_version())
def test_update_version(self):
path = os.path.join(CONFIG_PATH, "config.json")
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as file:
json.dump({"version": "0.1.0"}, file)
migrate()
2022-05-08 09:03:04 +00:00
self.assertEqual(pkg_resources.parse_version(VERSION), config_version())
def test_config_version(self):
path = os.path.join(CONFIG_PATH, "config.json")
with open(path, "w") as file:
file.write("{}")
self.assertEqual("0.0.0", config_version().public)
try:
os.remove(path)
except FileNotFoundError:
pass
self.assertEqual("0.0.0", config_version().public)
2022-04-17 10:19:23 +00:00
def test_migrate_left_and_right_purpose(self):
path = os.path.join(CONFIG_PATH, "presets", "Foo Device", "test.json")
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as file:
json.dump(
{
"gamepad": {
"joystick": {
"left_purpose": "mouse",
"right_purpose": "wheel",
"pointer_speed": 50,
"x_scroll_speed": 10,
"y_scroll_speed": 20,
}
}
},
file,
)
migrate()
preset = Preset(get_preset_path("Foo Device", "test"), UIMapping)
preset.load()
# 2 mappings for mouse
# 2 mappings for wheel
self.assertEqual(len(preset), 4)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_ABS, code=ABS_X))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_X)
),
2022-04-17 10:19:23 +00:00
target_uinput="mouse",
output_type=EV_REL,
output_code=REL_X,
gain=50 / 100,
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_ABS, code=ABS_Y))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_Y)
),
2022-04-17 10:19:23 +00:00
target_uinput="mouse",
output_type=EV_REL,
output_code=REL_Y,
gain=50 / 100,
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_ABS, code=ABS_RX))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_RX)
),
2022-04-17 10:19:23 +00:00
target_uinput="mouse",
output_type=EV_REL,
output_code=REL_HWHEEL_HI_RES,
gain=10,
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_ABS, code=ABS_RY))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_RY)
),
2022-04-17 10:19:23 +00:00
target_uinput="mouse",
output_type=EV_REL,
output_code=REL_WHEEL_HI_RES,
gain=20,
),
)
def test_migrate_left_and_right_purpose2(self):
# same as above, but left and right is swapped
path = os.path.join(CONFIG_PATH, "presets", "Foo Device", "test.json")
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as file:
json.dump(
{
"gamepad": {
"joystick": {
"right_purpose": "mouse",
"left_purpose": "wheel",
"pointer_speed": 50,
"x_scroll_speed": 10,
"y_scroll_speed": 20,
}
}
},
file,
)
migrate()
preset = Preset(get_preset_path("Foo Device", "test"), UIMapping)
preset.load()
# 2 mappings for mouse
# 2 mappings for wheel
self.assertEqual(len(preset), 4)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_ABS, code=ABS_RX))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_RX)
),
2022-04-17 10:19:23 +00:00
target_uinput="mouse",
output_type=EV_REL,
output_code=REL_X,
gain=50 / 100,
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_ABS, code=ABS_RY))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_RY)
),
2022-04-17 10:19:23 +00:00
target_uinput="mouse",
output_type=EV_REL,
output_code=REL_Y,
gain=50 / 100,
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_ABS, code=ABS_X))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_X)
),
2022-04-17 10:19:23 +00:00
target_uinput="mouse",
output_type=EV_REL,
output_code=REL_HWHEEL_HI_RES,
gain=10,
),
)
self.assertEqual(
2022-12-15 13:43:03 +00:00
preset.get_mapping(InputCombination(InputConfig(type=EV_ABS, code=ABS_Y))),
2022-04-17 10:19:23 +00:00
UIMapping(
2022-12-15 13:43:03 +00:00
input_combination=InputCombination(
InputConfig(type=EV_ABS, code=ABS_Y)
),
2022-04-17 10:19:23 +00:00
target_uinput="mouse",
output_type=EV_REL,
output_code=REL_WHEEL_HI_RES,
gain=20,
),
)
if __name__ == "__main__":
unittest.main()