diff --git a/tests/test_common.py b/tests/test_common.py new file mode 100644 index 0000000..3879209 --- /dev/null +++ b/tests/test_common.py @@ -0,0 +1,29 @@ +# Copyright 2023 Iguazio +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import unittest +from v3io.common.helpers import url_join + +class Test(unittest.TestCase): + def test_url_join(self): + self.assertEqual(url_join('a','b') , 'a/b') # add one '/' between parts + self.assertEqual(url_join('/','a','b') , '/a/b') # limit '/' prefix count to one + self.assertEqual(url_join('/','/a','b') , '/a/b') # + self.assertEqual(url_join('/','a','/b'), '/a/b') # + self.assertEqual(url_join('/','/a','/b'), '/a/b') # + self.assertEqual(url_join('a','b'), 'a/b') # keep suffix '/' exist/not-exist invariant + self.assertEqual(url_join('a','b/'), 'a/b/') # + self.assertEqual(url_join('a','b//'), 'a/b//') # + self.assertEqual(url_join('a','b//', '/'), 'a/b//') # suffix '/' count may change (we don't care) + diff --git a/v3io/common/helpers.py b/v3io/common/helpers.py index ded4b0c..1be9d2b 100644 --- a/v3io/common/helpers.py +++ b/v3io/common/helpers.py @@ -12,13 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from functools import reduce - - -def _join_slash(left, right): - return left.rstrip("/") + "/" + right.lstrip("/") - def url_join(*parts): - """join parts by pairs with a single slash, leaving left-part leading and right-part trailing slashes.""" - return reduce(_join_slash, parts) if parts else "" + result = "" + slash_suffix = False + for part_index, part in enumerate(parts): + if part == '': + continue + # add slash prefix before part if: + # 1. slash suffix did not exit in prev part + # 2. slash prefix does not exit in this part + # 3. part is not the first + if not slash_suffix and part[0] != '/' and part_index != 0: + result += '/' + part + else: + # if slash suffix existed in prev trim slash prefix from this part + result += part if not slash_suffix else part.lstrip("/") + slash_suffix = True if part[-1] == '/' else False + return result