diff --git a/plinth/context_processors.py b/plinth/context_processors.py index e58d7e162..433e0dd4b 100644 --- a/plinth/context_processors.py +++ b/plinth/context_processors.py @@ -25,8 +25,7 @@ from plinth import cfg def common(request): - """ - Add additional context values to RequestContext for use in templates. + """Add additional context values to RequestContext for use in templates. Any resources referenced in the return value are expected to have been initialized or configured externally beforehand. diff --git a/plinth/tests/coverage/coverage.py b/plinth/tests/coverage/coverage.py index aa22e7cbf..965bf3393 100644 --- a/plinth/tests/coverage/coverage.py +++ b/plinth/tests/coverage/coverage.py @@ -73,7 +73,7 @@ class CoverageCommand(setuptools.Command): # Erase any existing HTML report files try: shutil.rmtree(COVERAGE_REPORT_DIR, True) - except: + except Exception: pass # Run the coverage analysis diff --git a/plinth/tests/data/django_test_settings.py b/plinth/tests/data/django_test_settings.py index 894b60da3..8f2978c90 100644 --- a/plinth/tests/data/django_test_settings.py +++ b/plinth/tests/data/django_test_settings.py @@ -1,7 +1,28 @@ -# Django settings for test modules. +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +""" +Django settings for test modules. +""" import os + TEST_DATA_DIR = os.path.dirname(os.path.abspath(__file__)) + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', @@ -9,8 +30,6 @@ DATABASES = { } } -DEFAULT_INDEX_TABLESPACE = '' - INSTALLED_APPS = ( 'plinth', ) diff --git a/plinth/tests/runtests.py b/plinth/tests/runtests.py index 6aabc7862..992b82509 100644 --- a/plinth/tests/runtests.py +++ b/plinth/tests/runtests.py @@ -16,7 +16,9 @@ # along with this program. If not, see . # -"""Module for Django pre-test configuration and setup.""" +""" +Module for Django pre-test configuration and setup. +""" import os import sys @@ -28,7 +30,7 @@ from django.test.utils import get_runner def run_tests(pattern=None, return_to_caller=False): """Set up the Django test environment and run the specified tests.""" - os.environ['DJANGO_SETTINGS_MODULE'] =\ + os.environ['DJANGO_SETTINGS_MODULE'] = \ 'plinth.tests.data.django_test_settings' django.setup() TestRunner = get_runner(settings) @@ -36,6 +38,7 @@ def run_tests(pattern=None, return_to_caller=False): if pattern is None: pattern = 'plinth.tests' + failures = test_runner.run_tests([pattern]) if failures > 0 or not return_to_caller: sys.exit(bool(failures)) diff --git a/plinth/tests/test_actions.py b/plinth/tests/test_actions.py index 4da222834..1cc70724c 100644 --- a/plinth/tests/test_actions.py +++ b/plinth/tests/test_actions.py @@ -1,5 +1,3 @@ -#!/usr/bin/python3 -# -*- mode: python; mode: auto-fill; fill-column: 80 -*- # # This file is part of Plinth. # @@ -17,6 +15,10 @@ # along with this program. If not, see . # +""" +Test module for actions utilities that modify configuration. +""" + import os import shutil import unittest @@ -45,8 +47,8 @@ class TestPrivileged(unittest.TestCase): @classmethod def tearDownClass(cls): - os.remove('actions/echo') - os.remove('actions/id') + os.remove(os.path.join(cfg.actions_dir, 'echo')) + os.remove(os.path.join(cfg.actions_dir, 'id')) def notest_run_as_root(self): """1. Privileged actions run as root. """ @@ -141,8 +143,3 @@ class TestPrivileged(unittest.TestCase): output = run('echo', tuple(options.split())) output = output.rstrip('\n') self.assertEqual(options, output) - - - -if __name__ == "__main__": - unittest.main() diff --git a/plinth/tests/test_cfg.py b/plinth/tests/test_cfg.py index c5f9ab27b..e491a0a0b 100644 --- a/plinth/tests/test_cfg.py +++ b/plinth/tests/test_cfg.py @@ -1,4 +1,3 @@ -#!/usr/bin/python3 # # This file is part of Plinth. # @@ -16,6 +15,10 @@ # along with this program. If not, see . # +""" +Test module for configuration module. +""" + import configparser import os import shutil @@ -244,7 +247,3 @@ class CfgTestCase(unittest.TestCase): if os.path.isfile(TEST_CONFIG_FILE): os.remove(TEST_CONFIG_FILE) shutil.move(SAVED_CONFIG_FILE, TEST_CONFIG_FILE) - - -if __name__ == '__main__': - unittest.main() diff --git a/plinth/tests/test_context_processors.py b/plinth/tests/test_context_processors.py index f3be34fab..b48a4f040 100644 --- a/plinth/tests/test_context_processors.py +++ b/plinth/tests/test_context_processors.py @@ -1,4 +1,3 @@ -#!/usr/bin/python3 # # This file is part of Plinth. # @@ -16,6 +15,10 @@ # along with this program. If not, see . # +""" +Test module for Plinth's custom context processors. +""" + from django.http import HttpRequest from django.test import TestCase diff --git a/plinth/tests/test_kvstore.py b/plinth/tests/test_kvstore.py index fdf078bc0..8854f1a98 100644 --- a/plinth/tests/test_kvstore.py +++ b/plinth/tests/test_kvstore.py @@ -1,4 +1,3 @@ -#!/usr/bin/python3 # # This file is part of Plinth. # @@ -16,15 +15,19 @@ # along with this program. If not, see . # +""" +Test module for key/value store. +""" + from django.test import TestCase from plinth import kvstore -class KvstoreTestCase(TestCase): +class KVStoreTestCase(TestCase): """Verify the behavior of the kvstore module.""" - def test_get(self): + def test_get_set(self): """Verify that a set value can be retrieved.""" key = 'name' expected_value = 'Guido' @@ -32,6 +35,15 @@ class KvstoreTestCase(TestCase): actual_value = kvstore.get(key) self.assertEqual(expected_value, actual_value) + def test_get_set_complex_structures(self): + """Verify that complex structures can be stored and retrieved.""" + key = 'compex_structure' + expected_value = {'k1': 1, 'k2': [2, 3], 'k3': 4.5, 'k4': 'Hello', + 'k5': {'a': 'b'}} + kvstore.set(key, expected_value) + actual_value = kvstore.get(key) + self.assertEqual(expected_value, actual_value) + def test_get_default(self): """Verify that either a set value or its default can be retrieved.""" expected = 'default' diff --git a/plinth/tests/test_menu.py b/plinth/tests/test_menu.py index dcb214d26..ccc9d52a3 100644 --- a/plinth/tests/test_menu.py +++ b/plinth/tests/test_menu.py @@ -1,4 +1,3 @@ -#!/usr/bin/python3 # # This file is part of Plinth. # @@ -16,6 +15,10 @@ # along with this program. If not, see . # +""" +Test module for menus. +""" + from django.core.urlresolvers import reverse from django.http import HttpRequest from django.test import TestCase @@ -38,9 +41,11 @@ def build_menu(size=5): 'Icon' + str(index), URL_TEMPLATE.format(index, index, index), random.randint(0, 1000)]) + menu = Menu() for data in item_data: menu.add_item(data[0], data[1], data[2], data[3]) + return menu @@ -133,7 +138,9 @@ class MenuTestCase(TestCase): actual_item = menu.add_urlname(expected_label, expected_icon, expected_url, expected_order) + self.assertEqual(1, len(menu.items)) self.assertIsNotNone(actual_item) + self.assertEqual(actual_item, menu.items[0]) self.assertEqual(expected_label, actual_item.label) self.assertEqual(expected_icon, actual_item.icon) self.assertEqual(reversed_url, actual_item.url) @@ -150,7 +157,9 @@ class MenuTestCase(TestCase): actual_item = menu.add_item(expected_label, expected_icon, expected_url, expected_order) + self.assertEqual(1, len(menu.items)) self.assertIsNotNone(actual_item) + self.assertEqual(actual_item, menu.items[0]) self.assertEqual(expected_label, actual_item.label) self.assertEqual(expected_icon, actual_item.icon) self.assertEqual(expected_url, actual_item.url) diff --git a/plinth/tests/test_network.py b/plinth/tests/test_network.py index 65b97e2ad..78c88a89d 100644 --- a/plinth/tests/test_network.py +++ b/plinth/tests/test_network.py @@ -1,4 +1,3 @@ -#!/usr/bin/python3 # # This file is part of Plinth. # @@ -16,6 +15,10 @@ # along with this program. If not, see . # +""" +Test module for network configuration utilities. +""" + import os import unittest @@ -116,7 +119,3 @@ class TestNetwork(unittest.TestCase): self.assertEqual( secrets['802-11-wireless-security']['psk'], 'secretpassword') - - -if __name__ == "__main__": - unittest.main() diff --git a/plinth/tests/test_pagekite.py b/plinth/tests/test_pagekite.py index 7463f25f5..613bc4c18 100644 --- a/plinth/tests/test_pagekite.py +++ b/plinth/tests/test_pagekite.py @@ -1,4 +1,3 @@ -#!/usr/bin/python3 # # This file is part of Plinth. # @@ -16,6 +15,10 @@ # along with this program. If not, see . # +""" +Test modules for Pagekite functions. +""" + import unittest diff --git a/plinth/tests/test_templatetags.py b/plinth/tests/test_templatetags.py index 8f815cc6d..b24334d9c 100644 --- a/plinth/tests/test_templatetags.py +++ b/plinth/tests/test_templatetags.py @@ -15,6 +15,10 @@ # along with this program. If not, see . # +""" +Test module for custom Django template tags. +""" + import unittest from plinth.templatetags.plinth_extras import mark_active_menuitem @@ -30,7 +34,8 @@ class TestShowSubSubMenu(unittest.TestCase): else: self.assertFalse(item['active']) - def _verify_active_menuitems(self, menu): + @staticmethod + def _verify_active_menuitems(menu): """Verify that one and only one menuitem is marked as active""" return sum([item['active'] for item in menu]) == 1