summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@gmail.com>2018-04-12 15:07:25 +0200
committerTodd Gamblin <tgamblin@llnl.gov>2018-07-24 11:27:17 -0700
commit008f171a7e4ec46b528d2f3492f48a1fa5692d38 (patch)
treea9de804b90494b288df358ab891e64f805721d7b /lib
parente81c0c3e2c1a45c77d91fbffb2911b9dc3511272 (diff)
downloadspack-008f171a7e4ec46b528d2f3492f48a1fa5692d38.tar.gz
spack-008f171a7e4ec46b528d2f3492f48a1fa5692d38.tar.bz2
spack-008f171a7e4ec46b528d2f3492f48a1fa5692d38.tar.xz
spack-008f171a7e4ec46b528d2f3492f48a1fa5692d38.zip
Added a unit test for 'spack lmod setdefault'
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/test/cmd/lmod.py62
-rw-r--r--lib/spack/spack/test/cmd/tcl.py2
-rw-r--r--lib/spack/spack/test/conftest.py41
-rw-r--r--lib/spack/spack/test/modules/conftest.py8
4 files changed, 101 insertions, 12 deletions
diff --git a/lib/spack/spack/test/cmd/lmod.py b/lib/spack/spack/test/cmd/lmod.py
index c7c0089be9..5ab886b071 100644
--- a/lib/spack/spack/test/cmd/lmod.py
+++ b/lib/spack/spack/test/cmd/lmod.py
@@ -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
diff --git a/lib/spack/spack/test/cmd/tcl.py b/lib/spack/spack/test/cmd/tcl.py
index 64182cbe2e..ba3e322b49 100644
--- a/lib/spack/spack/test/cmd/tcl.py
+++ b/lib/spack/spack/test/cmd/tcl.py
@@ -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))
diff --git a/lib/spack/spack/test/conftest.py b/lib/spack/spack/test/conftest.py
index 4f4e01930a..c2323e5156 100644
--- a/lib/spack/spack/test/conftest.py
+++ b/lib/spack/spack/test/conftest.py
@@ -24,7 +24,9 @@
##############################################################################
import collections
import copy
+import inspect
import os
+import os.path
import shutil
import re
@@ -404,6 +406,45 @@ def mock_fetch(mock_archive):
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
##########
diff --git a/lib/spack/spack/test/modules/conftest.py b/lib/spack/spack/test/modules/conftest.py
index 6cf6f7e1a5..5b52395270 100644
--- a/lib/spack/spack/test/modules/conftest.py
+++ b/lib/spack/spack/test/modules/conftest.py
@@ -131,6 +131,14 @@ def patch_configuration(monkeypatch, request):
@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
and the corresponding spec.