test_symlink.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. def test_divergent_link_exists(tmpdir):
  40. os.chdir(tmpdir.strpath)
  41. osex.symlink("source1", "link")
  42. assert os.path.lexists("link")
  43. with pytest.raises(OSError):
  44. osex.symlink("source2", "link")
  45. with pytest.raises(OSError):
  46. osex.symlink("source3", "link", override = False)
  47. def test_dir_exists(tmpdir):
  48. os.chdir(tmpdir.strpath)
  49. os.mkdir("dir")
  50. assert os.path.exists("dir")
  51. with pytest.raises(OSError):
  52. osex.symlink("source", "dir")
  53. with pytest.raises(OSError):
  54. osex.symlink("source", "dir", override = False)
  55. def test_override_link(tmpdir):
  56. os.chdir(tmpdir.strpath)
  57. osex.symlink("source1", "link")
  58. assert os.path.lexists("link")
  59. osex.symlink("source2", "link", override = True, backup = False, backup_suffix = "~")
  60. assert os.path.lexists("link")
  61. assert os.readlink("link") == "source2"
  62. assert not os.path.lexists("link~")
  63. def test_override_dir(tmpdir):
  64. os.chdir(tmpdir.strpath)
  65. os.mkdir("link")
  66. assert os.path.exists("link")
  67. osex.symlink("source", "link", override = True, backup = False, backup_suffix = "-")
  68. assert os.path.lexists("link")
  69. assert os.readlink("link") == "source"
  70. assert not os.path.isdir("link-")
  71. def test_backup_link(tmpdir):
  72. os.chdir(tmpdir.strpath)
  73. osex.symlink("source1", "link")
  74. assert os.path.lexists("link")
  75. osex.symlink("source2", "link", override = True, backup = True, backup_suffix = "~")
  76. assert os.path.lexists("link")
  77. assert os.readlink("link") == "source2"
  78. assert os.path.lexists("link~")
  79. assert os.readlink("link~") == "source1"
  80. def test_backup_dir(tmpdir):
  81. os.chdir(tmpdir.strpath)
  82. os.mkdir("link")
  83. assert os.path.exists("link")
  84. osex.symlink("source", "link", override = True, backup = True, backup_suffix = "-")
  85. assert os.path.lexists("link")
  86. assert os.readlink("link") == "source"
  87. assert os.path.isdir("link-")