From 65afc249bcfc332e8158bab52f6911c05d5ca83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20F=C3=B6rster?= Date: Fri, 14 Mar 2025 12:21:00 +0100 Subject: [PATCH] add route combination --- src/dopt_basics/io.py | 11 +++++++++ tests/test_io.py | 52 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/dopt_basics/io.py b/src/dopt_basics/io.py index 1fd7643..ef1d573 100644 --- a/src/dopt_basics/io.py +++ b/src/dopt_basics/io.py @@ -1,6 +1,7 @@ from __future__ import annotations import pickle +import re import shutil from collections.abc import Sequence from pathlib import Path @@ -95,6 +96,16 @@ def prepare_path( return (pth_parent / filename).with_suffix(suffix) +def _strip_url_components(string: str) -> str: + return re.sub(r"^[ /]+|[ /]+$", "", string) + + +def combine_route(base_url: str, route: str) -> str: + base_url = _strip_url_components(base_url) + route = _strip_url_components(route) + return "/".join((base_url, route)) + + def search_cwd( glob_pattern: str, ) -> Path | None: diff --git a/tests/test_io.py b/tests/test_io.py index 5c1c7f9..a9fb3b3 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -70,7 +70,7 @@ def test_create_folder(tmp_path, delete_existing): assert target_dir.is_dir() -def test_prepare_save_path_SuccessWithCreate(tmp_path): +def test_prepare_path_SuccessWithCreate(tmp_path): base_folder = tmp_path dirs = ("target", "dir") filename = None @@ -81,7 +81,7 @@ def test_prepare_save_path_SuccessWithCreate(tmp_path): assert res_pth == target_pth -def test_prepare_save_path_SuccessWithCreateTimestamp(tmp_path): +def test_prepare_path_SuccessWithCreateTimestamp(tmp_path): base_folder = tmp_path dirs = ("target", "dir") filename = "test" @@ -92,7 +92,7 @@ def test_prepare_save_path_SuccessWithCreateTimestamp(tmp_path): assert res_pth.parent.exists() -def test_prepare_save_path_SuccessWithoutCreate(tmp_path): +def test_prepare_path_SuccessWithoutCreate(tmp_path): base_folder = tmp_path dirs = ("target", "dir") filename = None @@ -103,7 +103,7 @@ def test_prepare_save_path_SuccessWithoutCreate(tmp_path): assert res_pth == target_pth -def test_prepare_save_path_FailNoTargets(tmp_path): +def test_prepare_path_FailNoTargets(tmp_path): base_folder = tmp_path dirs = None filename = None @@ -118,7 +118,7 @@ def test_prepare_save_path_FailNoTargets(tmp_path): ) -def test_prepare_save_path_FailNoFilenameSuffix(tmp_path): +def test_prepare_path_FailNoFilenameSuffix(tmp_path): base_folder = tmp_path dirs = None filename = None @@ -144,7 +144,7 @@ def test_prepare_save_path_FailNoFilenameSuffix(tmp_path): ) -def test_prepare_save_path_FailTimestampWithoutFilename(tmp_path): +def test_prepare_path_FailTimestampWithoutFilename(tmp_path): base_folder = tmp_path dirs = ["test"] filename = None @@ -160,7 +160,7 @@ def test_prepare_save_path_FailTimestampWithoutFilename(tmp_path): ) -def test_prepare_save_path_FailBadSuffix(tmp_path): +def test_prepare_path_FailBadSuffix(tmp_path): base_folder = tmp_path dirs = None filename = "test" @@ -176,7 +176,7 @@ def test_prepare_save_path_FailBadSuffix(tmp_path): ) -def test_prepare_save_path_SuccessSuffixAddDot(tmp_path): +def test_prepare_path_SuccessSuffixAddDot(tmp_path): base_folder = tmp_path dirs = None filename = "test" @@ -193,6 +193,42 @@ def test_prepare_save_path_SuccessSuffixAddDot(tmp_path): assert ret_path == target_path +@pytest.mark.parametrize( + ["case", "expect"], + [ + ("http://test.com/ ", "http://test.com"), + ("http://test.com/", "http://test.com"), + ("http://test.com ", "http://test.com"), + ("http://test.com// ", "http://test.com"), + ("http://test.com", "http://test.com"), + (" /http://test.com", "http://test.com"), + (" //http://test.com", "http://test.com"), + ("//http://test.com", "http://test.com"), + ], +) +def test_strip_url_components(case, expect): + res = io._strip_url_components(case) + assert res == expect + + +@pytest.mark.parametrize( + ["base", "route", "expect"], + [ + ("http://test.com/ ", "ping", "http://test.com/ping"), + ("http://test.com/", "ping ", "http://test.com/ping"), + ("http://test.com ", "ping/", "http://test.com/ping"), + ("http://test.com// ", "ping", "http://test.com/ping"), + ("http://test.com", "/ping ", "http://test.com/ping"), + (" /http://test.com", "/ ping/ ", "http://test.com/ping"), + (" //http://test.com", "ping", "http://test.com/ping"), + ("//http://test.com", "// ping// ", "http://test.com/ping"), + ], +) +def test_combine_route(base, route, expect): + res = io.combine_route(base, route) + assert res == expect + + def test_search_cwd(monkeypatch, base_folder, target_file_pth): monkeypatch.setattr(Path, "cwd", lambda: base_folder) assert Path.cwd() == base_folder