|
1 | 1 | # Test suite for the `rotate-backups' Python package. |
2 | 2 | # |
3 | 3 | # Author: Peter Odding <peter@peterodding.com> |
4 | | -# Last Change: August 3, 2018 |
| 4 | +# Last Change: February 11, 2020 |
5 | 5 | # URL: https://github.com/xolox/python-rotate-backups |
6 | 6 |
|
7 | 7 | """Test suite for the `rotate-backups` package.""" |
8 | 8 |
|
9 | 9 | # Standard library modules. |
| 10 | +import contextlib |
10 | 11 | import datetime |
11 | 12 | import logging |
12 | 13 | import os |
@@ -392,6 +393,27 @@ def test_removal_command(self): |
392 | 393 | commands = program.rotate_backups(root, prepare=True) |
393 | 394 | assert any(cmd.command_line[0] == 'rmdir' for cmd in commands) |
394 | 395 |
|
| 396 | + def test_ensure_writable(self): |
| 397 | + """Test that ensure_writable() complains when the location isn't writable.""" |
| 398 | + with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root: |
| 399 | + for date in '2019-03-05', '2019-03-06': |
| 400 | + os.mkdir(os.path.join(root, date)) |
| 401 | + with readonly_directory(root): |
| 402 | + program = RotateBackups(rotation_scheme=dict(monthly='always')) |
| 403 | + self.assertRaises(ValueError, program.rotate_backups, root) |
| 404 | + |
| 405 | + def test_ensure_writable_optional(self): |
| 406 | + """Test that ensure_writable() isn't called when a custom removal command is used.""" |
| 407 | + with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root: |
| 408 | + for date in '2019-03-05', '2019-03-06': |
| 409 | + os.mkdir(os.path.join(root, date)) |
| 410 | + with readonly_directory(root): |
| 411 | + program = RotateBackups( |
| 412 | + removal_command=['echo', 'Deleting'], |
| 413 | + rotation_scheme=dict(monthly='always'), |
| 414 | + ) |
| 415 | + program.rotate_backups(root) |
| 416 | + |
395 | 417 | def test_filename_patterns(self): |
396 | 418 | """Test support for filename patterns in configuration files.""" |
397 | 419 | with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root: |
@@ -424,3 +446,11 @@ def create_sample_backup_set(self, root): |
424 | 446 | """Create a sample backup set to be rotated.""" |
425 | 447 | for name in SAMPLE_BACKUP_SET: |
426 | 448 | os.mkdir(os.path.join(root, name)) |
| 449 | + |
| 450 | + |
| 451 | +@contextlib.contextmanager |
| 452 | +def readonly_directory(pathname): |
| 453 | + """Context manager to temporarily make something read only.""" |
| 454 | + os.chmod(pathname, 0o555) |
| 455 | + yield |
| 456 | + os.chmod(pathname, 0o775) |
0 commit comments