diff options
author | Massimiliano Culpo <massimiliano.culpo@googlemail.com> | 2017-08-14 20:29:18 +0200 |
---|---|---|
committer | becker33 <becker33@llnl.gov> | 2017-08-14 11:29:18 -0700 |
commit | 5184edb15f2cf260d561d8fcb7bf56ad147b6555 (patch) | |
tree | 6f4e91d58bf3ce43067f64dfd47ce1bc7c492719 | |
parent | 4d9ef49b49a5f2e3034e26de535e6744251a10fa (diff) | |
download | spack-5184edb15f2cf260d561d8fcb7bf56ad147b6555.tar.gz spack-5184edb15f2cf260d561d8fcb7bf56ad147b6555.tar.bz2 spack-5184edb15f2cf260d561d8fcb7bf56ad147b6555.tar.xz spack-5184edb15f2cf260d561d8fcb7bf56ad147b6555.zip |
Added a custom action for --clean and --dirty. (#5081)
The action `CleanOrDirtyAction` has been added. It sets the default
value for `dest` to `spack.dirty`, and changes it according to the flags
passed via command line. Added unit tests to check that the arguments
are parsed correctly. Removed lines in `PackageBase` that were setting
the default value of dirty according to what was in the configuration.
-rw-r--r-- | lib/spack/spack/cmd/common/arguments.py | 33 | ||||
-rw-r--r-- | lib/spack/spack/package.py | 4 | ||||
-rw-r--r-- | lib/spack/spack/test/cmd/install.py | 24 |
3 files changed, 52 insertions, 9 deletions
diff --git a/lib/spack/spack/cmd/common/arguments.py b/lib/spack/spack/cmd/common/arguments.py index 46ff44c84a..d1b0418770 100644 --- a/lib/spack/spack/cmd/common/arguments.py +++ b/lib/spack/spack/cmd/common/arguments.py @@ -74,6 +74,23 @@ class ConstraintAction(argparse.Action): return sorted(specs) +class CleanOrDirtyAction(argparse.Action): + """Sets the dirty flag in the current namespace""" + + def __init__(self, *args, **kwargs): + kwargs['default'] = spack.dirty + super(CleanOrDirtyAction, self).__init__(*args, **kwargs) + + def __call__(self, parser, namespace, values, option_string=None): + if option_string == '--clean': + setattr(namespace, self.dest, False) + elif option_string == '--dirty': + setattr(namespace, self.dest, True) + else: + msg = 'expected "--dirty" or "--clean" [got {0} instead]' + raise argparse.ArgumentError(msg.format(option_string)) + + _arguments['constraint'] = Args( 'constraint', nargs=argparse.REMAINDER, action=ConstraintAction, help='constraint to select a subset of installed packages') @@ -93,12 +110,20 @@ _arguments['recurse_dependencies'] = Args( help='recursively traverse spec dependencies') _arguments['clean'] = Args( - '--clean', action='store_false', dest='dirty', - help='clean environment before installing package') + '--clean', + action=CleanOrDirtyAction, + dest='dirty', + help='clean environment before installing package', + nargs=0 +) _arguments['dirty'] = Args( - '--dirty', action='store_true', dest='dirty', - help='do NOT clean environment before installing') + '--dirty', + action=CleanOrDirtyAction, + dest='dirty', + help='do NOT clean environment before installing', + nargs=0 +) _arguments['long'] = Args( '-l', '--long', action='store_true', diff --git a/lib/spack/spack/package.py b/lib/spack/spack/package.py index e8e741fefa..86df0ec947 100644 --- a/lib/spack/spack/package.py +++ b/lib/spack/spack/package.py @@ -1218,10 +1218,6 @@ class PackageBase(with_metaclass(PackageMeta, object)): rec = spack.store.db.get_record(self.spec) return self._update_explicit_entry_in_db(rec, explicit) - # Dirty argument takes precedence over dirty config setting. - if dirty is None: - dirty = spack.dirty - self._do_install_pop_kwargs(kwargs) # First, install dependencies recursively. diff --git a/lib/spack/spack/test/cmd/install.py b/lib/spack/spack/test/cmd/install.py index 951d9d85d7..5886d602a2 100644 --- a/lib/spack/spack/test/cmd/install.py +++ b/lib/spack/spack/test/cmd/install.py @@ -22,12 +22,24 @@ # License along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ############################################################################## -from spack.main import SpackCommand +import argparse + +import pytest +import spack.cmd.install +from spack.main import SpackCommand install = SpackCommand('install') +@pytest.fixture(scope='module') +def parser(): + """Returns the parser for the module command""" + parser = argparse.ArgumentParser() + spack.cmd.install.setup_parser(parser) + return parser + + def _install_package_and_dependency( tmpdir, builtin_mock, mock_archive, mock_fetch, config, install_mockery): @@ -64,3 +76,13 @@ def test_install_package_already_installed( skipped = [line for line in content.split('\n') if 'skipped' in line] assert len(skipped) == 2 + + +@pytest.mark.parametrize('arguments,expected', [ + ([], spack.dirty), # The default read from configuration file + (['--clean'], False), + (['--dirty'], True), +]) +def test_install_dirty_flag(parser, arguments, expected): + args = parser.parse_args(arguments) + assert args.dirty == expected |