test_symlink.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(tmpdir):
  56. os.chdir(tmpdir.strpath)
  57. osex.symlink("source1", "link")
  58. assert os.path.lexists("link")
  59. osex.symlink("source2", "link", override = True)
  60. assert os.path.lexists("link")
  61. assert os.readlink("link") == "source2"