|
| 1 | +import pathlib |
| 2 | +import subprocess |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +EXPECTED_FILES = [ |
| 8 | + "2024-06-27.tar.gz", |
| 9 | + "2024-06-28.tar.gz", |
| 10 | + "2024-06-29.tar.gz", |
| 11 | + "2024-06-30.tar.gz", |
| 12 | + "2024-07-01.tar.gz", |
| 13 | + "2024-07-02.tar.gz", |
| 14 | + "2024-07-03.tar.gz", |
| 15 | + "2024-07-08.tar.gz", |
| 16 | + "2024-07-09.tar.gz", |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def backups(tmp_path: pathlib.Path): |
| 22 | + for filename in EXPECTED_FILES: |
| 23 | + (tmp_path / filename).write_text(filename) |
| 24 | + |
| 25 | + assert sorted(item.name for item in tmp_path.glob("*")) == EXPECTED_FILES |
| 26 | + return tmp_path |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def no_backups(tmp_path: pathlib.Path): |
| 31 | + assert sorted(item.name for item in tmp_path.glob("*")) == [] |
| 32 | + return tmp_path |
| 33 | + |
| 34 | + |
| 35 | +def test_normal_invocation(backups: pathlib.Path): |
| 36 | + subprocess.check_call( |
| 37 | + ["rotate.py", "--no-dry-run", "--keep", "3", "--dir", backups] |
| 38 | + ) |
| 39 | + assert sorted(item.name for item in backups.glob("*")) == [ |
| 40 | + "2024-07-03.tar.gz", |
| 41 | + "2024-07-08.tar.gz", |
| 42 | + "2024-07-09.tar.gz", |
| 43 | + ] |
| 44 | + |
| 45 | + |
| 46 | +def test_normal_invocation_is_idempotent(backups: pathlib.Path): |
| 47 | + subprocess.check_call( |
| 48 | + ["rotate.py", "--no-dry-run", "--keep", "3", "--dir", backups] |
| 49 | + ) |
| 50 | + subprocess.check_call( |
| 51 | + ["rotate.py", "--no-dry-run", "--keep", "3", "--dir", backups] |
| 52 | + ) |
| 53 | + subprocess.check_call( |
| 54 | + ["rotate.py", "--no-dry-run", "--keep", "3", "--dir", backups] |
| 55 | + ) |
| 56 | + assert sorted(item.name for item in backups.glob("*")) == [ |
| 57 | + "2024-07-03.tar.gz", |
| 58 | + "2024-07-08.tar.gz", |
| 59 | + "2024-07-09.tar.gz", |
| 60 | + ] |
| 61 | + |
| 62 | + |
| 63 | +def test_dry_run_invocation(backups: pathlib.Path): |
| 64 | + subprocess.check_call(["rotate.py", "--keep", "3", "--dir", backups]) |
| 65 | + assert sorted(item.name for item in backups.glob("*")) == EXPECTED_FILES |
| 66 | + |
| 67 | + |
| 68 | +def test_keep_zero(backups: pathlib.Path): |
| 69 | + subprocess.check_call( |
| 70 | + ["rotate.py", "--no-dry-run", "--keep", "0", "--dir", backups] |
| 71 | + ) |
| 72 | + assert sorted(item.name for item in backups.glob("*")) == [] |
| 73 | + |
| 74 | + |
| 75 | +def test_keep_negative(backups: pathlib.Path): |
| 76 | + with pytest.raises(subprocess.CalledProcessError): |
| 77 | + subprocess.check_call( |
| 78 | + ["rotate.py", "--no-dry-run", "--keep", "-1", "--dir", backups] |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def test_keep_more_than_files(backups: pathlib.Path): |
| 83 | + subprocess.check_call( |
| 84 | + ["rotate.py", "--no-dry-run", "--keep", "100", "--dir", backups] |
| 85 | + ) |
| 86 | + assert sorted(item.name for item in backups.glob("*")) == EXPECTED_FILES |
| 87 | + |
| 88 | + |
| 89 | +def test_keep_pattern(backups): |
| 90 | + subprocess.check_call( |
| 91 | + [ |
| 92 | + "rotate.py", |
| 93 | + "--no-dry-run", |
| 94 | + "--keep", |
| 95 | + "1", |
| 96 | + "--dir", |
| 97 | + backups, |
| 98 | + "--pattern", |
| 99 | + "2024-06*tar.gz", |
| 100 | + ] |
| 101 | + ) |
| 102 | + assert sorted(item.name for item in backups.glob("*")) == [ |
| 103 | + "2024-06-30.tar.gz", |
| 104 | + "2024-07-01.tar.gz", |
| 105 | + "2024-07-02.tar.gz", |
| 106 | + "2024-07-03.tar.gz", |
| 107 | + "2024-07-08.tar.gz", |
| 108 | + "2024-07-09.tar.gz", |
| 109 | + ] |
| 110 | + |
| 111 | + |
| 112 | +def test_keep_pattern_does_not_match_anything(backups): |
| 113 | + subprocess.check_call( |
| 114 | + [ |
| 115 | + "rotate.py", |
| 116 | + "--no-dry-run", |
| 117 | + "--keep", |
| 118 | + "1", |
| 119 | + "--dir", |
| 120 | + backups, |
| 121 | + "--pattern", |
| 122 | + "2024-08*tar.gz", |
| 123 | + ] |
| 124 | + ) |
| 125 | + assert sorted(item.name for item in backups.glob("*")) == EXPECTED_FILES |
0 commit comments