Skip to content

Commit

Permalink
Add test for builtins and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dagwieers committed Oct 29, 2019
1 parent de1b970 commit 310a81b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
coverage:
range: 50..100
range: 0..100
round: nearest
status:
project:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ install:
script:
- tox
- tox -e flake8
- pylint screensaver.py
- pylint screensaver.py test/
#- msgcmp resources/language/resource.language.{nl_nl,en_gb}/strings.po
#- kodi-addon-checker . --branch=krypton
#- kodi-addon-checker . --branch=leia
Expand Down
22 changes: 11 additions & 11 deletions screensaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@
dict(name='do-nothing', title='Do nothing',
function='log', args=[1, 'Do nothing to power off system']),
dict(name='suspend-builtin', title='Suspend (built-in)',
function='jsonrpc', kwargs=dict(method='System.Suspend')),
function='jsonrpc', kwargs_off=dict(method='System.Suspend')),
dict(name='hibernate-builtin', title='Hibernate (built-in)',
function='jsonrpc', kwargs=dict(method='System.Hibernate')),
function='jsonrpc', kwargs_off=dict(method='System.Hibernate')),
dict(name='quit-builtin', title='Quit (built-in)',
function='jsonrpc', kwargs=dict(method='Application.Quit')),
function='jsonrpc', kwargs_off=dict(method='Application.Quit')),
dict(name='shutdown-builtin', title='ShutDown action (built-in)',
function='jsonrpc', kwargs=dict(method='System.Shutdown')),
function='jsonrpc', kwargs_off=dict(method='System.Shutdown')),
dict(name='reboot-builtin', title='Reboot (built-in)',
function='jsonrpc', kwargs=dict(method='System.Reboot')),
function='jsonrpc', kwargs_off=dict(method='System.Reboot')),
dict(name='powerdown-builtin', title='Powerdown (built-in)',
function='jsonrpc', kwargs=dict(method='System.Powerdown')),
function='jsonrpc', kwargs_off=dict(method='System.Powerdown')),
]


Expand Down Expand Up @@ -168,11 +168,11 @@ def run_builtin(builtin):
popup(msg="Exception executing builtin '%s': %s" % (builtin, exc))


def run_command(command, shell=False):
def run_command(*command, **kwargs):
''' Run commands on the OS while catching exceptions '''
# TODO: Add options for running using su or sudo
try:
cmd = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
cmd = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwargs)
(out, err) = cmd.communicate()
if cmd.returncode == 0:
log(2, msg="Running command '{command}' returned rc={rc}", command=' '.join(command), rc=cmd.returncode)
Expand All @@ -190,9 +190,9 @@ def run_command(command, shell=False):
sys.exit(2)


def func(function, *args):
def func(function, *args, **kwargs):
''' Execute a global function with arguments '''
return globals()[function](*args)
return globals()[function](*args, **kwargs)


class TurnOffMonitor(Monitor, object):
Expand Down Expand Up @@ -260,7 +260,7 @@ def onInit(self): # pylint: disable=invalid-name
# Power off system
if self.power.get('name') != 'do-nothing':
log(1, msg="Turn system off using method '{power_method}'", power_method=self.power.get('name'))
func(self.power.get('function'), *self.power.get('args', []), **self.power.get('kwargs', {}))
func(self.power.get('function'), **self.power.get('kwargs_off', {}))

def resume(self):
''' Perform this when the Screensaver is stopped '''
Expand Down
35 changes: 31 additions & 4 deletions test/test_screensaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,40 @@


class TestScreensaver(unittest.TestCase):
turnoff = screensaver.TurnOffDialog('gui.xml', screensaver.ADDON_PATH, 'default')

def test_screensaver(self):
@staticmethod
def test_screensaver_log():
''' Test enabling screensaver '''
self.turnoff.onInit()
screensaver.ADDON.settings['display_method'] = '0'
screensaver.ADDON.settings['power_method'] = '0'
turnoff = screensaver.TurnOffDialog('gui.xml', screensaver.ADDON_PATH, 'default')
turnoff.onInit()
time.sleep(5)
self.turnoff.resume()
turnoff.resume()

@staticmethod
def test_screensaver_builtin():
''' Test enabling screensaver '''
screensaver.ADDON.settings['display_method'] = '1'
screensaver.ADDON.settings['power_method'] = '1'
turnoff = screensaver.TurnOffDialog('gui.xml', screensaver.ADDON_PATH, 'default')
turnoff.onInit()
time.sleep(5)
turnoff.resume()

@unittest.expectedFailure
def test_screensaver_command(self):
''' Test enabling screensaver '''
screensaver.ADDON.settings['display_method'] = '2'
screensaver.ADDON.settings['power_method'] = '2'
turnoff = screensaver.TurnOffDialog('gui.xml', screensaver.ADDON_PATH, 'default')
with self.assertRaises(SystemExit) as init:
turnoff.onInit()
self.assertEqual(init.exception.code, 2)
time.sleep(5)
with self.assertRaises(SystemExit) as resume:
turnoff.resume()
self.assertEqual(resume.exception.code, 2)


if __name__ == '__main__':
Expand Down

0 comments on commit 310a81b

Please sign in to comment.