From 883b58567c0b0ddc404d6c433115ac050073fffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20Forslund?= Date: Thu, 27 Apr 2017 12:39:12 +0200 Subject: [PATCH 1/3] Ensure that signal directory exists when creating signal. Also add test cases for ipc signals --- mycroft/util/__init__.py | 7 +++---- test/util/test_signal.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/util/test_signal.py diff --git a/mycroft/util/__init__.py b/mycroft/util/__init__.py index 004a1ba4f1..44079073fd 100644 --- a/mycroft/util/__init__.py +++ b/mycroft/util/__init__.py @@ -301,9 +301,9 @@ def create_signal(signal_name): valid in filenames. """ try: - with open(os.path.join(get_ipc_directory(), "signal", signal_name), - 'w'): - return True + path = os.path.join(get_ipc_directory(), "signal", signal_name) + create_file(path) + return os.path.isfile(path) except IOError: return False @@ -321,7 +321,6 @@ def check_for_signal(signal_name, sec_lifetime=0): bool: True if the signal is defined, False otherwise """ path = os.path.join(get_ipc_directory(), "signal", signal_name) - if os.path.isfile(path): if sec_lifetime == 0: # consume this single-use signal diff --git a/test/util/test_signal.py b/test/util/test_signal.py new file mode 100644 index 0000000000..674e537831 --- /dev/null +++ b/test/util/test_signal.py @@ -0,0 +1,30 @@ +from os.path import dirname, join, exists, isfile +import unittest +from shutil import rmtree +from mycroft.util import create_signal, check_for_signal + + +class TestSignals(unittest.TestCase): + def setUp(self): + if exists('/tmp/mycroft'): + rmtree('/tmp/mycroft') + + def test_create_signal(self): + create_signal('test_signal') + self.assertTrue(isfile('/tmp/mycroft/ipc/signal/test_signal')) + + def test_check_signal(self): + if exists('/tmp/mycroft'): + rmtree('/tmp/mycroft') + # check that signal is not found if file does not exist + self.assertFalse(check_for_signal('test_signal')) + + # Check that the signal is found when created + create_signal('test_signal') + self.assertTrue(check_for_signal('test_signal')) + # Check that the signal is removed after use + self.assertFalse(isfile('/tmp/mycroft/ipc/signal/test_signal')) + + +if __name__ == "__main__": + unittest.main() From 2ed9118372d787e53932dc5ecb28d337a49ceb01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20Forslund?= Date: Thu, 27 Apr 2017 18:01:43 +0200 Subject: [PATCH 2/3] Add short docstring to create_file() --- mycroft/util/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mycroft/util/__init__.py b/mycroft/util/__init__.py index 44079073fd..81251246b4 100644 --- a/mycroft/util/__init__.py +++ b/mycroft/util/__init__.py @@ -136,6 +136,11 @@ def read_dict(filename, div='='): def create_file(filename): + """ Create the file filename and create any directories needed + + Args: + filename: Path to the file to be created + """ try: os.makedirs(dirname(filename)) except OSError: From e4f2bb310bae01154e81d6e19395441f8c07b548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20Forslund?= Date: Thu, 27 Apr 2017 18:37:50 +0200 Subject: [PATCH 3/3] Add __init__ to test/util to activate tests in that subdir --- test/util/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/util/__init__.py diff --git a/test/util/__init__.py b/test/util/__init__.py new file mode 100644 index 0000000000..e69de29bb2