Added a unit test for 'spack lmod setdefault'

This commit is contained in:
Massimiliano Culpo 2018-04-12 15:07:25 +02:00 committed by Todd Gamblin
parent e81c0c3e2c
commit 008f171a7e
4 changed files with 101 additions and 12 deletions

View File

@ -22,23 +22,17 @@
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import os.path
import pytest
import spack.main
import spack.modules as modules
import spack.modules
lmod = spack.main.SpackCommand('lmod')
def _get_module_files(args):
files = []
specs = args.specs()
for module_type in args.module_type:
writer_cls = modules.module_types[module_type]
files.extend([writer_cls(spec).layout.filename for spec in specs])
return files
# Needed to make the fixture work
writer_cls = spack.modules.lmod.LmodModulefileWriter
@pytest.fixture(
@ -61,3 +55,49 @@ def failure_args(request):
def test_exit_with_failure(database, failure_args):
with pytest.raises(spack.main.SpackCommandError):
lmod(*failure_args)
def test_setdefault_command(refresh_db_on_exit, database, patch_configuration):
patch_configuration('autoload_direct')
# Install two different versions of a package
other_spec, preferred = 'a@1.0', 'a@2.0'
database.install(preferred)
database.install(other_spec)
writer_cls = spack.modules.module_types['lmod']
writers = {
preferred: writer_cls(spack.spec.Spec(preferred).concretized()),
other_spec: writer_cls(spack.spec.Spec(other_spec).concretized())
}
# Create two module files for the same software
lmod('refresh', '-y', '--delete-tree', preferred, other_spec)
# Assert initial directory state: no link and all module files present
link_name = os.path.join(
os.path.dirname(writers[preferred].layout.filename),
'default'
)
for k in preferred, other_spec:
assert os.path.exists(writers[k].layout.filename)
assert not os.path.exists(link_name)
# Set the default to be the other spec
lmod('setdefault', other_spec)
# Check that a link named default exists, and points to the right file
for k in preferred, other_spec:
assert os.path.exists(writers[k].layout.filename)
assert os.path.exists(link_name) and os.path.islink(link_name)
assert os.path.realpath(link_name) == writers[other_spec].layout.filename
# Reset the default to be the preferred spec
lmod('setdefault', preferred)
# Check that a link named default exists, and points to the right file
for k in preferred, other_spec:
assert os.path.exists(writers[k].layout.filename)
assert os.path.exists(link_name) and os.path.islink(link_name)
assert os.path.realpath(link_name) == writers[preferred].layout.filename

View File

@ -70,7 +70,7 @@ def test_exit_with_failure(database, failure_args):
def test_remove_and_add_tcl(database, parser):
"""Tests adding and removing a dotkit module file."""
"""Tests adding and removing a tcl module file."""
rm_cli_args = ['rm', '-y', 'mpileaks']
module_files = _get_module_files(parser.parse_args(rm_cli_args))

View File

@ -24,7 +24,9 @@
##############################################################################
import collections
import copy
import inspect
import os
import os.path
import shutil
import re
@ -404,6 +406,45 @@ def fake_fn(self):
PackageBase.fetcher = orig_fn
@pytest.fixture()
def patch_configuration(monkeypatch, request):
"""Reads a configuration file from the mock ones prepared for tests
and monkeypatches the right classes to hook it in.
"""
# Class of the module file writer
writer_cls = getattr(request.module, 'writer_cls')
# Module where the module file writer is defined
writer_mod = inspect.getmodule(writer_cls)
# Key for specific settings relative to this module type
writer_key = str(writer_mod.__name__).split('.')[-1]
# Root folder for configuration
root_for_conf = os.path.join(
spack.test_path, 'data', 'modules', writer_key
)
def _impl(filename):
file = os.path.join(root_for_conf, filename + '.yaml')
with open(file) as f:
configuration = yaml.load(f)
monkeypatch.setattr(
spack.modules.common,
'configuration',
configuration
)
monkeypatch.setattr(
writer_mod,
'configuration',
configuration[writer_key]
)
monkeypatch.setattr(
writer_mod,
'configuration_registry',
{}
)
return _impl
##########
# Fake archives and repositories
##########

View File

@ -130,6 +130,14 @@ def _impl(filename):
return _impl
@pytest.fixture()
def update_template_dirs(config, monkeypatch):
"""Mocks the template directories for tests"""
dirs = spack.config.get_config('config')['template_dirs']
dirs = [spack.util.path.canonicalize_path(x) for x in dirs]
monkeypatch.setattr(spack, 'template_dirs', dirs)
@pytest.fixture()
def factory(request):
"""Function that, given a spec string, returns an instance of the writer