test_renames.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import pytest
  2. import osex
  3. import os
  4. def test_rename(tmpdir):
  5. os.chdir(tmpdir.strpath)
  6. open('a', 'w').close()
  7. assert os.path.exists('a')
  8. osex.renames('a', 'b')
  9. assert not os.path.exists('a')
  10. assert os.path.exists('b')
  11. def test_prune_source_dirs(tmpdir):
  12. os.chdir(tmpdir.strpath)
  13. source_dir_path = os.path.join('1', '2')
  14. os.makedirs(source_dir_path)
  15. source_file_path = os.path.join(source_dir_path, 'a')
  16. open(source_file_path, 'w').close()
  17. assert os.path.isdir(source_dir_path)
  18. assert os.path.exists(source_file_path)
  19. osex.renames('1/2/a', 'b')
  20. assert os.listdir(tmpdir.strpath) == ['b']
  21. def test_create_destination_dirs(tmpdir):
  22. os.chdir(tmpdir.strpath)
  23. open('a', 'w').close()
  24. assert os.listdir(tmpdir.strpath) == ['a']
  25. osex.renames('a', '1/2/b')
  26. assert os.listdir(tmpdir.strpath) == ['1']
  27. assert os.path.isdir('1/2')
  28. assert os.path.isfile('1/2/b')
  29. def test_file_exists(tmpdir):
  30. os.chdir(tmpdir.strpath)
  31. open('src', 'w').close()
  32. open('dest', 'w').close()
  33. assert os.path.isfile('src')
  34. assert os.path.isfile('dest')
  35. with pytest.raises(OSError):
  36. osex.renames('src', 'dest', override = False, backup = True)
  37. assert os.path.isfile('src')
  38. assert os.path.isfile('dest')
  39. assert len(os.listdir(tmpdir.strpath)) == 2
  40. def test_backup_file(tmpdir):
  41. os.chdir(tmpdir.strpath)
  42. open('src', 'w').close()
  43. open('dest', 'w').close()
  44. assert os.path.isfile('src')
  45. assert os.path.isfile('dest')
  46. osex.renames('src', 'dest', override = True, backup = True, backup_suffix = "~")
  47. assert not os.path.isfile('src')
  48. assert os.path.isfile('dest')
  49. assert os.path.isfile('dest~')
  50. assert len(os.listdir(tmpdir.strpath)) == 2
  51. def test_backup_directory(tmpdir):
  52. os.chdir(tmpdir.strpath)
  53. open('src', 'w').close()
  54. os.mkdir('dest')
  55. assert os.path.isfile('src')
  56. assert os.path.isdir('dest')
  57. osex.renames('src', 'dest', override = True, backup = True, backup_suffix = "~")
  58. assert not os.path.isfile('src')
  59. assert os.path.isfile('dest')
  60. assert os.path.isdir('dest~')
  61. assert len(os.listdir(tmpdir.strpath)) == 2
  62. def test_backup_directory_symlink(tmpdir):
  63. os.chdir(tmpdir.strpath)
  64. open('src', 'w').close()
  65. os.mkdir('dir')
  66. os.symlink('dir', 'dest')
  67. assert os.path.isfile('src')
  68. assert os.path.isdir('dir')
  69. assert os.path.islink('dest')
  70. assert os.path.isdir('dest')
  71. osex.renames('src', 'dest', override = True, backup = True, backup_suffix = "~")
  72. assert not os.path.isfile('src')
  73. assert os.path.isdir('dir')
  74. assert os.path.islink('dest~')
  75. assert os.path.isdir('dest~')
  76. assert os.path.isfile('dest')
  77. assert len(os.listdir(tmpdir.strpath)) == 3
  78. def test_override_file(tmpdir):
  79. os.chdir(tmpdir.strpath)
  80. open('src', 'w').close()
  81. open('dest', 'w').close()
  82. assert os.path.isfile('src')
  83. assert os.path.isfile('dest')
  84. osex.renames('src', 'dest', override = True, backup = False)
  85. assert not os.path.isfile('src')
  86. assert os.path.isfile('dest')
  87. assert len(os.listdir(tmpdir.strpath)) == 1
  88. def test_attempt_override_directory(tmpdir):
  89. os.chdir(tmpdir.strpath)
  90. open('src', 'w').close()
  91. os.mkdir('dest')
  92. assert os.path.isfile('src')
  93. assert os.path.isdir('dest')
  94. with pytest.raises(OSError):
  95. osex.renames('src', 'dest', override = True, backup = False)
  96. def test_override_directory_symlink(tmpdir):
  97. os.chdir(tmpdir.strpath)
  98. open('src', 'w').close()
  99. os.mkdir('dir')
  100. os.symlink('dir', 'link')
  101. assert os.path.isfile('src')
  102. assert os.path.isdir('dir')
  103. assert os.path.islink('link')
  104. assert os.path.isdir('link')
  105. osex.renames('src', 'link', override = True, backup = False)
  106. assert not os.path.isfile('src')
  107. assert os.path.isdir('dir')
  108. assert not os.path.islink('link')
  109. assert os.path.isfile('link')
  110. assert len(os.listdir(tmpdir.strpath)) == 2