diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-10-01 22:39:31 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-10-01 22:39:31 +0200 |
commit | 324032f2ad59318ec58def7182a26d7bf014b161 (patch) | |
tree | b0c8cb88bc75bb1aa4f28e12c21812ba6e2ea936 | |
parent | edcb688a6a54c8758ba27b085d1cc5a5159a7880 (diff) | |
download | psutil-324032f2ad59318ec58def7182a26d7bf014b161.tar.gz |
add tests for fs test utils
-rw-r--r-- | psutil/tests/__init__.py | 13 | ||||
-rw-r--r-- | psutil/tests/test_misc.py | 74 |
2 files changed, 87 insertions, 0 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 8f17d35e..07e48273 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -476,6 +476,19 @@ def safe_rmdir(dir): raise +def safe_rmpath(path): + """Removes a path either if it's a file or a directory. + If neither exist just do nothing. + """ + try: + safe_remove(TESTFN) + except OSError as err: + if err.errno == errno.EISDIR: + safe_rmdir(path) + else: + raise + + @contextlib.contextmanager def chdir(dirname): """Context manager which temporarily changes the current directory.""" diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index 00f26695..6479bd85 100644 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -23,12 +23,15 @@ from psutil import POSIX from psutil import WINDOWS from psutil._common import supports_ipv6 from psutil.tests import APPVEYOR +from psutil.tests import chdir from psutil.tests import importlib from psutil.tests import mock from psutil.tests import retry from psutil.tests import ROOT_DIR from psutil.tests import run_test_module_by_name from psutil.tests import safe_remove +from psutil.tests import safe_rmdir +from psutil.tests import safe_rmpath from psutil.tests import SCRIPTS_DIR from psutil.tests import sh from psutil.tests import TESTFN @@ -550,5 +553,76 @@ class TestSyncTestUtils(unittest.TestCase): assert os.path.exists(TESTFN) +class TestFSTestUtils(unittest.TestCase): + + def setUp(self): + safe_rmpath(TESTFN) + + tearDown = setUp + + def test_safe_remove(self): + # test file is removed + open(TESTFN, 'w').close() + safe_remove(TESTFN) + assert not os.path.exists(TESTFN) + # test no exception if file does not exist + safe_remove(TESTFN) + # test we get an exc if path is a directory + os.mkdir(TESTFN) + with self.assertRaises(OSError) as cm: + safe_remove(TESTFN) + self.assertEqual(cm.exception.errno, errno.EISDIR) + # ...or any other error + with mock.patch('psutil.tests.os.remove', + side_effect=OSError(errno.EINVAL, "")) as m: + with self.assertRaises(OSError) as cm: + safe_remove(TESTFN) + assert m.called + + def test_safe_rmdir(self): + # test dir is removed + os.mkdir(TESTFN) + safe_rmdir(TESTFN) + assert not os.path.exists(TESTFN) + # test no exception if dir does not exist + safe_remove(TESTFN) + # test we get an exc if path is a file + open(TESTFN, 'w').close() + with self.assertRaises(OSError) as cm: + safe_rmdir(TESTFN) + self.assertEqual(cm.exception.errno, errno.ENOTDIR) + # ...or any other error + with mock.patch('psutil.tests.os.rmdir', + side_effect=OSError(errno.EINVAL, "")) as m: + with self.assertRaises(OSError) as cm: + safe_rmdir(TESTFN) + assert m.called + + def test_safe_rmpath(self): + safe_rmpath(TESTFN) + assert not os.path.exists(TESTFN) + # path is file + open(TESTFN, 'w').close() + safe_rmpath(TESTFN) + assert not os.path.exists(TESTFN) + # path is dir + os.mkdir(TESTFN) + safe_rmpath(TESTFN) + assert not os.path.exists(TESTFN) + # path is something else which raises an exc + with mock.patch('psutil.tests.os.remove', + side_effect=OSError(errno.EINVAL, "")) as m: + with self.assertRaises(OSError): + safe_rmpath(TESTFN) + assert m.called + + def test_chdir(self): + base = os.getcwd() + os.mkdir(TESTFN) + with chdir(TESTFN): + self.assertEqual(os.getcwd(), os.path.join(base, TESTFN)) + self.assertEqual(os.getcwd(), base) + + if __name__ == '__main__': run_test_module_by_name(__file__) |