From bf477b6b9c492c5202c6263436fe28c55f06ddaf Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Tue, 22 Dec 2015 14:55:13 -0500 Subject: [PATCH] add slash_join helper and tests --- test/test_util.py | 16 ++++++++++++++++ util/string.py | 13 +++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 util/string.py diff --git a/test/test_util.py b/test/test_util.py index a04b25bfb..2c6254c21 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -6,6 +6,7 @@ from semantic_version import Version, Spec from util.validation import generate_valid_usernames from util.registry.generatorfile import GeneratorFile from util.registry.dockerver import docker_version +from util.string import slash_join class TestGeneratorFile(unittest.TestCase): def sample_generator(self): @@ -174,6 +175,21 @@ class TestDockerVersionParsing(unittest.TestCase): self.assertTrue(spec.match(Version(match_case)), 'Spec: %s Case: %s' % (spec, match_case)) + +class TestSlashJoining(unittest.TestCase): + def test_joining(self): + test_cases = [ + (['https://github.com', '/coreos-inc/' 'quay/pull/1092/files'], + 'https://github.com/coreos-inc/quay/pull/1092/files'), + (['https://', 'github.com/', '/coreos-inc', '/quay/pull/1092/files/'], + 'https://github.com/coreos-inc/quay/pull/1092/files'), + ] + + for args, url in test_cases: + joined_url = slash_join(*args) + self.assertEquals(url, joined_url) + + if __name__ == '__main__': unittest.main() diff --git a/util/string.py b/util/string.py new file mode 100644 index 000000000..c47907817 --- /dev/null +++ b/util/string.py @@ -0,0 +1,13 @@ +def slash_join(*args): + """ + Joins together strings and guarantees there is only one '/' in between the + each string joined. Double slashes ('//') are assumed to be intentional and + are not deduplicated. + """ + def rmslash(path): + path = path[1:] if path[0] == '/' else path + path = path[:-1] if path[-1] == '/' else path + return path + + args = [rmslash(path) for path in args] + return '/'.join(args)