|
| 1 | +import importlib |
| 2 | +import logging |
| 3 | +from _common import run_tests |
| 4 | + |
| 5 | + |
| 6 | +def test_migration_001_happy_path(tmp_path, monkeypatch): |
| 7 | + """Test standard migration where old exists and new does not.""" |
| 8 | + mig = importlib.import_module("timetagger.migrations.001_datadir_default_xdg") |
| 9 | + |
| 10 | + old_dir = tmp_path / "old" / "_timetagger" |
| 11 | + new_dir = tmp_path / "new" / "timetagger" |
| 12 | + |
| 13 | + monkeypatch.setattr(mig, "get_old_default_dir", lambda: old_dir) |
| 14 | + monkeypatch.setattr(mig, "get_new_default_dir", lambda: new_dir) |
| 15 | + |
| 16 | + old_dir.mkdir(parents=True) |
| 17 | + (old_dir / "jwt.key").write_text("fake key") |
| 18 | + |
| 19 | + mig.run(str(new_dir)) |
| 20 | + |
| 21 | + assert not old_dir.exists() |
| 22 | + assert new_dir.exists() |
| 23 | + assert (new_dir / "jwt.key").read_text() == "fake key" |
| 24 | + |
| 25 | + |
| 26 | +def test_migration_001_custom_path_skipped(tmp_path, monkeypatch): |
| 27 | + """Test that custom config dirs cause the migration to safely abort.""" |
| 28 | + mig = importlib.import_module("timetagger.migrations.001_datadir_default_xdg") |
| 29 | + |
| 30 | + old_dir = tmp_path / "old" / "_timetagger" |
| 31 | + new_dir = tmp_path / "new" / "timetagger" |
| 32 | + custom_dir = tmp_path / "custom" / "timetagger" |
| 33 | + |
| 34 | + monkeypatch.setattr(mig, "get_old_default_dir", lambda: old_dir) |
| 35 | + monkeypatch.setattr(mig, "get_new_default_dir", lambda: new_dir) |
| 36 | + |
| 37 | + old_dir.mkdir(parents=True) |
| 38 | + mig.run(str(custom_dir)) |
| 39 | + |
| 40 | + assert old_dir.exists() |
| 41 | + assert not new_dir.exists() |
| 42 | + |
| 43 | + |
| 44 | +def test_migration_001_conflict_aborts(tmp_path, monkeypatch, caplog): |
| 45 | + """Test that migration aborts if BOTH dirs exist and new has files.""" |
| 46 | + mig = importlib.import_module("timetagger.migrations.001_datadir_default_xdg") |
| 47 | + |
| 48 | + old_dir = tmp_path / "old" / "_timetagger" |
| 49 | + new_dir = tmp_path / "new" / "timetagger" |
| 50 | + |
| 51 | + monkeypatch.setattr(mig, "get_old_default_dir", lambda: old_dir) |
| 52 | + monkeypatch.setattr(mig, "get_new_default_dir", lambda: new_dir) |
| 53 | + |
| 54 | + old_dir.mkdir(parents=True) |
| 55 | + new_dir.mkdir(parents=True) |
| 56 | + (new_dir / "jwt.key").touch() |
| 57 | + |
| 58 | + with caplog.at_level(logging.WARNING): |
| 59 | + mig.run(str(new_dir)) |
| 60 | + |
| 61 | + assert old_dir.exists() |
| 62 | + assert new_dir.exists() |
| 63 | + assert "Skipping automatic migration" in caplog.text |
| 64 | + |
| 65 | + |
| 66 | +def test_migration_001_new_empty_proceeds(tmp_path, monkeypatch): |
| 67 | + """Test that an accidentally created EMPTY new dir is removed safely to allow move.""" |
| 68 | + mig = importlib.import_module("timetagger.migrations.001_datadir_default_xdg") |
| 69 | + |
| 70 | + old_dir = tmp_path / "old" / "_timetagger" |
| 71 | + new_dir = tmp_path / "new" / "timetagger" |
| 72 | + |
| 73 | + monkeypatch.setattr(mig, "get_old_default_dir", lambda: old_dir) |
| 74 | + monkeypatch.setattr(mig, "get_new_default_dir", lambda: new_dir) |
| 75 | + |
| 76 | + old_dir.mkdir(parents=True) |
| 77 | + (old_dir / "jwt.key").touch() |
| 78 | + new_dir.mkdir(parents=True) |
| 79 | + |
| 80 | + mig.run(str(new_dir)) |
| 81 | + |
| 82 | + assert not old_dir.exists() |
| 83 | + assert (new_dir / "jwt.key").exists() |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + run_tests(globals()) |
0 commit comments