Skip to content

Commit

Permalink
#197 Services started via sudo know the correct config path
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Oct 22, 2021
1 parent 92bb4b1 commit f9e7ef1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
6 changes: 5 additions & 1 deletion keymapper/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from keymapper.config import config
from keymapper.system_mapping import system_mapping
from keymapper.groups import groups
from keymapper.paths import get_config_path
from keymapper.paths import get_config_path, USER


BUS_NAME = "keymapper.Control"
Expand Down Expand Up @@ -147,8 +147,12 @@ def __init__(self):
"""Constructs the daemon."""
logger.debug("Creating daemon")
self.injectors = {}

self.config_dir = None

if USER != 'root':
self.set_config_dir(get_config_path())

self.autoload_history = AutoloadHistory()
self.refreshed_devices_at = 0

Expand Down
30 changes: 11 additions & 19 deletions tests/testcases/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def test_daemon(self):
push_events(group.key, [new_event(EV_KEY, 13, 1)])

self.daemon = Daemon()
self.daemon.set_config_dir(get_config_path())

self.assertFalse(uinput_write_history_pipe[0].poll())
self.daemon.start_injecting(group.key, preset)
Expand Down Expand Up @@ -222,6 +221,17 @@ def test_daemon(self):
self.assertEqual(event.code, keycode_to_2)
self.assertEqual(event.value, 1)

def test_config_dir(self):
config.set('foo', 'bar')
self.assertEqual(config.get('foo'), 'bar')

# freshly loads the config and therefore removes the previosly added key.
# This is important so that if the service is started via sudo or pkexec
# it knows where to look for configuration files.
self.daemon = Daemon()
self.assertEqual(self.daemon.config_dir, get_config_path())
self.assertIsNone(config.get('foo'))

def test_refresh_on_start(self):
if os.path.exists(get_config_path("xmodmap.json")):
os.remove(get_config_path("xmodmap.json"))
Expand Down Expand Up @@ -262,7 +272,6 @@ def test_refresh_on_start(self):
"name": group_name,
}

self.daemon.set_config_dir(get_config_path())
self.daemon.start_injecting(group_key, preset)

# test if the injector called groups.refresh successfully
Expand Down Expand Up @@ -359,15 +368,7 @@ def test_start_stop(self):
mapping.change(Key(3, 2, 1), "a")
mapping.save(group.get_preset_path(preset))

# the daemon needs set_config_dir first before doing anything
daemon.start_injecting(group.key, preset)
self.assertNotIn(group.key, daemon.autoload_history._autoload_history)
self.assertNotIn(group.key, daemon.injectors)
self.assertTrue(daemon.autoload_history.may_autoload(group.key, preset))

# start
config.save_config()
daemon.set_config_dir(get_config_path())
daemon.start_injecting(group.key, preset)
# explicit start, not autoload, so the history stays empty
self.assertNotIn(group.key, daemon.autoload_history._autoload_history)
Expand Down Expand Up @@ -415,7 +416,6 @@ def test_autoload(self):

daemon = Daemon()
self.daemon = daemon
self.daemon.set_config_dir(get_config_path())

mapping = Mapping()
mapping.change(Key(3, 2, 1), "a")
Expand All @@ -428,7 +428,6 @@ def test_autoload(self):

config.set_autoload_preset(group.key, preset)
config.save_config()
self.daemon.set_config_dir(get_config_path())
len_before = len(self.daemon.autoload_history._autoload_history)
# now autoloading is configured, so it will autoload
self.daemon._autoload(group.key)
Expand Down Expand Up @@ -479,12 +478,6 @@ def test_autoload_2(self):
# ignored, won't cause problems:
config.set_autoload_preset("non-existant-key", "foo")

# daemon is missing the config directory yet
self.daemon.autoload()
self.assertEqual(len(history), 0)

config.save_config()
self.daemon.set_config_dir(get_config_path())
self.daemon.autoload()
self.assertEqual(len(history), 1)
self.assertEqual(history[group.key][1], preset)
Expand All @@ -502,7 +495,6 @@ def test_autoload_3(self):
config.save_config()

self.daemon = Daemon()
self.daemon.set_config_dir(get_config_path())
groups.set_groups([]) # caused the bug
self.assertIsNone(groups.find(key="Foo Device 2"))
self.daemon.autoload()
Expand Down

0 comments on commit f9e7ef1

Please sign in to comment.