Browse Source

added function append_to_name

Fabian Peter Hammerle 9 năm trước cách đây
mục cha
commit
278a696c1f
2 tập tin đã thay đổi với 79 bổ sung7 xóa
  1. 10 7
      osex/__init__.py
  2. 69 0
      tests/test_append_to_name.py

+ 10 - 7
osex/__init__.py

@@ -1,6 +1,15 @@
 import os
 import sys
 
+def append_to_name(source_path, suffix):
+    dest_path_prefix = source_path + suffix
+    dest_path = dest_path_prefix
+    dest_index = 0
+    while os.path.lexists(dest_path):
+        dest_index += 1
+        dest_path = dest_path_prefix + str(dest_index)
+    os.rename(source_path, dest_path)
+
 def symlink(source, link_name, relative = False, override = False, backup = True, backup_suffix = "~"):
     if relative:
         source = os.path.relpath(source, os.path.dirname(link_name))
@@ -8,13 +17,7 @@ def symlink(source, link_name, relative = False, override = False, backup = True
     # True for broken symbolic links. 
     if override and os.path.lexists(link_name):
         if backup:
-            backup_name_prefix = link_name + backup_suffix
-            backup_name = backup_name_prefix
-            backup_index = 0
-            while os.path.lexists(backup_name):
-                backup_index += 1
-                backup_name = backup_name_prefix + str(backup_index)
-            os.rename(link_name, backup_name)
+            append_to_name(link_name, backup_suffix)
         else:
             if os.path.isdir(link_name):
                 os.rmdir(link_name)

+ 69 - 0
tests/test_append_to_name.py

@@ -0,0 +1,69 @@
+import pytest
+
+import osex
+
+import os
+
+def test_single_file(tmpdir):
+    os.chdir(tmpdir.strpath)
+    os.mknod("file")
+    assert os.path.exists("file")
+    osex.append_to_name("file", "-") 
+    assert not os.path.exists("file")
+    assert os.path.exists("file-")
+
+def test_single_dir(tmpdir):
+    os.chdir(tmpdir.strpath)
+    os.mkdir("dir")
+    assert os.path.exists("dir")
+    osex.append_to_name("dir", "+") 
+    assert not os.path.exists("dir")
+    assert os.path.exists("dir+")
+
+def test_multiple_files(tmpdir):
+    os.chdir(tmpdir.strpath)
+    # file 1
+    os.mknod("file")
+    assert os.path.exists("file")
+    osex.append_to_name("file", "-") 
+    assert not os.path.exists("file")
+    assert os.path.exists("file-")
+    # file 2
+    os.mknod("file")
+    assert os.path.exists("file")
+    osex.append_to_name("file", "-") 
+    assert not os.path.exists("file")
+    assert os.path.exists("file-")
+    assert os.path.exists("file-1")
+    # file 3
+    os.mknod("file")
+    assert os.path.exists("file")
+    osex.append_to_name("file", "-") 
+    assert not os.path.exists("file")
+    assert os.path.exists("file-")
+    assert os.path.exists("file-1")
+    assert os.path.exists("file-2")
+
+def test_empty_suffix(tmpdir):
+    os.chdir(tmpdir.strpath)
+    # file 1
+    os.mknod("file")
+    assert os.path.exists("file")
+    osex.append_to_name("file", "") 
+    assert not os.path.exists("file")
+    assert os.path.exists("file1")
+    # file 2
+    os.mknod("file")
+    assert os.path.exists("file")
+    osex.append_to_name("file", "") 
+    assert not os.path.exists("file")
+    assert os.path.exists("file1")
+    assert os.path.exists("file2")
+    # file 3
+    os.mknod("file")
+    assert os.path.exists("file")
+    osex.append_to_name("file", "") 
+    assert not os.path.exists("file")
+    assert os.path.exists("file1")
+    assert os.path.exists("file2")
+    assert os.path.exists("file3")