test_symlink_script.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import pytest
  2. import os
  3. import subprocess
  4. def test_new(tmpdir):
  5. os.chdir(tmpdir.strpath)
  6. subprocess.check_call(["symlink", "source", "link"])
  7. assert os.path.lexists("link")
  8. assert os.readlink("link") == "source"
  9. def test_absolute(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. subprocess.check_call(["symlink", source, link])
  15. assert os.path.lexists(link)
  16. assert os.readlink(link) == source
  17. def test_relative(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. subprocess.check_call(["symlink", source, link, "--relative"])
  23. assert os.path.lexists(link)
  24. assert os.readlink(link) == "../source"
  25. def test_suitable_link_exists(tmpdir):
  26. os.chdir(tmpdir.strpath)
  27. subprocess.check_call(["symlink", "source", "link"])
  28. assert os.path.lexists("link")
  29. subprocess.check_call(["symlink", "source", "link"])
  30. def test_override_suitable_link(tmpdir, **kwargs):
  31. os.chdir(tmpdir.strpath)
  32. subprocess.check_call(["symlink", "source", "link"])
  33. assert os.path.lexists("link")
  34. subprocess.check_call(["symlink", "--override", "source", "link"])
  35. # no backup should have been created since the existing link
  36. # already had the proper target
  37. assert os.listdir(tmpdir.strpath) == ["link"]
  38. def test_divergent_link_exists(tmpdir):
  39. os.chdir(tmpdir.strpath)
  40. subprocess.check_call(["symlink", "source", "link"])
  41. assert os.path.lexists("link")
  42. with pytest.raises(subprocess.CalledProcessError):
  43. subprocess.check_call(["symlink", "source2", "link"])
  44. def test_dir_exists(tmpdir):
  45. os.chdir(tmpdir.strpath)
  46. os.mkdir("dir")
  47. assert os.path.exists("dir")
  48. with pytest.raises(subprocess.CalledProcessError):
  49. subprocess.check_call(["symlink", "source", "dir"])
  50. def test_override_link(tmpdir):
  51. os.chdir(tmpdir.strpath)
  52. subprocess.check_call(["symlink", "source1", "link"])
  53. assert os.path.lexists("link")
  54. subprocess.check_call(["symlink", "source2", "link", "--override", "--no-backup"])
  55. assert os.path.lexists("link")
  56. assert os.readlink("link") == "source2"
  57. assert not os.path.lexists("link~")
  58. def test_override_dir(tmpdir):
  59. os.chdir(tmpdir.strpath)
  60. os.mkdir("link")
  61. assert os.path.exists("link")
  62. subprocess.check_call(["symlink", "source", "link", "--override", "--no-backup"])
  63. assert os.path.lexists("link")
  64. assert os.readlink("link") == "source"
  65. assert not os.path.isdir("link-")
  66. def test_backup_link(tmpdir):
  67. os.chdir(tmpdir.strpath)
  68. subprocess.check_call(["symlink", "source1", "link"])
  69. assert os.path.lexists("link")
  70. subprocess.check_call(["symlink", "source2", "link", "--override", "--backup-suffix", "~"])
  71. assert os.path.lexists("link")
  72. assert os.readlink("link") == "source2"
  73. assert os.path.lexists("link~")
  74. assert os.readlink("link~") == "source1"
  75. def test_backup_dir(tmpdir):
  76. os.chdir(tmpdir.strpath)
  77. os.mkdir("link")
  78. assert os.path.exists("link")
  79. subprocess.check_call(["symlink", "source", "link", "--override", "--backup-suffix", "-"])
  80. assert os.path.lexists("link")
  81. assert os.readlink("link") == "source"
  82. assert os.path.isdir("link-")
  83. def test_backup_multiple_links(tmpdir):
  84. os.chdir(tmpdir.strpath)
  85. # source 1
  86. subprocess.check_call(["symlink", "source1", "link"])
  87. assert os.path.lexists("link")
  88. # source 2
  89. subprocess.check_call(["symlink", "source2", "link", "--override", "--backup-suffix", "~"])
  90. assert os.path.lexists("link")
  91. assert os.readlink("link") == "source2"
  92. assert os.path.lexists("link~")
  93. assert os.readlink("link~") == "source1"
  94. # source 3
  95. subprocess.check_call(["symlink", "source3", "link", "--override", "--backup-suffix", "~"])
  96. assert os.path.lexists("link")
  97. assert os.readlink("link") == "source3"
  98. assert os.path.lexists("link~")
  99. assert os.readlink("link~") == "source1"
  100. assert os.path.lexists("link~1")
  101. assert os.readlink("link~1") == "source2"
  102. # source 4
  103. subprocess.check_call(["symlink", "source4", "link", "--override", "--backup-suffix", "~"])
  104. assert os.path.lexists("link")
  105. assert os.readlink("link") == "source4"
  106. assert os.path.lexists("link~")
  107. assert os.readlink("link~") == "source1"
  108. assert os.path.lexists("link~1")
  109. assert os.readlink("link~1") == "source2"
  110. assert os.path.lexists("link~2")
  111. assert os.readlink("link~2") == "source3"