|
| 1 | +import errno |
| 2 | +import shutil |
| 3 | +import sys |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import cibuildwheel.__main__ as main_module |
| 8 | +from cibuildwheel.__main__ import main |
| 9 | + |
| 10 | + |
| 11 | +def test_clean_cache_when_cache_exists(tmp_path, monkeypatch, capfd): |
| 12 | + fake_cache_dir = (tmp_path / "cibw_cache").resolve() |
| 13 | + monkeypatch.setattr(main_module, "CIBW_CACHE_PATH", fake_cache_dir) |
| 14 | + |
| 15 | + fake_cache_dir.mkdir(parents=True, exist_ok=True) |
| 16 | + assert fake_cache_dir.exists() |
| 17 | + |
| 18 | + cibw_sentinel = fake_cache_dir / "CACHEDIR.TAG" |
| 19 | + cibw_sentinel.write_text( |
| 20 | + "Signature: 8a477f597d28d172789f06886806bc55\n" |
| 21 | + "# This file is a cache directory tag created by cibuildwheel.\n" |
| 22 | + "# For information about cache directory tags, see:\n" |
| 23 | + "# https://www.brynosaurus.com/cachedir/", |
| 24 | + encoding="utf-8", |
| 25 | + ) |
| 26 | + |
| 27 | + dummy_file = fake_cache_dir / "dummy.txt" |
| 28 | + dummy_file.write_text("hello") |
| 29 | + |
| 30 | + monkeypatch.setattr(sys, "argv", ["cibuildwheel", "--clean-cache"]) |
| 31 | + |
| 32 | + with pytest.raises(SystemExit) as e: |
| 33 | + main() |
| 34 | + |
| 35 | + assert e.value.code == 0 |
| 36 | + |
| 37 | + out, err = capfd.readouterr() |
| 38 | + assert f"Clearing cache directory: {fake_cache_dir}" in out |
| 39 | + assert "Cache cleared successfully." in out |
| 40 | + assert not fake_cache_dir.exists() |
| 41 | + |
| 42 | + |
| 43 | +def test_clean_cache_when_cache_does_not_exist(tmp_path, monkeypatch, capfd): |
| 44 | + fake_cache_dir = (tmp_path / "nonexistent_cache").resolve() |
| 45 | + monkeypatch.setattr(main_module, "CIBW_CACHE_PATH", fake_cache_dir) |
| 46 | + |
| 47 | + monkeypatch.setattr(sys, "argv", ["cibuildwheel", "--clean-cache"]) |
| 48 | + |
| 49 | + with pytest.raises(SystemExit) as e: |
| 50 | + main() |
| 51 | + |
| 52 | + assert e.value.code == 0 |
| 53 | + |
| 54 | + out, err = capfd.readouterr() |
| 55 | + assert f"Cache directory does not exist: {fake_cache_dir}" in out |
| 56 | + |
| 57 | + |
| 58 | +def test_clean_cache_with_error(tmp_path, monkeypatch, capfd): |
| 59 | + fake_cache_dir = (tmp_path / "cibw_cache").resolve() |
| 60 | + monkeypatch.setattr(main_module, "CIBW_CACHE_PATH", fake_cache_dir) |
| 61 | + |
| 62 | + fake_cache_dir.mkdir(parents=True, exist_ok=True) |
| 63 | + assert fake_cache_dir.exists() |
| 64 | + |
| 65 | + cibw_sentinel = fake_cache_dir / "CACHEDIR.TAG" |
| 66 | + cibw_sentinel.write_text( |
| 67 | + "Signature: 8a477f597d28d172789f06886806bc55\n" |
| 68 | + "# This file is a cache directory tag created by cibuildwheel.\n" |
| 69 | + "# For information about cache directory tags, see:\n" |
| 70 | + "# https://www.brynosaurus.com/cachedir/", |
| 71 | + encoding="utf-8", |
| 72 | + ) |
| 73 | + |
| 74 | + monkeypatch.setattr(sys, "argv", ["cibuildwheel", "--clean-cache"]) |
| 75 | + |
| 76 | + def fake_rmtree(path): # noqa: ARG001 |
| 77 | + raise OSError(errno.EACCES, "Permission denied") |
| 78 | + |
| 79 | + monkeypatch.setattr(shutil, "rmtree", fake_rmtree) |
| 80 | + |
| 81 | + with pytest.raises(SystemExit) as e: |
| 82 | + main() |
| 83 | + |
| 84 | + assert e.value.code == 1 |
| 85 | + |
| 86 | + out, err = capfd.readouterr() |
| 87 | + assert f"Clearing cache directory: {fake_cache_dir}" in out |
| 88 | + assert "Error clearing cache:" in err |
| 89 | + |
| 90 | + |
| 91 | +def test_clean_cache_without_sentinel(tmp_path, monkeypatch, capfd): |
| 92 | + fake_cache_dir = (tmp_path / "not_a_cache").resolve() |
| 93 | + monkeypatch.setattr(main_module, "CIBW_CACHE_PATH", fake_cache_dir) |
| 94 | + |
| 95 | + fake_cache_dir.mkdir(parents=True, exist_ok=True) |
| 96 | + |
| 97 | + monkeypatch.setattr(sys, "argv", ["cibuildwheel", "--clean-cache"]) |
| 98 | + |
| 99 | + with pytest.raises(SystemExit) as e: |
| 100 | + main() |
| 101 | + |
| 102 | + assert e.value.code == 1 |
| 103 | + |
| 104 | + out, err = capfd.readouterr() |
| 105 | + assert "does not appear to be a cibuildwheel cache directory" in err |
| 106 | + assert fake_cache_dir.exists() |
| 107 | + |
| 108 | + |
| 109 | +def test_clean_cache_with_invalid_signature(tmp_path, monkeypatch, capfd): |
| 110 | + fake_cache_dir = (tmp_path / "fake_cache").resolve() |
| 111 | + monkeypatch.setattr(main_module, "CIBW_CACHE_PATH", fake_cache_dir) |
| 112 | + |
| 113 | + fake_cache_dir.mkdir(parents=True, exist_ok=True) |
| 114 | + |
| 115 | + cibw_sentinel = fake_cache_dir / "CACHEDIR.TAG" |
| 116 | + cibw_sentinel.write_text("Invalid signature\n# This is not a real cache directory tag") |
| 117 | + |
| 118 | + monkeypatch.setattr(sys, "argv", ["cibuildwheel", "--clean-cache"]) |
| 119 | + |
| 120 | + with pytest.raises(SystemExit) as e: |
| 121 | + main() |
| 122 | + |
| 123 | + assert e.value.code == 1 |
| 124 | + |
| 125 | + out, err = capfd.readouterr() |
| 126 | + assert "does not contain a valid cache directory signature" in err |
| 127 | + assert fake_cache_dir.exists() |
0 commit comments