test_symlink.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import pytest
  2. import osex
  3. import os
  4. def test_new(tmpdir):
  5. os.chdir(tmpdir.strpath)
  6. osex.symlink("source", "link")
  7. assert os.path.lexists("link")
  8. assert os.readlink("link") == "source"
  9. def test_absolute1(tmpdir):
  10. os.chdir(tmpdir.strpath)
  11. os.makedirs(os.path.join("1", "2"))
  12. link = os.path.join("1", "2", "link")
  13. source = os.path.join(os.getcwd(), "1", "source")
  14. osex.symlink(source, link)
  15. assert os.path.lexists(link)
  16. assert os.readlink(link) == source
  17. def test_absolute2(tmpdir):
  18. os.chdir(tmpdir.strpath)
  19. os.makedirs(os.path.join("1", "2"))
  20. link = os.path.join("1", "2", "link")
  21. source = os.path.join(os.getcwd(), "1", "source")
  22. osex.symlink(source, link, relative = False)
  23. assert os.path.lexists(link)
  24. assert os.readlink(link) == source
  25. def test_relative(tmpdir):
  26. os.chdir(tmpdir.strpath)
  27. os.makedirs(os.path.join("1", "2"))
  28. link = os.path.join("1", "2", "link")
  29. source = os.path.join(os.getcwd(), "1", "source")
  30. osex.symlink(source, link, relative = True)
  31. assert os.path.lexists(link)
  32. assert os.readlink(link) == "../source"
  33. def test_suitable_link_exists(tmpdir):
  34. os.chdir(tmpdir.strpath)
  35. osex.symlink("source", "link")
  36. assert os.path.lexists("link")
  37. osex.symlink("source", "link")
  38. osex.symlink("source", "link", override = False)
  39. assert os.listdir(tmpdir.strpath) == ["link"]
  40. def test_override_suitable_link(tmpdir):
  41. os.chdir(tmpdir.strpath)
  42. osex.symlink("source", "link")
  43. assert os.path.lexists("link")
  44. osex.symlink("source", "link", override = True, backup = True)
  45. # no backup should have been created since the existing link
  46. # already had the proper target
  47. assert os.listdir(tmpdir.strpath) == ["link"]
  48. def test_divergent_link_exists(tmpdir):
  49. os.chdir(tmpdir.strpath)
  50. osex.symlink("source1", "link")
  51. assert os.path.lexists("link")
  52. with pytest.raises(OSError):
  53. osex.symlink("source2", "link")
  54. with pytest.raises(OSError):
  55. osex.symlink("source3", "link", override = False)
  56. def test_dir_exists(tmpdir):
  57. os.chdir(tmpdir.strpath)
  58. os.mkdir("dir")
  59. assert os.path.exists("dir")
  60. with pytest.raises(OSError):
  61. osex.symlink("source", "dir")
  62. with pytest.raises(OSError):
  63. osex.symlink("source", "dir", override = False)
  64. def test_override_link(tmpdir):
  65. os.chdir(tmpdir.strpath)
  66. osex.symlink("source1", "link")
  67. assert os.path.lexists("link")
  68. osex.symlink("source2", "link", override = True, backup = False, backup_suffix = "~")
  69. assert os.path.lexists("link")
  70. assert os.readlink("link") == "source2"
  71. assert not os.path.lexists("link~")
  72. def test_override_dir(tmpdir):
  73. os.chdir(tmpdir.strpath)
  74. os.mkdir("link")
  75. assert os.path.exists("link")
  76. osex.symlink("source", "link", override = True, backup = False, backup_suffix = "-")
  77. assert os.path.lexists("link")
  78. assert os.readlink("link") == "source"
  79. assert not os.path.isdir("link-")
  80. def test_backup_link(tmpdir):
  81. os.chdir(tmpdir.strpath)
  82. osex.symlink("source1", "link")
  83. assert os.path.lexists("link")
  84. osex.symlink("source2", "link", override = True, backup = True, backup_suffix = "~")
  85. assert os.path.lexists("link")
  86. assert os.readlink("link") == "source2"
  87. assert os.path.lexists("link~")
  88. assert os.readlink("link~") == "source1"
  89. def test_backup_dir(tmpdir):
  90. os.chdir(tmpdir.strpath)
  91. os.mkdir("link")
  92. assert os.path.exists("link")
  93. osex.symlink("source", "link", override = True, backup = True, backup_suffix = "-")
  94. assert os.path.lexists("link")
  95. assert os.readlink("link") == "source"
  96. assert os.path.isdir("link-")
  97. def test_backup_multiple_links(tmpdir):
  98. os.chdir(tmpdir.strpath)
  99. # source 1
  100. osex.symlink("source1", "link")
  101. assert os.path.lexists("link")
  102. # source 2
  103. osex.symlink("source2", "link", override = True, backup = True, backup_suffix = "~")
  104. assert os.path.lexists("link")
  105. assert os.readlink("link") == "source2"
  106. assert os.path.lexists("link~")
  107. assert os.readlink("link~") == "source1"
  108. # source 3
  109. osex.symlink("source3", "link", override = True, backup = True, backup_suffix = "~")
  110. assert os.path.lexists("link")
  111. assert os.readlink("link") == "source3"
  112. assert os.path.lexists("link~")
  113. assert os.readlink("link~") == "source1"
  114. assert os.path.lexists("link~1")
  115. assert os.readlink("link~1") == "source2"
  116. # source 4
  117. osex.symlink("source4", "link", override = True, backup = True, backup_suffix = "~")
  118. assert os.path.lexists("link")
  119. assert os.readlink("link") == "source4"
  120. assert os.path.lexists("link~")
  121. assert os.readlink("link~") == "source1"
  122. assert os.path.lexists("link~1")
  123. assert os.readlink("link~1") == "source2"
  124. assert os.path.lexists("link~2")
  125. assert os.readlink("link~2") == "source3"