Browse Source

symlink: do not attempt to create symlink if link with expected source already exists

Fabian Peter Hammerle 8 years ago
parent
commit
f7f8d82bdb
2 changed files with 10 additions and 2 deletions
  1. 2 1
      osex/__init__.py
  2. 8 1
      tests/test_symlink.py

+ 2 - 1
osex/__init__.py

@@ -7,4 +7,5 @@ def symlink(source, link_name, relative = False, override = False):
     # True for broken symbolic links. 
     if override and os.path.lexists(link_name):
         os.remove(link_name)
-    os.symlink(source, link_name)
+    if not os.path.lexists(link_name) or os.readlink(link_name) != source:
+        os.symlink(source, link_name)

+ 8 - 1
tests/test_symlink.py

@@ -37,7 +37,14 @@ def test_relative(tmpdir):
     assert os.path.lexists(link)
     assert os.readlink(link) == "../source"
 
-def test_link_exists(tmpdir):
+def test_suitable_link_exists(tmpdir):
+    os.chdir(tmpdir.strpath)
+    osex.symlink("source", "link")
+    assert os.path.lexists("link")
+    osex.symlink("source", "link")
+    osex.symlink("source", "link", override = False)
+
+def test_divergent_link_exists(tmpdir):
     os.chdir(tmpdir.strpath)
     osex.symlink("source1", "link")
     assert os.path.lexists("link")