Skip to content

Commit 495b6d3

Browse files
committed
Testing Bugfix: refactor clean --python-cache to support all
There were two choices: 1) remove '-p' from '-a' or 2) allow monkeypatching the cleaning of the python cache since clean's test_function_calls isn't supposed to be actually cleaning anything. This commit supports the latter and adds a test case for `-p`.
1 parent dd70ed7 commit 495b6d3

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

lib/spack/spack/cmd/clean.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ def setup_parser(subparser):
5858
arguments.add_common_arguments(subparser, ['specs'])
5959

6060

61+
def remove_python_cache():
62+
for directory in [lib_path, var_path]:
63+
for root, dirs, files in os.walk(directory):
64+
for f in files:
65+
if f.endswith('.pyc') or f.endswith('.pyo'):
66+
fname = os.path.join(root, f)
67+
tty.debug('Removing {0}'.format(fname))
68+
os.remove(fname)
69+
for d in dirs:
70+
if d == '__pycache__':
71+
dname = os.path.join(root, d)
72+
tty.debug('Removing {0}'.format(dname))
73+
shutil.rmtree(dname)
74+
75+
6176
def clean(parser, args):
6277
# If nothing was set, activate the default
6378
if not any([args.specs, args.stage, args.downloads, args.failures,
@@ -95,18 +110,7 @@ def clean(parser, args):
95110

96111
if args.python_cache:
97112
tty.msg('Removing python cache files')
98-
for directory in [lib_path, var_path]:
99-
for root, dirs, files in os.walk(directory):
100-
for f in files:
101-
if f.endswith('.pyc') or f.endswith('.pyo'):
102-
fname = os.path.join(root, f)
103-
tty.debug('Removing {0}'.format(fname))
104-
os.remove(fname)
105-
for d in dirs:
106-
if d == '__pycache__':
107-
dname = os.path.join(root, d)
108-
tty.debug('Removing {0}'.format(dname))
109-
shutil.rmtree(dname)
113+
remove_python_cache()
110114

111115
if args.bootstrap:
112116
bootstrap_prefix = spack.util.path.canonicalize_path(

lib/spack/spack/test/cmd/clean.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
#
44
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
55

6+
import os
67
import sys
78

89
import pytest
910

11+
import llnl.util.filesystem as fs
12+
1013
import spack.caches
1114
import spack.main
1215
import spack.package_base
@@ -41,11 +44,13 @@ def __call__(self, *args, **kwargs):
4144
spack.caches.misc_cache, 'destroy', Counter('caches'))
4245
monkeypatch.setattr(
4346
spack.installer, 'clear_failures', Counter('failures'))
47+
monkeypatch.setattr(spack.cmd.clean, 'remove_python_cache',
48+
Counter('python_cache'))
4449

4550
yield counts
4651

4752

48-
all_effects = ['stages', 'downloads', 'caches', 'failures']
53+
all_effects = ['stages', 'downloads', 'caches', 'failures', 'python_cache']
4954

5055

5156
@pytest.mark.usefixtures(
@@ -57,6 +62,7 @@ def __call__(self, *args, **kwargs):
5762
('-sd', ['stages', 'downloads']),
5863
('-m', ['caches']),
5964
('-f', ['failures']),
65+
('-p', ['python_cache']),
6066
('-a', all_effects),
6167
('', []),
6268
])
@@ -69,3 +75,36 @@ def test_function_calls(command_line, effects, mock_calls_for_clean):
6975
# number of times
7076
for name in ['package'] + all_effects:
7177
assert mock_calls_for_clean[name] == (1 if name in effects else 0)
78+
79+
80+
def test_remove_python_cache(tmpdir, monkeypatch):
81+
cache_files = ['file1.pyo', 'file2.pyc']
82+
source_file = 'file1.py'
83+
84+
def _setup_files(directory):
85+
# Create a python cache and source file.
86+
cache_dir = fs.join_path(directory, '__pycache__')
87+
fs.mkdirp(cache_dir)
88+
fs.touch(fs.join_path(directory, source_file))
89+
for filename in cache_files:
90+
fs.touch(fs.join_path(cache_dir, filename))
91+
92+
def _check_files(directory):
93+
# Ensure the python cache created by _setup_files is removed
94+
# and the source file is not.
95+
assert os.path.exists(fs.join_path(directory, source_file))
96+
assert not os.path.exists(fs.join_path(directory, '__pycache__'))
97+
98+
source_dir = fs.join_path(tmpdir, 'lib', 'spack', 'spack')
99+
var_dir = fs.join_path(tmpdir, 'var', 'spack', 'stuff')
100+
101+
for d in [source_dir, var_dir]:
102+
_setup_files(d)
103+
104+
monkeypatch.setattr(spack.cmd.clean, "lib_path", source_dir)
105+
monkeypatch.setattr(spack.cmd.clean, "var_path", var_dir)
106+
107+
spack.cmd.clean.remove_python_cache()
108+
109+
for d in [source_dir, var_dir]:
110+
_check_files(d)

0 commit comments

Comments
 (0)