From 8c69e05f94ae52dd02dd42e44ab75e4d85e79e69 Mon Sep 17 00:00:00 2001 From: "Martin A. Brown" Date: Tue, 23 Feb 2016 13:10:10 -0800 Subject: [PATCH] beefing up some testing for new functions --- tests/test_utils.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ tldp/utils.py | 19 ++++++++++++------- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 6c985f1..518d97a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, division, print_function import os +import stat import uuid import unittest from tempfile import NamedTemporaryFile as ntf @@ -11,6 +12,51 @@ from tldptesttools import TestToolsFilesystem # -- SUT from tldp.utils import makefh, which, execute from tldp.utils import statfiles, stem_and_ext +from tldp.utils import arg_isexecutable, isexecutable +from tldp.utils import arg_isreadablefile, isreadablefile +from tldp.utils import arg_isdirectory +from tldp.utils import arg_isloglevel + + +class Test_isexecutable_and_friends(unittest.TestCase): + + def test_isexecutable(self): + f = ntf(prefix='executable-file') + self.assertFalse(isexecutable(f.name)) + mode = stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR + os.chmod(f.name, mode) + self.assertTrue(isexecutable(f.name)) + + def test_arg_isexecutable(self): + f = ntf(prefix='executable-file') + self.assertIsNone(arg_isexecutable(f.name)) + mode = stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR + os.chmod(f.name, mode) + self.assertEqual(f.name, arg_isexecutable(f.name)) + + +class Test_isreadablefile_and_friends(unittest.TestCase): + + def test_isreadablefile(self): + f = ntf(prefix='readable-file') + self.assertTrue(isreadablefile(f.name)) + os.chmod(f.name, 0) + self.assertFalse(isreadablefile(f.name)) + + def test_arg_isreadablefile(self): + f = ntf(prefix='readable-file') + self.assertEquals(f.name, arg_isreadablefile(f.name)) + os.chmod(f.name, 0) + self.assertIsNone(arg_isreadablefile(f.name)) + + +class Test_arg_isloglevel(unittest.TestCase): + + def test_arg_isloglevel_integer(self): + self.assertEquals(7, arg_isloglevel(7)) + self.assertEquals(40, arg_isloglevel('frobnitz')) + self.assertEquals(20, arg_isloglevel('INFO')) + self.assertEquals(10, arg_isloglevel('DEBUG')) class Test_execute(TestToolsFilesystem): diff --git a/tldp/utils.py b/tldp/utils.py index 29534d7..e70aa4c 100644 --- a/tldp/utils.py +++ b/tldp/utils.py @@ -28,7 +28,7 @@ logger = getLogger() def firstfoundfile(locations): '''return the first existing file from a list of filenames (or None)''' for option in locations: - if os.path.isfile(option): + if isreadablefile(option): return option return None @@ -40,19 +40,19 @@ def arg_isloglevel(l): level = getattr(logging, l.upper(), None) try: logging.getLogger().setLevel(level) - except ValueError: + except (TypeError, ValueError): level = logging.ERROR return level -def arg_isfile(f): - if os.path.exists(f): +def arg_isreadablefile(f): + if isreadablefile(f): return f return None def arg_isdirectory(d): - if os.path.exists(d): + if os.path.isdir(d): return d return None @@ -152,9 +152,14 @@ def execute(cmd, stdin=None, stdout=None, stderr=None, return result -def isexecutable(fpath): +def isexecutable(f): '''True if argument is executable''' - return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + return os.path.isfile(f) and os.access(f, os.X_OK) + + +def isreadablefile(f): + '''True if argument is readable file''' + return os.path.isfile(f) and os.access(f, os.R_OK) def which(program):