Browse Source

"file:" Location: use absolute path

Thomas Waldmann 7 months ago
parent
commit
ffdc9581d9
2 changed files with 10 additions and 12 deletions
  1. 1 1
      src/borg/helpers/parseformat.py
  2. 9 11
      src/borg/testsuite/helpers_test.py

+ 1 - 1
src/borg/helpers/parseformat.py

@@ -502,7 +502,7 @@ class Location:
         m = self.local_re.match(text)
         m = self.local_re.match(text)
         if m:
         if m:
             self.proto = "file"
             self.proto = "file"
-            self.path = os.path.normpath(m.group("path"))
+            self.path = os.path.abspath(os.path.normpath(m.group("path")))
             return True
             return True
         return False
         return False
 
 

+ 9 - 11
src/borg/testsuite/helpers_test.py

@@ -226,12 +226,10 @@ class TestLocationWithoutEnv:
 
 
     def test_folder(self, monkeypatch, keys_dir):
     def test_folder(self, monkeypatch, keys_dir):
         monkeypatch.delenv("BORG_REPO", raising=False)
         monkeypatch.delenv("BORG_REPO", raising=False)
-        assert repr(Location("path")) == "Location(proto='file', user=None, host=None, port=None, path='path')"
-        assert Location("path").to_key_filename() == keys_dir + "path"
-
-    def test_long_path(self, monkeypatch, keys_dir):
-        monkeypatch.delenv("BORG_REPO", raising=False)
-        assert Location(os.path.join(*(40 * ["path"]))).to_key_filename() == keys_dir + "_".join(20 * ["path"]) + "_"
+        rel_path = "path"
+        abs_path = os.path.abspath(rel_path)
+        assert repr(Location(rel_path)) == f"Location(proto='file', user=None, host=None, port=None, path='{abs_path}')"
+        assert Location("path").to_key_filename().endswith(rel_path)
 
 
     def test_abspath(self, monkeypatch, keys_dir):
     def test_abspath(self, monkeypatch, keys_dir):
         monkeypatch.delenv("BORG_REPO", raising=False)
         monkeypatch.delenv("BORG_REPO", raising=False)
@@ -248,11 +246,11 @@ class TestLocationWithoutEnv:
 
 
     def test_relpath(self, monkeypatch, keys_dir):
     def test_relpath(self, monkeypatch, keys_dir):
         monkeypatch.delenv("BORG_REPO", raising=False)
         monkeypatch.delenv("BORG_REPO", raising=False)
-        assert (
-            repr(Location("relative/path"))
-            == "Location(proto='file', user=None, host=None, port=None, path='relative/path')"
-        )
-        assert Location("relative/path").to_key_filename() == keys_dir + "relative_path"
+        # for a local path, borg creates a Location instance with an absolute path
+        rel_path = "relative/path"
+        abs_path = os.path.abspath(rel_path)
+        assert repr(Location(rel_path)) == f"Location(proto='file', user=None, host=None, port=None, path='{abs_path}')"
+        assert Location(rel_path).to_key_filename().endswith("relative_path")
         assert (
         assert (
             repr(Location("ssh://user@host/relative/path"))
             repr(Location("ssh://user@host/relative/path"))
             == "Location(proto='ssh', user='user', host='host', port=None, path='relative/path')"
             == "Location(proto='ssh', user='user', host='host', port=None, path='relative/path')"