Пример #1
0
 def test_representation(self):
     a = Variant('foo',
                 default='',
                 description='',
                 values=('bar', 'baz', 'foobar'),
                 multi=False)
     assert a.allowed_values == 'bar, baz, foobar'
Пример #2
0
    def _execute(pkg):
        if not re.match(spack.spec.identifier_re, name):
            directive = 'variant'
            msg = "Invalid variant name in {0}: '{1}'"
            raise DirectiveError(directive, msg.format(pkg.name, name))

        pkg.variants[name] = Variant(default, description)
Пример #3
0
    def test_validation(self):
        a = Variant('foo',
                    default='',
                    description='',
                    values=('bar', 'baz', 'foobar'),
                    multi=False)
        # Valid vspec, shouldn't raise
        vspec = a.make_variant('bar')
        a.validate_or_raise(vspec)

        # Multiple values are not allowed
        with pytest.raises(MultipleValuesInExclusiveVariantError):
            vspec.value = 'bar,baz'

        # Inconsistent vspec
        vspec.name = 'FOO'
        with pytest.raises(InconsistentValidationError):
            a.validate_or_raise(vspec)

        # Valid multi-value vspec
        a.multi = True
        vspec = a.make_variant('bar,baz')
        a.validate_or_raise(vspec)
        # Add an invalid value
        vspec.value = 'bar,baz,barbaz'
        with pytest.raises(InvalidVariantValueError):
            a.validate_or_raise(vspec)
Пример #4
0
def variant(pkg, name, default=False, description=""):
    """Define a variant for the package. Packager can specify a default
    value (on or off) as well as a text description."""

    default = bool(default)
    description = str(description).strip()

    if not re.match(spack.spec.identifier_re, name):
        raise DirectiveError("Invalid variant name in %s: '%s'" %
                             (pkg.name, name))

    pkg.variants[name] = Variant(default, description)
Пример #5
0
    def test_callable_validator(self):
        def validator(x):
            try:
                return isinstance(int(x), numbers.Integral)
            except ValueError:
                return False

        a = Variant('foo',
                    default=1024,
                    description='',
                    values=validator,
                    multi=False)
        vspec = a.make_default()
        a.validate_or_raise(vspec)
        vspec.value = 2056
        a.validate_or_raise(vspec)
        vspec.value = 'foo'
        with pytest.raises(InvalidVariantValueError):
            a.validate_or_raise(vspec)