test_daklib_fstransactions.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #! /usr/bin/env python
  2. #
  3. # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. from __future__ import print_function
  19. from base_test import DakTestCase
  20. from daklib.fstransactions import FilesystemTransaction
  21. from unittest import main
  22. import os
  23. import shutil
  24. import tempfile
  25. class TemporaryDirectory:
  26. def __init__(self):
  27. self.directory = None
  28. def __str__(self):
  29. return self.directory
  30. def filename(self, suffix):
  31. return os.path.join(self.directory, suffix)
  32. def __enter__(self):
  33. self.directory = tempfile.mkdtemp()
  34. return self
  35. def __exit__(self, *args):
  36. if self.directory is not None:
  37. shutil.rmtree(self.directory)
  38. self.directory = None
  39. return None
  40. class FilesystemTransactionTestCase(DakTestCase):
  41. def _copy_a_b(self, tmp, fs, **kwargs):
  42. fs.copy(tmp.filename('a'), tmp.filename('b'), **kwargs)
  43. def _write_to_a(self, tmp):
  44. with open(tmp.filename('a'), 'w') as fh:
  45. print('a', file=fh)
  46. def test_copy_non_existing(self):
  47. def copy():
  48. with TemporaryDirectory() as t:
  49. with FilesystemTransaction() as fs:
  50. self._copy_a_b(t, fs)
  51. self.assertRaises(IOError, copy)
  52. def test_copy_existing_and_commit(self):
  53. with TemporaryDirectory() as t:
  54. self._write_to_a(t)
  55. with FilesystemTransaction() as fs:
  56. self._copy_a_b(t, fs)
  57. self.assert_(os.path.exists(t.filename('a')))
  58. self.assert_(os.path.exists(t.filename('b')))
  59. self.assert_(os.path.exists(t.filename('a')))
  60. self.assert_(os.path.exists(t.filename('b')))
  61. def test_copy_existing_and_rollback(self):
  62. with TemporaryDirectory() as t:
  63. self._write_to_a(t)
  64. class TestException(Exception):
  65. pass
  66. try:
  67. with FilesystemTransaction() as fs:
  68. self._copy_a_b(t, fs)
  69. self.assert_(os.path.exists(t.filename('a')))
  70. self.assert_(os.path.exists(t.filename('b')))
  71. raise TestException()
  72. except TestException:
  73. pass
  74. self.assert_(os.path.exists(t.filename('a')))
  75. self.assert_(not os.path.exists(t.filename('b')))
  76. def test_unlink_and_commit(self):
  77. with TemporaryDirectory() as t:
  78. self._write_to_a(t)
  79. a = t.filename('a')
  80. with FilesystemTransaction() as fs:
  81. self.assert_(os.path.exists(a))
  82. fs.unlink(a)
  83. self.assert_(not os.path.exists(a))
  84. self.assert_(not os.path.exists(a))
  85. def test_unlink_and_rollback(self):
  86. with TemporaryDirectory() as t:
  87. self._write_to_a(t)
  88. a = t.filename('a')
  89. class TestException(Exception):
  90. pass
  91. try:
  92. with FilesystemTransaction() as fs:
  93. self.assert_(os.path.exists(a))
  94. fs.unlink(a)
  95. self.assert_(not os.path.exists(a))
  96. raise TestException()
  97. except TestException:
  98. pass
  99. self.assert_(os.path.exists(a))
  100. if __name__ == '__main__':
  101. main()