From f4adcf24ec4b7e2ec321718f6cfdb9870a8ed4a2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 27 Jan 2018 17:20:52 +0000 Subject: Handle absolute paths in zips.join_path --- mammoth/zips.py | 11 ++++++++++- tests/zips_tests.py | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/mammoth/zips.py b/mammoth/zips.py index cca0b49..79e9b17 100644 --- a/mammoth/zips.py +++ b/mammoth/zips.py @@ -65,4 +65,13 @@ def split_path(path): def join_path(*args): - return "/".join(list(filter(None, args))) + non_empty_paths = list(filter(None, args)) + + relevant_paths = [] + for path in non_empty_paths: + if path.startswith("/"): + relevant_paths = [path] + else: + relevant_paths.append(path) + + return "/".join(relevant_paths) diff --git a/tests/zips_tests.py b/tests/zips_tests.py index 29f7fbf..8d1505f 100644 --- a/tests/zips_tests.py +++ b/tests/zips_tests.py @@ -27,4 +27,11 @@ def empty_parts_are_ignored_when_joining_paths(): assert_equal("a", zips.join_path("a", "")) assert_equal("b", zips.join_path("", "b")) assert_equal("a/b", zips.join_path("a", "", "b")) - + + +@istest +def when_joining_paths_then_absolute_paths_ignore_earlier_paths(): + assert_equal("/b", zips.join_path("a", "/b")) + assert_equal("/b/c", zips.join_path("a", "/b", "c")) + assert_equal("/b", zips.join_path("/a", "/b")) + assert_equal("/a", zips.join_path("/a")) -- cgit v1.2.3