| 1 |
# Copyright 2015-2026 Facundo Batista, Nicolás Demarchi |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify it |
| 4 |
# under the terms of the GNU General Public License version 3, as published |
| 5 |
# by the Free Software Foundation. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, but |
| 8 |
# WITHOUT ANY WARRANTY; without even the implied warranties of |
| 9 |
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 10 |
# PURPOSE. See the GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License along |
| 13 |
# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
# |
| 15 |
# For further info, check https://github.com/PyAr/fades |
| 16 |
|
| 17 |
import json |
| 18 |
import os |
| 19 |
import time |
| 20 |
from pathlib import Path |
| 21 |
|
| 22 |
from threading import Thread |
| 23 |
|
| 24 |
from fades import cache |
| 25 |
|
| 26 |
|
| 27 |
def test_missing_file(tmp_file): |
| 28 |
venvscache = cache.VEnvsCache(tmp_file) |
| 29 |
venvscache.remove("missing/path") |
| 30 |
|
| 31 |
lines = venvscache._read_cache() |
| 32 |
assert lines == [] |
| 33 |
|
| 34 |
|
| 35 |
def test_missing_env_in_cache(tmp_file): |
| 36 |
venvscache = cache.VEnvsCache(tmp_file) |
| 37 |
options = {"foo": "bar"} |
| 38 |
metadata = {"env_path": "some/path", "env_bin_path": "other/path"} |
| 39 |
venvscache.store("installed", metadata, "interpreter", options=options) |
| 40 |
lines = venvscache._read_cache() |
| 41 |
assert len(lines) == 1 |
| 42 |
|
| 43 |
venvscache.remove(Path("some/path")) |
| 44 |
|
| 45 |
lines = venvscache._read_cache() |
| 46 |
assert lines == [] |
| 47 |
|
| 48 |
|
| 49 |
def test_preserve_cache_data_ordering(tmp_file): |
| 50 |
venvscache = cache.VEnvsCache(tmp_file) |
| 51 |
# store 3 venvs |
| 52 |
options = {"foo": "bar"} |
| 53 |
metadata1 = {"env_path": "path/env1", "env_bin_path": "other/path"} |
| 54 |
metadata2 = {"env_path": "path/env2", "env_bin_path": "other/path"} |
| 55 |
metadata3 = {"env_path": "path/env3", "env_bin_path": "other/path"} |
| 56 |
venvscache.store("installed1", metadata1, "interpreter", options=options) |
| 57 |
venvscache.store("installed2", metadata2, "interpreter", options=options) |
| 58 |
venvscache.store("installed3", metadata3, "interpreter", options=options) |
| 59 |
|
| 60 |
venvscache.remove(Path("path/env2")) |
| 61 |
|
| 62 |
lines = venvscache._read_cache() |
| 63 |
assert len(lines) == 2 |
| 64 |
assert json.loads(lines[0]).get("metadata").get("env_path") == "path/env1" |
| 65 |
assert json.loads(lines[1]).get("metadata").get("env_path") == "path/env3" |
| 66 |
|
| 67 |
|
| 68 |
def test_lock_cache_for_remove(tmp_file): |
| 69 |
venvscache = cache.VEnvsCache(tmp_file) |
| 70 |
# store 3 venvs |
| 71 |
options = {"foo": "bar"} |
| 72 |
metadata1 = {"env_path": "path/env1", "env_bin_path": "other/path"} |
| 73 |
metadata2 = {"env_path": "path/env2", "env_bin_path": "other/path"} |
| 74 |
metadata3 = {"env_path": "path/env3", "env_bin_path": "other/path"} |
| 75 |
venvscache.store("installed1", metadata1, "interpreter", options=options) |
| 76 |
venvscache.store("installed2", metadata2, "interpreter", options=options) |
| 77 |
venvscache.store("installed3", metadata3, "interpreter", options=options) |
| 78 |
|
| 79 |
# patch _write_cache so it emulates a slow write during which |
| 80 |
# another process managed to modify the cache file before the |
| 81 |
# first process finished writing the modified cache data |
| 82 |
original_write_cache = venvscache._write_cache |
| 83 |
|
| 84 |
other_process = Thread(target=venvscache.remove, args=(Path("path/env1"),)) |
| 85 |
|
| 86 |
def slow_write_cache(*args, **kwargs): |
| 87 |
venvscache._write_cache = original_write_cache |
| 88 |
|
| 89 |
# start "other process" and wait a little to ensure it must wait |
| 90 |
# for the lock to be released |
| 91 |
other_process.start() |
| 92 |
time.sleep(0.01) |
| 93 |
|
| 94 |
original_write_cache(*args, **kwargs) |
| 95 |
|
| 96 |
venvscache._write_cache = slow_write_cache |
| 97 |
|
| 98 |
# just a sanity check |
| 99 |
assert not os.path.exists(venvscache.filepath.name + ".lock") |
| 100 |
# remove a virtualenv from the cache |
| 101 |
venvscache.remove(Path("path/env2")) |
| 102 |
other_process.join() |
| 103 |
|
| 104 |
# when cache file is properly locked both virtualenvs |
| 105 |
# will have been removed from the cache |
| 106 |
lines = venvscache._read_cache() |
| 107 |
assert len(lines) == 1 |
| 108 |
assert json.loads(lines[0])["metadata"]["env_path"] == "path/env3" |
| 109 |
assert not os.path.exists(venvscache.filepath.name + ".lock") |