Exemple #1
0
def test_hangwatch():
    from syn.base_utils import hangwatch, message
    from time import sleep

    accum = []
    assert sum(accum) == 0

    def thing1():
        accum.append(1)
    
    hangwatch(1, thing1)
    assert sum(accum) == 1

    def thing2():
        sleep(.05)

    assert_raises(RuntimeError, hangwatch, .01, thing2)

    def thing3(msg='foo'):
        raise NotImplementedError(msg)

    with capture():
        assert_raises(NotImplementedError, hangwatch, 1, thing3)

    try:
        with capture():
            hangwatch(1, thing3, 'bar')
    except NotImplementedError as e:
        assert message(e) == 'bar'
Exemple #2
0
 def run(self, command, env, **kwargs):
     try:
         eval(command)
         return 0
     except Exception as e:
         eprint(message(e))
         return 1
Exemple #3
0
def test_hangwatch():
    from syn.base_utils import hangwatch, message
    from time import sleep

    accum = []
    assert sum(accum) == 0

    def thing1():
        accum.append(1)

    hangwatch(1, thing1)
    assert sum(accum) == 1

    def thing2():
        sleep(.05)

    assert_raises(RuntimeError, hangwatch, .01, thing2)

    def thing3(msg='foo'):
        raise NotImplementedError(msg)

    with capture():
        assert_raises(NotImplementedError, hangwatch, 1, thing3)

    try:
        with capture():
            hangwatch(1, thing3, 'bar')
    except NotImplementedError as e:
        assert message(e) == 'bar'
Exemple #4
0
    def coerce(self, value, **kwargs):
        if self.query(value):
            return value

        try:
            if self.call_coerce:
                return self.type.coerce(value, **kwargs)
            return self.type(value)
        except Exception as e:
            raise TypeError('Cannot coerce {} to type {}: {}'
                            .format(value, self.type, message(e)))
Exemple #5
0
    def validate(self):
        '''Raise an exception if the object is missing required attributes, or if the attributes are of an invalid type.
        '''
        optional = self._attrs.optional
        optional_none = self._opts.optional_none

        for attr, typ in self._attrs.types.items():
            if not hasattr(self, attr):
                if attr in optional:
                    continue
                raise AttributeError('Required attribute {} not defined'.
                                     format(attr))

            val = getattr(self, attr)
            if optional_none:
                if attr in optional and val is None:
                    continue

            res, e = typ.query_exception(val)
            if not res:
                raise TypeError('Validation error for attribute {}: {}'.
                                format(attr, message(e)))
Exemple #6
0
    def validate(self):
        '''Raise an exception if the object is missing required attributes, or if the attributes are of an invalid type.
        '''
        optional = self._attrs.optional
        optional_none = self._opts.optional_none

        for attr, typ in self._attrs.types.items():
            if not hasattr(self, attr):
                if attr in optional:
                    continue
                raise AttributeError(
                    'Required attribute {} not defined'.format(attr))

            val = getattr(self, attr)
            if optional_none:
                if attr in optional and val is None:
                    continue

            res, e = typ.query_exception(val)
            if not res:
                raise TypeError('Validation error for attribute {}: {}'.format(
                    attr, message(e)))
Exemple #7
0
    def run(self, env, **kwargs):
        verbose = kwargs.get('verbose', False)
        preview = kwargs.get('preview', False)
        # Called from Task.run_preview()
        run_preview = kwargs.get('run_preview', False)
        silent = kwargs.get('silent', env.settings.get('silent', False))

        pre = ''
        if preview and not run_preview:
            pre = kwargs.get('preview_pre', '')

        command, context_name = self.resolve_macros(env, **kwargs)
        if not context_name:
            context = env.default_context
        else:
            context = env.contexts[context_name]
            
        if callable(context.run):
            return context.run(command, env, **kwargs)
        
        cmd = context.run_command(command, env, **kwargs)

        if verbose:
            sys.stdout.write(pre + cmd + '\n')
            sys.stdout.flush()
        
        if not preview:
            args = shlex.split(cmd)
            if args[0] == 'yatr':
                try:
                    from .main import _main
                    _main(*args[1:])
                    return 0
                except Exception as e:
                    eprint(message(e))
                    return 1
            return command_(cmd, silent)
Exemple #8
0
 def coerce(self, value, **kwargs):
     try:
         self.check(value)
     except TypeError as e:
         raise TypeError('Cannot coerce {}: {}'.format(value, message(e)))
     return value
Exemple #9
0
def test_message():
    e = TypeError('err')
    assert message(e) == 'err'
    assert message(TypeError()) == ''
Exemple #10
0
 def failure(self):
     return MatchFailure(message=message(self),
                         seq=self.seq,
                         fails=self.fails)
Exemple #11
0
def test_nested_context():
    from syn.base_utils import nested_context, assign

    class Foo(object):
        pass

    assert not hasattr(Foo, 'a')
    assert not hasattr(Foo, 'b')

    with nested_context([assign]*2, [(Foo, 'a', 1),
                                     (Foo, 'b', 2)]):
        assert Foo.a == 1
        assert Foo.b == 2

    assert not hasattr(Foo, 'a')
    assert not hasattr(Foo, 'b')

    @contextmanager
    def plus1(x):
        yield x+1

    @contextmanager
    def plus2(x):
        yield x+2

    @contextmanager
    def plus3(x):
        yield x+3

    x = 1
    with nested_context([partial(plus1, x),
                         partial(plus2, x),
                         partial(plus3, x)]) as (w, y, z):
        assert x == 1
        assert w == 2
        assert y == 3
        assert z == 4
    
    @contextmanager
    def bad(x):
        raise TypeError('a really really bad error')

    @contextmanager
    def terrible(x):
        if False:
            yield # pragma: no cover

    try:
        with nested_context([plus1, bad], [[x]]*2) as (y, z): pass
    except TypeError as e:
        assert message(e) == 'a really really bad error'

    try:
        with nested_context([plus1, terrible], [[x]]*2) as (y, z): pass
    except RuntimeError as e:
        assert message(e) == "generator didn't yield"

    try:
        with nested_context([plus1, plus2], [[5]]*2) as (y, z):
            assert y == 6
            assert z == 7
            raise TypeError('foobarbaz')
    except TypeError as e:
        assert message(e) == 'foobarbaz'

    @contextmanager
    def listy(x):
        yield [x]*x

    with nested_context([listy, listy], [[1], [2]]) as ret:
        assert ret == ([1], [2, 2])

    with nested_context([listy, listy], [[1], [2]], extend=True) as ret:
        assert ret == (1, 2, 2)
Exemple #12
0
def test_message():
    e = TypeError('err')
    assert message(e) == 'err'
    assert message(TypeError()) == ''
Exemple #13
0
 def failure(self):
     return MatchFailure(message=message(self), 
                         seq=self.seq, 
                         fails=self.fails)