From 5bb2134e1d6a139eb6698325d8c475bebdec2fad Mon Sep 17 00:00:00 2001 From: Lazlo Westerhof Date: Thu, 19 Oct 2023 10:23:30 +0200 Subject: [PATCH] Unit tests: add tests for pathutil --- unit-tests/test_util_pathutil.py | 107 +++++++++++++++++++++++++++++++ unit-tests/unit_tests.py | 2 + util/pathutil.py | 5 -- 3 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 unit-tests/test_util_pathutil.py diff --git a/unit-tests/test_util_pathutil.py b/unit-tests/test_util_pathutil.py new file mode 100644 index 000000000..001f57fd9 --- /dev/null +++ b/unit-tests/test_util_pathutil.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- +"""Unit tests for the pathutil utils module""" + +__copyright__ = 'Copyright (c) 2023, Utrecht University' +__license__ = 'GPLv3, see LICENSE' + +import sys +from unittest import TestCase + +sys.path.append('../util') + +from pathutil import basename, chop, dirname, info, Space + + +class UtilPathutilTest(TestCase): + + def test_chop(self): + output = chop("") + self.assertEquals(output, ('/', '')) + output = chop("/") + self.assertEquals(output, ('/', '')) + output = chop("/tempZone") + self.assertEquals(output, ('', 'tempZone')) + output = chop("/tempZone/yoda") + self.assertEquals(output, ('/tempZone', 'yoda')) + output = chop("/tempZone/home") + self.assertEquals(output, ('/tempZone', 'home')) + output = chop("/tempZone/home/rods") + self.assertEquals(output, ('/tempZone/home', 'rods')) + output = chop("/tempZone/home/research-test") + self.assertEquals(output, ('/tempZone/home', 'research-test')) + output = chop("/tempZone/home/research-test/test") + self.assertEquals(output, ('/tempZone/home/research-test', 'test')) + output = chop("/tempZone/home/research-test/test/file.txt") + self.assertEquals(output, ('/tempZone/home/research-test/test', 'file.txt')) + + def test_dirname(self): + output = dirname("") + self.assertEquals(output, '/') + output = dirname("/") + self.assertEquals(output, '/') + output = dirname("/tempZone") + self.assertEquals(output, '') + output = dirname("/tempZone/yoda") + self.assertEquals(output, '/tempZone') + output = dirname("/tempZone/home") + self.assertEquals(output, '/tempZone') + output = dirname("/tempZone/home/rods") + self.assertEquals(output, '/tempZone/home') + output = dirname("/tempZone/home/research-test") + self.assertEquals(output, '/tempZone/home') + output = dirname("/tempZone/home/research-test/test") + self.assertEquals(output, '/tempZone/home/research-test') + output = dirname("/tempZone/home/research-test/test/file.txt") + self.assertEquals(output, '/tempZone/home/research-test/test') + + def test_basename(self): + output = basename("") + self.assertEquals(output, '') + output = basename("/") + self.assertEquals(output, '') + output = basename("/tempZone") + self.assertEquals(output, 'tempZone') + output = basename("/tempZone/yoda") + self.assertEquals(output, 'yoda') + output = basename("/tempZone/home") + self.assertEquals(output, 'home') + output = basename("/tempZone/home/rods") + self.assertEquals(output, 'rods') + output = basename("/tempZone/home/research-test") + self.assertEquals(output, 'research-test') + output = basename("/tempZone/home/research-test/test") + self.assertEquals(output, 'test') + output = basename("/tempZone/home/research-test/test/file.txt") + self.assertEquals(output, 'file.txt') + + def test_info(self): + output = info("") + self.assertEquals(output, (Space.OTHER, '', '', '')) + output = info("/") + self.assertEquals(output, (Space.OTHER, '', '', '')) + output = info("/tempZone") + self.assertEquals(output, (Space.OTHER, 'tempZone', '', '')) + output = info("/tempZone/yoda") + self.assertEquals(output, (Space.OTHER, 'tempZone', '', 'yoda')) + output = info("/tempZone/home") + self.assertEquals(output, (Space.OTHER, 'tempZone', '', 'home')) + output = info("/tempZone/home/rods") + self.assertEquals(output, (Space.OTHER, 'tempZone', 'rods', '')) + output = info("/tempZone/home/research-test") + self.assertEquals(output, (Space.RESEARCH, 'tempZone', 'research-test', '')) + output = info("/tempZone/home/research-test/test") + self.assertEquals(output, (Space.RESEARCH, 'tempZone', 'research-test', 'test')) + output = info("/tempZone/home/research-test/test/file.txt") + self.assertEquals(output, (Space.RESEARCH, 'tempZone', 'research-test', 'test/file.txt')) + output = info("/tempZone/home/vault-test") + self.assertEquals(output, (Space.VAULT, 'tempZone', 'vault-test', '')) + output = info("/tempZone/home/datamanager-test") + self.assertEquals(output, (Space.DATAMANAGER, 'tempZone', 'datamanager-test', '')) + output = info("/tempZone/home/deposit-test") + self.assertEquals(output, (Space.DEPOSIT, 'tempZone', 'deposit-test', '')) + output = info("/tempZone/home/intake-test") + self.assertEquals(output, (Space.INTAKE, 'tempZone', 'intake-test', '')) + output = info("/tempZone/home/grp-intake-test") + self.assertEquals(output, (Space.INTAKE, 'tempZone', 'grp-intake-test', '')) + output = info("/tempZone/home/datarequests-test") + self.assertEquals(output, (Space.DATAREQUEST, 'tempZone', 'datarequests-test', '')) diff --git a/unit-tests/unit_tests.py b/unit-tests/unit_tests.py index 71fee11ff..242f30015 100644 --- a/unit-tests/unit_tests.py +++ b/unit-tests/unit_tests.py @@ -7,10 +7,12 @@ from test_intake import IntakeTest from test_util_misc import UtilMiscTest +from test_util_pathutil import UtilPathutilTest def suite(): test_suite = TestSuite() test_suite.addTest(makeSuite(IntakeTest)) test_suite.addTest(makeSuite(UtilMiscTest)) + test_suite.addTest(makeSuite(UtilPathutilTest)) return test_suite diff --git a/util/pathutil.py b/util/pathutil.py index 32d2b6164..0552120eb 100644 --- a/util/pathutil.py +++ b/util/pathutil.py @@ -64,11 +64,6 @@ def basename(path): return chop(path)[1] # chops everything *but* the last component -def chopext(path): - """Return the extension of a path.""" - return path.rsplit('.', 1) - - def info(path): """Parse a path into a (Space, zone, group, subpath) tuple.