forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main_clean.py
392 lines (303 loc) · 11 KB
/
test_main_clean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations
import json
from datetime import datetime
from pathlib import Path
from typing import Iterable
import pytest
from pytest_mock import MockerFixture
from conda.base.constants import (
CONDA_LOGS_DIR,
CONDA_PACKAGE_EXTENSIONS,
CONDA_TEMP_EXTENSIONS,
)
from conda.cli.main_clean import _get_size
from conda.core.subdir_data import create_cache_dir
from conda.gateways.logging import set_verbosity
from conda.testing import CondaCLIFixture, TmpEnvFixture
def _get_pkgs(pkgs_dir: str | Path) -> list[Path]:
return [package for package in Path(pkgs_dir).iterdir() if package.is_dir()]
def _get_tars(pkgs_dir: str | Path) -> list[Path]:
return [
file
for file in Path(pkgs_dir).iterdir()
if file.is_file() and file.name.endswith(CONDA_PACKAGE_EXTENSIONS)
]
def _get_index_cache() -> list[Path]:
return [
file
for file in Path(create_cache_dir()).iterdir()
if file.is_file() and file.name.endswith(".json")
]
def _get_tempfiles(pkgs_dir: str | Path) -> list[Path]:
return [
file
for file in Path(pkgs_dir).iterdir()
if file.is_file() and file.name.endswith(CONDA_TEMP_EXTENSIONS)
]
def _get_logfiles(pkgs_dir: str | Path) -> list[Path]:
try:
return [file for file in Path(pkgs_dir, CONDA_LOGS_DIR).iterdir()]
except FileNotFoundError:
# FileNotFoundError: CONDA_LOGS_DIR doesn't exist
return []
def _get_all(pkgs_dir: str | Path) -> tuple[list[Path], list[Path], list[Path]]:
return _get_pkgs(pkgs_dir), _get_tars(pkgs_dir), _get_index_cache()
def has_pkg(name: str, contents: Iterable[str | Path]) -> bool:
return any(Path(content).name.startswith(f"{name}-") for content in contents)
# conda clean --force-pkgs-dirs
def test_clean_force_pkgs_dirs(
clear_cache,
test_recipes_channel: Path,
conda_cli: CondaCLIFixture,
tmp_env: TmpEnvFixture,
tmp_pkgs_dir: Path,
):
pkg = "small-executable"
# pkgs_dir is a directory
assert tmp_pkgs_dir.is_dir()
with tmp_env(pkg):
stdout, _, _ = conda_cli("clean", "--force-pkgs-dirs", "--yes", "--json")
json.loads(stdout) # assert valid json
# pkgs_dir is removed
assert not tmp_pkgs_dir.exists()
# pkgs_dir is still removed
assert not tmp_pkgs_dir.exists()
# conda clean --packages
def test_clean_and_packages(
clear_cache,
test_recipes_channel: Path,
conda_cli: CondaCLIFixture,
tmp_env: TmpEnvFixture,
tmp_pkgs_dir: Path,
):
pkg = "small-executable"
# pkg doesn't exist ahead of time
assert not has_pkg(pkg, _get_pkgs(tmp_pkgs_dir))
with tmp_env(pkg) as prefix:
# pkg exists
assert has_pkg(pkg, _get_pkgs(tmp_pkgs_dir))
# --json flag is regression test for #5451
stdout, _, _ = conda_cli("clean", "--packages", "--yes", "--json")
json.loads(stdout) # assert valid json
# pkg still exists since its in use by temp env
assert has_pkg(pkg, _get_pkgs(tmp_pkgs_dir))
conda_cli("remove", "--prefix", prefix, pkg, "--yes", "--json")
stdout, _, _ = conda_cli("clean", "--packages", "--yes", "--json")
json.loads(stdout) # assert valid json
# pkg is removed
assert not has_pkg(pkg, _get_pkgs(tmp_pkgs_dir))
# pkg is still removed
assert not has_pkg(pkg, _get_pkgs(tmp_pkgs_dir))
# conda clean --tarballs
def test_clean_tarballs(
clear_cache,
test_recipes_channel: Path,
conda_cli: CondaCLIFixture,
tmp_env: TmpEnvFixture,
tmp_pkgs_dir: Path,
):
pkg = "small-executable"
# tarball doesn't exist ahead of time
assert not has_pkg(pkg, _get_tars(tmp_pkgs_dir))
with tmp_env(pkg):
# tarball exists
assert has_pkg(pkg, _get_tars(tmp_pkgs_dir))
# --json flag is regression test for #5451
stdout, _, _ = conda_cli("clean", "--tarballs", "--yes", "--json")
json.loads(stdout) # assert valid json
# tarball is removed
assert not has_pkg(pkg, _get_tars(tmp_pkgs_dir))
# tarball is still removed
assert not has_pkg(pkg, _get_tars(tmp_pkgs_dir))
# conda clean --index-cache
def test_clean_index_cache(
clear_cache,
test_recipes_channel: Path,
conda_cli: CondaCLIFixture,
tmp_env: TmpEnvFixture,
tmp_pkgs_dir: Path,
):
pkg = "small-executable"
# index cache doesn't exist ahead of time
assert not _get_index_cache()
with tmp_env(pkg):
# index cache exists
assert _get_index_cache()
stdout, _, _ = conda_cli("clean", "--index-cache", "--yes", "--json")
json.loads(stdout) # assert valid json
# index cache is cleared
assert not _get_index_cache()
# index cache is still cleared
assert not _get_index_cache()
# conda clean --tempfiles
def test_clean_tempfiles(
clear_cache,
test_recipes_channel: Path,
conda_cli: CondaCLIFixture,
tmp_env: TmpEnvFixture,
tmp_pkgs_dir: Path,
):
"""Tempfiles are either suffixed with .c~ or .trash.
.c~ is used to indicate that conda is actively using that file. If the conda process is
terminated unexpectedly these .c~ files may remain and hence can be cleaned up after the fact.
.trash appears to be a legacy suffix that is no longer used by conda.
Since the presence of .c~ and .trash files are dependent upon irregular termination we create
our own temporary files to confirm they get cleaned up.
"""
pkg = "small-executable"
# tempfiles don't exist ahead of time
assert not _get_tempfiles(tmp_pkgs_dir)
with tmp_env(pkg):
# mimic tempfiles being created
path = _get_tars(tmp_pkgs_dir)[0] # grab any tarball
for ext in CONDA_TEMP_EXTENSIONS:
(path.parent / f"{path.name}{ext}").touch()
# tempfiles exist
assert len(_get_tempfiles(tmp_pkgs_dir)) == len(CONDA_TEMP_EXTENSIONS)
# --json flag is regression test for #5451
stdout, _, _ = conda_cli(
"clean",
"--tempfiles",
tmp_pkgs_dir,
"--yes",
"--json",
)
json.loads(stdout) # assert valid json
# tempfiles removed
assert not _get_tempfiles(tmp_pkgs_dir)
# tempfiles still removed
assert not _get_tempfiles(tmp_pkgs_dir)
# conda clean --logfiles
def test_clean_logfiles(
clear_cache,
test_recipes_channel: Path,
conda_cli: CondaCLIFixture,
tmp_env: TmpEnvFixture,
tmp_pkgs_dir: Path,
):
"""Logfiles are found in pkgs_dir/.logs.
Since these log files were uniquely created during the experimental
phase of the conda-libmamba-solver.
"""
pkg = "small-executable"
# logfiles don't exist ahead of time
assert not _get_logfiles(tmp_pkgs_dir)
with tmp_env(pkg):
# mimic logfiles being created
logs_dir = Path(tmp_pkgs_dir, CONDA_LOGS_DIR)
logs_dir.mkdir(parents=True, exist_ok=True)
path = logs_dir / f"{datetime.utcnow():%Y%m%d-%H%M%S-%f}.log"
path.touch()
# logfiles exist
assert path in _get_logfiles(tmp_pkgs_dir)
# --json flag is regression test for #5451
stdout, _, _ = conda_cli("clean", "--logfiles", "--yes", "--json")
json.loads(stdout) # assert valid json
# logfiles removed
assert not _get_logfiles(tmp_pkgs_dir)
# logfiles still removed
assert not _get_logfiles(tmp_pkgs_dir)
# conda clean --all [--verbose]
@pytest.mark.parametrize("verbose", [True, False])
def test_clean_all(
clear_cache,
verbose: bool,
test_recipes_channel: Path,
conda_cli: CondaCLIFixture,
tmp_env: TmpEnvFixture,
tmp_pkgs_dir: Path,
):
pkg = "small-executable"
args = ("--yes", "--json")
if verbose:
args = (*args, "--verbose")
# pkg, tarball, & index cache doesn't exist ahead of time
pkgs, tars, cache = _get_all(tmp_pkgs_dir)
assert not has_pkg(pkg, pkgs)
assert not has_pkg(pkg, tars)
assert not cache
with tmp_env(pkg) as prefix:
# pkg, tarball, & index cache exists
pkgs, tars, cache = _get_all(tmp_pkgs_dir)
assert has_pkg(pkg, pkgs)
assert has_pkg(pkg, tars)
assert cache
stdout, _, _ = conda_cli("clean", "--all", *args)
json.loads(stdout) # assert valid json
# pkg still exists since its in use by temp env
# tarball is removed
# index cache is cleared
pkgs, tars, cache = _get_all(tmp_pkgs_dir)
assert has_pkg(pkg, pkgs)
assert not has_pkg(pkg, tars)
assert not cache
conda_cli("remove", "--prefix", prefix, pkg, *args)
stdout, _, _ = conda_cli("clean", "--packages", *args)
json.loads(stdout) # assert valid json
# pkg is removed
# tarball is still removed
# index cache is still cleared
pkgs, tars, index_cache = _get_all(tmp_pkgs_dir)
assert not has_pkg(pkg, pkgs)
assert not has_pkg(pkg, tars)
assert not cache
# pkg is still removed
# tarball is still removed
# index cache is still cleared
pkgs, tars, index_cache = _get_all(tmp_pkgs_dir)
assert not has_pkg(pkg, pkgs)
assert not has_pkg(pkg, tars)
assert not cache
set_verbosity(0) # reset verbosity
# conda clean --all --verbose
@pytest.mark.parametrize("as_json", [True, False])
def test_clean_all_mock_lstat(
clear_cache,
mocker: MockerFixture,
as_json: bool,
test_recipes_channel: Path,
conda_cli: CondaCLIFixture,
tmp_env: TmpEnvFixture,
tmp_pkgs_dir: Path,
):
pkg = "small-executable"
args = ("--yes", "--verbose")
if as_json:
args = (*args, "--json")
with tmp_env(pkg) as prefix:
# pkg, tarball, & index cache exists
pkgs, tars, cache = _get_all(tmp_pkgs_dir)
assert has_pkg(pkg, pkgs)
assert has_pkg(pkg, tars)
assert cache
mocker.patch("os.lstat", side_effect=OSError)
conda_cli("remove", "--prefix", prefix, pkg, *args)
stdout, _, _ = conda_cli("clean", "--packages", *args)
assert "WARNING:" in stdout
if as_json:
json.loads(stdout) # assert valid json
# pkg, tarball, & index cache still exists
pkgs, tars, index_cache = _get_all(tmp_pkgs_dir)
assert has_pkg(pkg, pkgs)
assert has_pkg(pkg, tars)
assert cache
set_verbosity(0) # reset verbosity
# _get_size unittest, valid file
def test_get_size(tmp_path: Path):
warnings: list[str] = []
path = tmp_path / "file"
path.write_text("hello")
assert _get_size(path, warnings=warnings)
assert not warnings
# _get_size unittest, invalid file
def test_get_size_None():
with pytest.raises(OSError):
_get_size("not-a-file", warnings=None)
# _get_size unittest, invalid file and collect warnings
def test_get_size_list():
warnings: list[str] = []
with pytest.raises(NotImplementedError):
_get_size("not-a-file", warnings=warnings)
assert warnings