コード例 #1
0
ファイル: test_py.py プロジェクト: mbodenhamer/syn
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'
コード例 #2
0
ファイル: test_debug.py プロジェクト: mbodenhamer/syn
def test_call_trace():
    from syn.base_utils import call_trace, reset_trace, capture, CallTrace

    def foo():
        bar()

    def bar():
        pass

    foo()
    tr = sys.gettrace()
    with capture() as (out, err):
        with reset_trace():
            call_trace()
            foo()
    assert sys.gettrace() is tr

    assert out.getvalue() == 'foo\n bar\n__exit__\n reset_trace\n'
    assert err.getvalue() == ''

    t = CallTrace()
    with capture() as (out, err):
        t(sys._getframe(), 'call', None)
    
    assert out.getvalue() == 'test_call_trace\n'
    assert err.getvalue() == ''
    assert t.indent == 1

    t(sys._getframe(), 'return', None)
    assert t.indent == 0
コード例 #3
0
ファイル: test_py.py プロジェクト: pombredanne/syn-1
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'
コード例 #4
0
def test_call_trace():
    from syn.base_utils import call_trace, reset_trace, capture, CallTrace

    def foo():
        bar()

    def bar():
        pass

    foo()
    tr = sys.gettrace()
    with capture() as (out, err):
        with reset_trace():
            call_trace()
            foo()
    assert sys.gettrace() is tr

    assert out.getvalue() == 'foo\n bar\n__exit__\n reset_trace\n'
    assert err.getvalue() == ''

    t = CallTrace()
    with capture() as (out, err):
        t(sys._getframe(), 'call', None)

    assert out.getvalue() == 'test_call_trace\n'
    assert err.getvalue() == ''
    assert t.indent == 1

    t(sys._getframe(), 'return', None)
    assert t.indent == 0
コード例 #5
0
ファイル: test_context.py プロジェクト: mbodenhamer/syn
def test_capture():
    from syn.base_utils import capture, assign
    from six.moves import cStringIO

    oout = cStringIO()
    oerr = cStringIO()

    with assign(sys, 'stdout', oout):
        with assign(sys, 'stderr', oerr):
            print("Outside")
            sys.stderr.write('Err1\n')
            with capture() as (out, err):
                print("Inside")
                sys.stderr.write('Err!\n')

                assert out.getvalue() == 'Inside\n'
                assert err.getvalue() == 'Err!\n'

            print("Outside2")
            sys.stderr.write('Err2\n')

            assert out.getvalue() == 'Inside\n'
            assert err.getvalue() == 'Err!\n'
            
    print("Outside")
    
    assert oout.getvalue() == 'Outside\nOutside2\n'
    assert oerr.getvalue() == 'Err1\nErr2\n'
コード例 #6
0
ファイル: test_task.py プロジェクト: pombredanne/yatr
def test_task():
    t = Task(commands=[Command('{{a}}'), Command('{{b}}')])

    env = Env()
    assert_raises(UndefinedError, t.run_preview, env)

    env = Env(macros=dict(a='ls', b='pwd'))
    env.resolve_macros()
    assert t.run_preview(env) == 'ls\npwd\n'

    t = Task(commands=[Command('ls')])
    assert t == Task.from_yaml('foo', 'ls')

    t = Task(commands=[Command('ls'), Command('pwd')])
    assert t == Task.from_yaml('foo', ['ls', 'pwd'])

    t = Task(commands=[Command('ls', context='bash')])
    assert t == Task.from_yaml('foo', dict(command='ls', context='bash'))

    env = Env()
    t = Task(commands=[Command('1/1', context='python')])
    assert t.run(env) == [0]
    t = Task(commands=[Command('1/0', context='python')])
    with capture():
        assert t.run(env) == [1]

    assert_raises(ValidationError, Task.from_yaml, 'foo', 1)
コード例 #7
0
ファイル: test_base.py プロジェクト: pombredanne/yatr
def test_fix_functions():
    from yatr import Env

    class Foo(object):
        pass
    
    def fooer():
        ret = Foo()
        ret.hex = 'foo'
        return ret

    def identity(s=None, **kwargs):
        return s

    env = Env(macros=dict(abc='def'),
              jinja_functions=dict(abc=identity))
    with capture() as (out, err):
        with assign(ybase, 'uuid4', fooer):
            out = fix_functions("{{abc('sdf')}} {{abc}} {{abc('qwe')}}", 
                                {'abc'}, env)
            assert out == "{{abc_foo('sdf')}} {{abc}} {{abc_foo('qwe')}}"
            assert resolve(out, env.macros, jenv=env.jenv) == 'sdf def qwe'

            out = fix_functions("{{abc('ghi')}}", {'abc'}, env)
            assert out == "{{abc_foo('ghi')}}"
            assert resolve(out, env.macros, jenv=env.jenv) == 'ghi'
コード例 #8
0
ファイル: test_py.py プロジェクト: mbodenhamer/syn
def test_eprint():
    from syn.base_utils import eprint, capture

    with capture() as (out, err):
        eprint('abc')

    assert out.getvalue() == ''
    assert err.getvalue() == 'abc\n'
コード例 #9
0
ファイル: test_py.py プロジェクト: pombredanne/syn-1
def test_eprint():
    from syn.base_utils import eprint, capture

    with capture() as (out, err):
        eprint('abc')

    assert out.getvalue() == ''
    assert err.getvalue() == 'abc\n'
コード例 #10
0
ファイル: test_context.py プロジェクト: pombredanne/yatr
def test_python():
    env = Env()
    p = Context.from_yaml('foo', dict(instanceof='python'))
    assert type(p) is Python

    assert p.run_command('1/1', env) == '1/1'
    assert p.run('1/1', env) == 0

    with capture() as (out, err):
        assert p.run('1/0', env) == 1
    assert out.getvalue() == ''
    assert 'zero' in err.getvalue()
コード例 #11
0
ファイル: test_str.py プロジェクト: mbodenhamer/syn
def test_safe_print():
    from syn.base_utils import safe_unicode, safe_print
    from syn.base_utils import capture, chrs

    with capture() as (out, err):
        s = safe_unicode('\xac\xab')
        safe_print(s)

    if PY2:
        assert out.getvalue() == '\xc2\xac\xc2\xab\n'
    else:
        assert out.getvalue() == chrs([172, 171, 10])
コード例 #12
0
ファイル: test_str.py プロジェクト: pombredanne/syn-1
def test_safe_print():
    from syn.base_utils import safe_unicode, safe_print
    from syn.base_utils import capture, chrs

    with capture() as (out, err):
        s = safe_unicode('\xac\xab')
        safe_print(s)

    if PY2:
        assert out.getvalue() == '\xc2\xac\xc2\xab\n'
    else:
        assert out.getvalue() == chrs([172, 171, 10])
コード例 #13
0
ファイル: test_main.py プロジェクト: mbodenhamer/depman
def test_main():
    def invoke(cmd):
        if isinstance(cmd, STR):
            _main(*shlex.split(cmd))
        else:
            _main(*cmd)

    # Version
    with assign(sys, 'argv', ['', 'version']):
        with capture() as (out, err):
            main()
    assert out.getvalue() == 'depman {}\n'.format(dver)

    # Satisfy
    with assign(aptd, 'command', MagicMock()):
        with assign(pipd, 'command', MagicMock()):
            with assign(Pip, '_pkgs', dict(lxml='', PyYAML='')):
                with assign(Apt, '_pkgs', {
                        'libxml2-dev': '',
                        'libxslt1-dev': ''
                }):
                    invoke(['satisfy', '-f', DEPS1])
                    assert aptd.command.call_count == 0
                    assert pipd.command.call_count == 0

    # Validate
    with assign(aptd, 'command', MagicMock()):
        with assign(pipd, 'command', MagicMock()):
            with assign(Pip, '_pkgs', dict(lxml='', PyYAML='')):
                invoke(['validate', '-f', DEPS1])
                assert aptd.command.call_count == 0
                assert pipd.command.call_count == 0

    # Validate the example file(s)
    invoke(['validate', '-f', DEPSEX])

    # Export
    eout = '''# Auto-generated by depman 0.3.4
gevent<=1.0.2
numpy
openopt
six
syn>=0.0.14'''

    invoke(['export', 'prod', '-f', DEPSEX, '-t', 'pip', '-o', TEST1])
    with open(TEST1, 'rt') as f:
        assert f.read() == eout

    #Version
    invoke(['version'])
コード例 #14
0
ファイル: test_main.py プロジェクト: mbodenhamer/depman
def test_main():
    def invoke(cmd):
        if  isinstance(cmd, STR):
            _main(*shlex.split(cmd))
        else:
            _main(*cmd)

    # Version
    with assign(sys, 'argv', ['', 'version']):
        with capture() as (out, err):
            main()
    assert out.getvalue() == 'depman {}\n'.format(dver)

    # Satisfy
    with assign(aptd, 'command', MagicMock()):
        with assign(pipd, 'command', MagicMock()):
            with assign(Pip, '_pkgs', dict(lxml='', PyYAML='')):
                with assign(Apt, '_pkgs', {'libxml2-dev': '',
                                          'libxslt1-dev': ''}):
                    invoke(['satisfy', '-f', DEPS1])
                    assert aptd.command.call_count == 0
                    assert pipd.command.call_count == 0

    # Validate
    with assign(aptd, 'command', MagicMock()):
        with assign(pipd, 'command', MagicMock()):
            with assign(Pip, '_pkgs', dict(lxml='', PyYAML='')):
                invoke(['validate', '-f', DEPS1])
                assert aptd.command.call_count == 0
                assert pipd.command.call_count == 0

    # Validate the example file(s)
    invoke(['validate', '-f', DEPSEX])

    # Export
    eout = '''# Auto-generated by depman 0.3.4
gevent<=1.0.2
numpy
openopt
six
syn>=0.0.14'''

    invoke(['export', 'prod', '-f', DEPSEX, '-t', 'pip', '-o', TEST1])
    with open(TEST1, 'rt') as f:
        assert f.read() == eout

    #Version
    invoke(['version'])
コード例 #15
0
ファイル: test_ne.py プロジェクト: mbodenhamer/syn
def test_diffexplorer():
    l1 = [1, 2, 3]
    l2 = [1, 2, 4]

    x = DiffExplorer(l1, l2)
    assert x.display() == u'A: 1\nB: 1'
    assert x.current_value == (1, 1)
    x.step()
    assert x.display() == u'A: 2\nB: 2'
    x.down()
    assert x.display() == u'A: 2\nB: 2'
    x.up()
    assert x.display() == u'A: 2\nB: 2'
    x.step()
    assert x.display() == u'A: 3\nB: 4'
    assert_raises(ExplorationError, x.step)
    x.step(-1)
    assert x.display() == u'A: 2\nB: 2'
    x.step()
    assert x.display() == u'A: 1\nB: 1'
    assert_raises(ExplorationError, x.step)
    
    x.reset()
    assert list(x.depth_first()) == [(l1, l2), (1, 1), (2, 2), (3, 4)]

    def last_lines(si):
        return si.getvalue().split('\n')[-3:-1]

    l1 = [1, [2, 3], [[4]]]
    l2 = [1, [2, 6], [[5]]]
    r = DiffExplorer(ValueExplorer(l1), ValueExplorer(l2))
    with capture() as (out, err):
        r._eval('c')
        assert last_lines(out) == ['A: 1', 'B: 1']

        r._eval('l')
        assert last_lines(out) == ['A: [1, [2, 3], [[4]]]',
                                   'B: [1, [2, 6], [[5]]]']

        r._eval('n 2')
        r._eval('c')
        assert last_lines(out) == ['A: [[4]]', 'B: [[5]]']

        r._eval('d 2')
        r._eval('c')
        assert last_lines(out) == ['A: 4', 'B: 5']

        r._eval('u 2')
        r._eval('c')
        assert last_lines(out) == ['A: [[4]]', 'B: [[5]]']

        r._eval('n -1')
        r._eval('c')
        assert last_lines(out) == ['A: [2, 3]', 'B: [2, 6]']

    d1 = dict(a = 1)
    d2 = dict(a = 2)
    r = DiffExplorer(d1, d2)
    with capture() as (out, err):
        r._eval('c')
        assert last_lines(out) == ['A: 1', 'B: 2']

        r._eval('l')
        assert last_lines(out) == ["A: {'a': 1}", "B: {'a': 2}"]

    class Bar(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    b1 = Bar(1, [2, 3, 'abc'])
    b2 = Bar(1, [2, 3, 'adc'])

    accum = []
    def fake_input(prompt):
        accum.append(1)
        if sum(accum) <= 1:
            return 'f'
        return 'q'

    r = find_ne(b1,b2)    
    with assign(repl, 'raw_input', fake_input):
        r()
コード例 #16
0
ファイル: test_py.py プロジェクト: pombredanne/syn-1
def test_run_all_tests():
    from syn.base_utils import run_all_tests

    var1 = [1]
    var2 = [2]
    var3 = [3]
    var4 = [4]

    def test():
        var1.append(5)

    def test_blah_blah():
        var2.append(6)

    def blank_test():
        var3.append(7)

    def some_other_func():
        var4.append(8)

    assert 'run_all_tests' in locals()
    with assign(sys, 'argv', []):
        with capture() as (out, err):
            run_all_tests(locals(), verbose=True)
            assert out.getvalue() == 'blank_test\ntest\ntest_blah_blah\n'
            assert err.getvalue() == ''

    assert var1 == [1, 5]
    assert var2 == [2, 6]
    assert var3 == [3, 7]
    assert var4 == [4]

    with assign(sys, 'argv',
                ['', '', '--include', 'test,test_blah_blah,some_other_func']):
        with capture() as (out, err):
            run_all_tests(locals(), verbose=True)
            assert out.getvalue() == 'test\ntest_blah_blah\n'
            assert err.getvalue() == ''

    assert var1 == [1, 5, 5]
    assert var2 == [2, 6, 6]
    assert var3 == [3, 7]
    assert var4 == [4]

    some_other_func()
    assert var4 == [4, 8]

    def test_error_func():
        raise TypeError('Testing exception trace printing')

    with assign(sys, 'argv', []):
        with capture() as (out, err):
            run_all_tests(locals(), verbose=True, print_errors=True)
            assert out.getvalue() == ('blank_test\ntest\ntest_blah_blah\n'
                                      'test_error_func\n')
            assert err.getvalue().split('\n')[-2] == \
                'TypeError: Testing exception trace printing'

    with assign(sys, 'argv', ['--print-errors']):
        with capture() as (out, err):
            run_all_tests(locals(), verbose=True, print_errors=False)
            assert out.getvalue() == ('blank_test\ntest\ntest_blah_blah\n'
                                      'test_error_func\n')
            assert err.getvalue().split('\n')[-2] == \
                'TypeError: Testing exception trace printing'

    with assign(sys, 'argv', []):
        with capture() as (out, err):
            assert_raises(TypeError,
                          run_all_tests,
                          locals(),
                          verbose=True,
                          print_errors=False)
コード例 #17
0
ファイル: test_repl.py プロジェクト: pombredanne/syn-1
def test_repl():
    from syn.base_utils import repl_command
    import syn.base_utils.repl as repl

    class R1(repl.REPL):
        commands = dict(repl.REPL.commands)
        command_help = dict(repl.REPL.command_help)

        @repl_command('a', 'foo')
        def foo(self):
            print("foo!")

        @repl_command('b', 'RuntimeError')
        def rterr(self):
            raise RuntimeError('error!')

    def last_line(si):
        return si.getvalue().split('\n')[-2]

    r1 = R1('r1> ')
    with capture() as (out, err):
        r1._eval('a')
        assert last_line(out) == 'foo!'

        r1._eval('zzz')
        assert last_line(out) == 'Unrecognized command: zzz'

        r1._eval('b')
        assert last_line(out) == 'Error in b: ***RuntimeError***: error!'

        r1._eval('e "1 + 2"')
        assert last_line(out) == '3'

        r1._eval('h')
        assert last_line(out) == ' q               quit'

        fi_count = []

        def fake_input(prompt):
            fi_count.append(1)

            N = sum(fi_count)
            if N <= 4:
                return 'e {}'.format(N)
            else:
                raise EOFError

        def fake_input2(prompt):
            fi_count.append(1)
            return 'q'

        assert sum(fi_count) == 0
        with assign(repl, 'raw_input', fake_input):
            r1()
            assert last_line(out) == '4'

        assert sum(fi_count) == 5
        with assign(repl, 'raw_input', fake_input2):
            r1()
        assert sum(fi_count) == 6

    # Test overriding e in a REPL subclass
    class R2(R1):
        @repl_command('e', 'bar')
        def bar(self, *args):
            print("bar!")

    r2 = R2('r2> ')
    with capture() as (out, err):
        r2._eval('a')
        assert last_line(out) == 'foo!'

        r2._eval('e "1 + 2"')
        assert last_line(out) == 'bar!'

    assert len(repl.REPL.commands) == 4
    assert len(repl.REPL.command_help) == 4
コード例 #18
0
ファイル: test_repl.py プロジェクト: mbodenhamer/syn
def test_repl():
    from syn.base_utils import repl_command
    import syn.base_utils.repl as repl

    class R1(repl.REPL):
        commands = dict(repl.REPL.commands)
        command_help = dict(repl.REPL.command_help)

        @repl_command('a', 'foo')
        def foo(self):
            print("foo!")

        @repl_command('b', 'RuntimeError')
        def rterr(self):
            raise RuntimeError('error!')


    def last_line(si):
        return si.getvalue().split('\n')[-2]

    r1 = R1('r1> ')
    with capture() as (out, err):
        r1._eval('a')
        assert last_line(out) == 'foo!'

        r1._eval('zzz')
        assert last_line(out) == 'Unrecognized command: zzz'

        r1._eval('b')
        assert last_line(out) == 'Error in b: ***RuntimeError***: error!'

        r1._eval('e "1 + 2"')
        assert last_line(out) == '3'

        r1._eval('h')
        assert last_line(out) == ' q               quit'

        fi_count = []
        def fake_input(prompt):
            fi_count.append(1)

            N = sum(fi_count)
            if N <= 4:
                return 'e {}'.format(N)
            else:
                raise EOFError

        def fake_input2(prompt):
            fi_count.append(1)
            return 'q'

        assert sum(fi_count) == 0
        with assign(repl, 'raw_input', fake_input):
            r1()
            assert last_line(out) == '4'
            
        assert sum(fi_count) == 5
        with assign(repl, 'raw_input', fake_input2):
            r1()
        assert sum(fi_count) == 6

    # Test overriding e in a REPL subclass
    class R2(R1):
        @repl_command('e', 'bar')
        def bar(self, *args):
            print("bar!")

    r2 = R2('r2> ')
    with capture() as (out, err):
        r2._eval('a')
        assert last_line(out) == 'foo!'

        r2._eval('e "1 + 2"')
        assert last_line(out) == 'bar!'

    assert len(repl.REPL.commands) == 4
    assert len(repl.REPL.command_help) == 4
コード例 #19
0
ファイル: test_py.py プロジェクト: mbodenhamer/syn
def test_run_all_tests():
    from syn.base_utils import run_all_tests

    var1 = [1]
    var2 = [2]
    var3 = [3]
    var4 = [4]

    def test():
        var1.append(5)
    def test_blah_blah():
        var2.append(6)
    def blank_test():
        var3.append(7)
    def some_other_func():
        var4.append(8)
        
    assert 'run_all_tests' in locals()
    with assign(sys, 'argv', []):
        with capture() as (out, err):
            run_all_tests(locals(), verbose=True)
            assert out.getvalue() == 'blank_test\ntest\ntest_blah_blah\n'
            assert err.getvalue() == ''

    assert var1 == [1,5]
    assert var2 == [2,6]
    assert var3 == [3,7]
    assert var4 == [4]

    with assign(sys, 'argv', ['', '', '--include', 
                              'test,test_blah_blah,some_other_func']):
        with capture() as (out, err):
            run_all_tests(locals(), verbose=True)
            assert out.getvalue() == 'test\ntest_blah_blah\n'
            assert err.getvalue() == ''
    
    assert var1 == [1,5,5]
    assert var2 == [2,6,6]
    assert var3 == [3,7]
    assert var4 == [4]

    some_other_func()
    assert var4 == [4,8]

    def test_error_func():
        raise TypeError('Testing exception trace printing')

    with assign(sys, 'argv', []):
        with capture() as (out, err):
            run_all_tests(locals(), verbose=True, print_errors=True)
            assert out.getvalue() == ('blank_test\ntest\ntest_blah_blah\n'
                                      'test_error_func\n')
            assert err.getvalue().split('\n')[-2] == \
                'TypeError: Testing exception trace printing'

    with assign(sys, 'argv', ['--print-errors']):
        with capture() as (out, err):
            run_all_tests(locals(), verbose=True, print_errors=False)
            assert out.getvalue() == ('blank_test\ntest\ntest_blah_blah\n'
                                      'test_error_func\n')
            assert err.getvalue().split('\n')[-2] == \
                'TypeError: Testing exception trace printing'

    with assign(sys, 'argv', []):
        with capture() as (out, err):
            assert_raises(TypeError, run_all_tests, locals(), verbose=True, 
                          print_errors=False)
コード例 #20
0
def test_netypes():
    from syn.types.a import NEType, NotEqual, DiffersAtIndex, DiffersAtKey, \
        DifferentLength, DifferentTypes, SetDifferences, KeyDifferences, \
        DiffersAtAttribute

    class Foo(object):
        def __init__(self, a):
            self.a = a

    n = NEType(1, 2)
    assert str(n) == repr(n)
    x = n.explorer()
    assert x.current_value == (1, 2)

    assert n == NEType(1, 2)
    assert n != NEType(1, 3)
    assert n != NotEqual(1, 2)

    n = NotEqual(1, 2)
    assert str(n) == '1 != 2'

    accum = []

    def fake_explorer():
        def func():
            accum.append(1)

        return func

    assert sum(accum) == 0
    with capture() as (out, err):
        with assign(n, 'explorer', fake_explorer):
            n()
    assert sum(accum) == 1
    assert out.getvalue() == '1 != 2\n'

    l1 = [1, 2, 3]
    l2 = [1, 4, 3]
    n = DiffersAtIndex(l1, l2, 1)
    assert n.explorer().current_value == (2, 4)
    assert n.message() == 'Sequences differ at index 1: 2 != 4'

    assert DiffersAtIndex(l1, l2, 1) == DiffersAtIndex(l1, l2, 1)
    assert DiffersAtIndex(l1, l2, 1) != DiffersAtIndex(l1, l2, 2)
    assert DiffersAtIndex(l1, l2, 1) != DiffersAtIndex(l1, l1, 1)

    d1 = dict(a=1, b=2)
    d2 = dict(a=1, b=3)
    n = DiffersAtKey(d1, d2, key='b')
    assert n.explorer().current_value == (2, 3)
    assert n.message() == 'Mappings differ at key "b": 2 != 3'

    assert DiffersAtKey(d1, d2, 'a') == DiffersAtKey(d1, d2, 'a')
    assert DiffersAtKey(d1, d2, 'a') != DiffersAtKey(d1, d2, 'b')
    assert DiffersAtKey(d1, d2, 'a') != DiffersAtKey(d1, d1, 'a')

    l1 = [1, 2]
    l2 = [1, 2, 3]
    n = DifferentLength(l1, l2)
    assert n.message() == 'Different lengths: 2 != 3'

    l1 = [1, 2]
    l2 = (1, 2, 3)
    n = DifferentTypes(l1, l2)
    assert n.message() == ('Different types: {} != {}'.format(
        str(list), str(tuple)))

    s1 = {1, 2, 3}
    s2 = {2, 3, 4}
    n = SetDifferences(s1, s2)
    assert n.message() == 'Exclusive items: {}'.format({1, 4})

    d1 = dict(a=1, b=2)
    d2 = dict(b=2)
    n = KeyDifferences(d1, d2)
    assert n.message() == 'Exclusive keys: {}'.format({'a'})
    n = KeyDifferences(d2, d1)
    assert n.message() == 'Exclusive keys: {}'.format({'a'})

    f1 = Foo(1)
    f2 = Foo(2)
    n = DiffersAtAttribute(f1, f2, 'a')
    assert n.message() == 'Objects differ at attribute "a": 1 != 2'
    assert n != NotEqual(1, 2)
    assert n.explorer().current_value == (1, 2)
コード例 #21
0
ファイル: test_task.py プロジェクト: pombredanne/yatr
def _run_command(c, env):
    with capture() as (out, err):
        c.run(env, preview=True, verbose=True, run_preview=True)
    return out.getvalue().rstrip()
コード例 #22
0
def test_valueexplorer():
    x = ValueExplorer(1)

    assert x.value == 1
    assert x.current_value == 1
    assert x.display() == u'1'
    with capture() as (out, err):
        x.command_display_value()
    assert out.getvalue() == '1\n'
    assert_raises(ExplorationError, x.step)
    assert_raises(ExplorationError, x.down)
    assert_raises(ExplorationError, x.up)

    x = ValueExplorer([1, 2, 3])
    assert x.current_value == 1
    x.step()
    assert x.current_value == 2
    x.step()
    assert x.current_value == 3
    assert_raises(ExplorationError, x.step)
    assert_raises(ExplorationError, x.up)
    x.down()
    assert x.value == 3
    assert x.current_value == 3
    x.up()
    assert x.value == [1, 2, 3]
    assert x.current_value == 3
    assert_raises(ExplorationError, x.step)
    x.down()
    assert x.value == 3
    x.up()
    assert x.current_value == 3
    x.step(-1)
    assert x.current_value == 2
    x.step()
    assert x.current_value == 1
    assert_raises(ExplorationError, x.step)
    x.step(1)
    assert x.current_value == 2
    x.step()
    assert x.current_value == 3
    assert_raises(ExplorationError, x.step)
    x.reset()
    assert list(x.depth_first()) == [[1, 2, 3], 1, 2, 3]

    x = ValueExplorer([])
    assert x.value == []
    assert x.current_value is None
    assert_raises(ExplorationError, x.step)
    assert list(x.depth_first()) == [[]]

    l = [1, [2, 3], 4]
    x = ValueExplorer(l)
    assert list(x.depth_first()) == [l, 1, [2, 3], 2, 3, 4]

    x = ValueExplorer(l, index=1)
    assert list(x.depth_first()) == [l, [2, 3], 2, 3, 4]

    x = ValueExplorer(l, index=2)
    assert list(x.depth_first()) == [l, 4]

    x = ValueExplorer(l, index=3)
    assert list(x.depth_first()) == []

    l = [1, [2, 3], [[4]]]
    x = ValueExplorer(l)
    assert list(x.depth_first()) == [l, 1, [2, 3], 2, 3, [[4]], [4], 4]

    d = dict(a=1, b=2)
    x = ValueExplorer(d)
    assert set(list(x.depth_first())[1:]) == {1, 2}

    d = dict(a=1, b=2, c=(3, 4))
    x = ValueExplorer(d)
    assert set(list(x.depth_first())[1:]) == {1, 2, (3, 4), 3, 4}

    assert ValueExplorer(d, key='a').current_value == 1
    assert ValueExplorer(d, key='b').current_value == 2
    assert ValueExplorer(d, key='c').current_value == (3, 4)
    assert_raises(ExplorationError, ValueExplorer, d, key='d')

    d = dict(a=1, b=2, c=dict(a=3, b=4))
    x = ValueExplorer(d)
    dfl = list(item for item in x.depth_first() if not isinstance(item, dict))
    assert set(dfl) == {1, 2, 3, 4}
    x.reset()
    assert set(x.depth_first(leaves_only=True)) == {1, 2, 3, 4}

    s = set()
    x = ValueExplorer(d, key='c')
    assert x.current_value == dict(a=3, b=4)
    x.down()
    s.add(x.current_value)
    x.step()
    s.add(x.current_value)
    assert_raises(ExplorationError, x.step)
    assert x.at_end
    assert s == {3, 4}

    s = 'abcd'
    x = ValueExplorer(s)
    assert list(x.depth_first()) == ['abcd', 'a', 'b', 'c', 'd']

    s = ''
    x = ValueExplorer(s)
    assert list(x.depth_first()) == ['']

    x = ValueExplorer([])
    assert list(x.depth_first()) == [[]]

    class Foo(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    f = Foo(1, 2)
    x = ValueExplorer(f)
    assert list(x.depth_first()) == [f, 1, 2]
    #assert list(x.depth_first(leaves_only=True)) == [1, 2]

    x = ValueExplorer(f, attr='b')
    assert x.value is f
    assert x.current_value == 2
    assert x.attr == 'b'
    assert x.index == 1

    assert_raises(ExplorationError, ValueExplorer, f, attr='c')

    def last_line(si):
        return si.getvalue().split('\n')[-2]

    l = [1, [2, 3], [[4]]]
    r = ValueExplorer(l)
    with capture() as (out, err):
        r._eval('c')
        assert last_line(out) == '1'

        r._eval('l')
        assert last_line(out) == '[1, [2, 3], [[4]]]'

        r._eval('n 2')
        r._eval('c')
        assert last_line(out) == '[[4]]'

        r._eval('d 2')
        r._eval('c')
        assert last_line(out) == '4'

        r._eval('u 2')
        r._eval('c')
        assert last_line(out) == '[[4]]'

        r._eval('n -1')
        r._eval('c')
        assert last_line(out) == '[2, 3]'
コード例 #23
0
ファイル: test_ne.py プロジェクト: mbodenhamer/syn
def test_valueexplorer():
    x = ValueExplorer(1)

    assert x.value == 1
    assert x.current_value == 1
    assert x.display() == u'1'
    with capture() as (out, err):
        x.command_display_value()
    assert out.getvalue() == '1\n'
    assert_raises(ExplorationError, x.step)
    assert_raises(ExplorationError, x.down)
    assert_raises(ExplorationError, x.up)

    x = ValueExplorer([1, 2, 3])
    assert x.current_value == 1
    x.step()
    assert x.current_value == 2
    x.step()
    assert x.current_value == 3
    assert_raises(ExplorationError, x.step)
    assert_raises(ExplorationError, x.up)
    x.down()
    assert x.value == 3
    assert x.current_value == 3
    x.up()
    assert x.value == [1, 2, 3]
    assert x.current_value == 3
    assert_raises(ExplorationError, x.step)
    x.down()
    assert x.value == 3
    x.up()
    assert x.current_value == 3
    x.step(-1)
    assert x.current_value == 2
    x.step()
    assert x.current_value == 1
    assert_raises(ExplorationError, x.step)
    x.step(1)
    assert x.current_value == 2
    x.step()
    assert x.current_value == 3
    assert_raises(ExplorationError, x.step)
    x.reset()
    assert list(x.depth_first()) == [[1, 2, 3], 1, 2, 3]

    x = ValueExplorer([])
    assert x.value == []
    assert x.current_value is None
    assert_raises(ExplorationError, x.step)
    assert list(x.depth_first()) == [[]]

    l = [1, [2, 3], 4]
    x = ValueExplorer(l)
    assert list(x.depth_first()) == [l, 1, [2, 3], 2, 3, 4]

    x = ValueExplorer(l, index=1)
    assert list(x.depth_first()) == [l, [2, 3], 2, 3, 4]

    x = ValueExplorer(l, index=2)
    assert list(x.depth_first()) == [l, 4]

    x = ValueExplorer(l, index=3)
    assert list(x.depth_first()) == []

    l = [1, [2, 3], [[4]]]
    x = ValueExplorer(l)
    assert list(x.depth_first()) == [l, 1, [2, 3], 2, 3, [[4]], [4], 4]

    d = dict(a=1, b=2)
    x = ValueExplorer(d)
    assert set(list(x.depth_first())[1:]) == {1, 2}

    d = dict(a=1, b=2, c=(3, 4))
    x = ValueExplorer(d)
    assert set(list(x.depth_first())[1:]) == {1, 2, (3, 4), 3, 4}

    assert ValueExplorer(d, key='a').current_value == 1
    assert ValueExplorer(d, key='b').current_value == 2
    assert ValueExplorer(d, key='c').current_value == (3, 4)
    assert_raises(ExplorationError, ValueExplorer, d, key='d')

    d = dict(a=1, b=2, c=dict(a=3, b=4))
    x = ValueExplorer(d)
    dfl = list(item for item in x.depth_first() if not isinstance(item, dict))
    assert set(dfl) == {1, 2, 3, 4}
    x.reset()
    assert set(x.depth_first(leaves_only=True)) == {1, 2, 3, 4}

    s = set()
    x = ValueExplorer(d, key='c')
    assert x.current_value == dict(a=3, b=4)
    x.down()
    s.add(x.current_value)
    x.step()
    s.add(x.current_value)
    assert_raises(ExplorationError, x.step)
    assert x.at_end
    assert s == {3, 4}

    s = 'abcd'
    x = ValueExplorer(s)
    assert list(x.depth_first()) == ['abcd', 'a', 'b', 'c', 'd']

    s = ''
    x = ValueExplorer(s)
    assert list(x.depth_first()) == ['']
    
    x = ValueExplorer([])
    assert list(x.depth_first()) == [[]]

    
    class Foo(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b


    f = Foo(1, 2)
    x = ValueExplorer(f)
    assert list(x.depth_first()) == [f, 1, 2]
    #assert list(x.depth_first(leaves_only=True)) == [1, 2]

    x = ValueExplorer(f, attr='b')
    assert x.value is f
    assert x.current_value == 2
    assert x.attr == 'b'
    assert x.index == 1

    assert_raises(ExplorationError, ValueExplorer, f, attr='c')


    def last_line(si):
        return si.getvalue().split('\n')[-2]

    l = [1, [2, 3], [[4]]]
    r = ValueExplorer(l)
    with capture() as (out, err):
        r._eval('c')
        assert last_line(out) == '1'

        r._eval('l')
        assert last_line(out) == '[1, [2, 3], [[4]]]'

        r._eval('n 2')
        r._eval('c')
        assert last_line(out) == '[[4]]'

        r._eval('d 2')
        r._eval('c')
        assert last_line(out) == '4'

        r._eval('u 2')
        r._eval('c')
        assert last_line(out) == '[[4]]'

        r._eval('n -1')
        r._eval('c')
        assert last_line(out) == '[2, 3]'
コード例 #24
0
ファイル: test_main.py プロジェクト: pombredanne/yatr
def test_bash_completions():
    settings = DEFAULT_SETTINGS

    with chdir(DIR):
        out = find_bash_completions(['--cache-dir', '/app', 'print'], 1)
        assert out == []

        out = find_bash_completions(['-f', '/app', 'print'], 1)
        assert out == []

        out = find_bash_completions(['--yatrfile', '/app', 'print'], 1)
        assert out == []

        out = find_bash_completions(['-'], 0)
        assert out == OPTION_STRINGS

        out = find_bash_completions(['p'], 0)
        assert out == ['print']

        out = find_bash_completions(['xyz'], 0)
        assert out == []

        out = find_bash_completions(['-m', 'a'], 1)
        assert out == ['a']

        out = find_bash_completions(['--macro', 'a'], 1)
        assert out == ['a']

        out = find_bash_completions(['-m', 'a='], 1)
        assert out == []

        out = find_bash_completions(['-s', 's'], 1)
        assert out == ['silent']

        out = find_bash_completions(['--setting', 's'], 1)
        assert out == ['silent']

        out = find_bash_completions(['-s', 'silent='], 1)
        assert out == []

        out = find_bash_completions(['--dump-path', 'p'], 1)
        assert out == ['print']

        out = find_bash_completions(['print', 'xyz'], 1)
        assert out == []

        out = find_bash_completions(['-i', '/app'], 1)
        assert out == []

        out = find_bash_completions(['-o', '/app'], 1)
        assert out == []

        out = find_bash_completions([], 0)
        assert out == ['print']

        out = find_bash_completions(['-m'], 1)
        assert out == ['a', 'b', 'c']

        out = find_bash_completions(['--macro'], 1)
        assert out == ['a', 'b', 'c']

        out = find_bash_completions(['-s'], 1)
        assert out == settings

        out = find_bash_completions(['--setting'], 1)
        assert out == settings

        out = find_bash_completions(['-m', 'a=xyz'], 2)
        assert out == ['print']

        out = find_bash_completions(['-m', 'a=xyz', 'print'], 3)
        assert out == []

        out = find_bash_completions(['-i'], 1)
        assert out == []

        out = find_bash_completions(['-o'], 1)
        assert out == []

        with capture() as (out, err):
            dump_bash_completions(['-m'], 1)
        assert out.getvalue() == 'a b c\n'

        with capture() as (out, err):
            _main('--dump-bash-completions', '1', '--macro')
        assert out.getvalue() == 'a b c\n'

    with tempdir() as d:
        with chdir(d):
            out = find_bash_completions(['-'], 0)
            assert out == OPTION_STRINGS

            out = find_bash_completions([], 0)
            assert out == []

            out = find_bash_completions(['-m'], 1)
            assert out == []

            out = find_bash_completions(['-s'], 1)
            assert out == settings

            yfpath = os.path.join(DIR, 'yatrfile.yml')
            out = find_bash_completions(['-f', yfpath], 2)
            assert out == ['print']

            out = find_bash_completions(['--yatrfile', yfpath], 2)
            assert out == ['print']

            out = find_bash_completions(['-f', yfpath, '-s'], 3)
            assert out == settings

            yfpath = os.path.join(DIR, 'yatrfile.yaml')
            out = find_bash_completions(['-f', yfpath], 2)
            assert out == []

            yfpath = os.path.join(d, 'yatrfile.yml')
            shutil.copyfile(os.path.join(DIR, 'test2.yml'), yfpath)
            out = find_bash_completions([], 0)
            assert out == ['bad', 'good']

            with tempdir() as d2:
                cd = os.path.join(d2, 'foo')
                out = find_bash_completions(['--cache-dir', cd], 2)
                assert out == ['bad', 'good']

                with open(yfpath, 'r') as f:
                    data = f.read()
                out = data.replace('bad', 'dad')
                with open(yfpath, 'w') as f:
                    f.write(out)

                out = find_bash_completions(['--cache-dir', cd], 2)
                assert out == ['dad', 'good']

    spath = os.path.join(os.path.abspath(os.path.dirname(ybase.__file__)),
                         'scripts/completion/yatr')
    dpath = '/etc/bash_completion.d/yatr'
    with assign(shutil, 'copyfile', MagicMock()):
        with capture() as (out, err):
            _main('--install-bash-completions')
        assert out.getvalue() == BASH_COMPLETION_MESSAGE + '\n'
        assert shutil.copyfile.call_count == 1
        shutil.copyfile.assert_any_call(spath, dpath)
コード例 #25
0
def test_diffexplorer():
    l1 = [1, 2, 3]
    l2 = [1, 2, 4]

    x = DiffExplorer(l1, l2)
    assert x.display() == u'A: 1\nB: 1'
    assert x.current_value == (1, 1)
    x.step()
    assert x.display() == u'A: 2\nB: 2'
    x.down()
    assert x.display() == u'A: 2\nB: 2'
    x.up()
    assert x.display() == u'A: 2\nB: 2'
    x.step()
    assert x.display() == u'A: 3\nB: 4'
    assert_raises(ExplorationError, x.step)
    x.step(-1)
    assert x.display() == u'A: 2\nB: 2'
    x.step()
    assert x.display() == u'A: 1\nB: 1'
    assert_raises(ExplorationError, x.step)

    x.reset()
    assert list(x.depth_first()) == [(l1, l2), (1, 1), (2, 2), (3, 4)]

    def last_lines(si):
        return si.getvalue().split('\n')[-3:-1]

    l1 = [1, [2, 3], [[4]]]
    l2 = [1, [2, 6], [[5]]]
    r = DiffExplorer(ValueExplorer(l1), ValueExplorer(l2))
    with capture() as (out, err):
        r._eval('c')
        assert last_lines(out) == ['A: 1', 'B: 1']

        r._eval('l')
        assert last_lines(out) == [
            'A: [1, [2, 3], [[4]]]', 'B: [1, [2, 6], [[5]]]'
        ]

        r._eval('n 2')
        r._eval('c')
        assert last_lines(out) == ['A: [[4]]', 'B: [[5]]']

        r._eval('d 2')
        r._eval('c')
        assert last_lines(out) == ['A: 4', 'B: 5']

        r._eval('u 2')
        r._eval('c')
        assert last_lines(out) == ['A: [[4]]', 'B: [[5]]']

        r._eval('n -1')
        r._eval('c')
        assert last_lines(out) == ['A: [2, 3]', 'B: [2, 6]']

    d1 = dict(a=1)
    d2 = dict(a=2)
    r = DiffExplorer(d1, d2)
    with capture() as (out, err):
        r._eval('c')
        assert last_lines(out) == ['A: 1', 'B: 2']

        r._eval('l')
        assert last_lines(out) == ["A: {'a': 1}", "B: {'a': 2}"]

    class Bar(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    b1 = Bar(1, [2, 3, 'abc'])
    b2 = Bar(1, [2, 3, 'adc'])

    accum = []

    def fake_input(prompt):
        accum.append(1)
        if sum(accum) <= 1:
            return 'f'
        return 'q'

    r = find_ne(b1, b2)
    with assign(repl, 'raw_input', fake_input):
        r()
コード例 #26
0
ファイル: test_ne.py プロジェクト: mbodenhamer/syn
def test_netypes():
    from syn.types.a import NEType, NotEqual, DiffersAtIndex, DiffersAtKey, \
        DifferentLength, DifferentTypes, SetDifferences, KeyDifferences, \
        DiffersAtAttribute

    class Foo(object):
        def __init__(self, a):
            self.a = a

    n = NEType(1, 2)
    assert str(n) == repr(n)
    x = n.explorer()
    assert x.current_value == (1, 2)
    
    assert n == NEType(1, 2)
    assert n != NEType(1, 3)
    assert n != NotEqual(1, 2)

    n = NotEqual(1, 2)
    assert str(n) == '1 != 2'
    
    accum = []
    def fake_explorer():
        def func():
            accum.append(1)
        return func

    assert sum(accum) == 0
    with capture() as (out, err):
        with assign(n, 'explorer', fake_explorer):
            n()
    assert sum(accum) == 1
    assert out.getvalue() == '1 != 2\n'

    l1 = [1, 2, 3]
    l2 = [1, 4, 3]
    n = DiffersAtIndex(l1, l2, 1)
    assert n.explorer().current_value == (2, 4)
    assert n.message() == 'Sequences differ at index 1: 2 != 4'

    assert DiffersAtIndex(l1, l2, 1) == DiffersAtIndex(l1, l2, 1)
    assert DiffersAtIndex(l1, l2, 1) != DiffersAtIndex(l1, l2, 2)
    assert DiffersAtIndex(l1, l2, 1) != DiffersAtIndex(l1, l1, 1)

    d1 = dict(a=1, b=2)
    d2 = dict(a=1, b=3)
    n = DiffersAtKey(d1, d2, key='b')
    assert n.explorer().current_value == (2, 3)
    assert n.message() == 'Mappings differ at key "b": 2 != 3'

    assert DiffersAtKey(d1, d2, 'a') == DiffersAtKey(d1, d2, 'a')
    assert DiffersAtKey(d1, d2, 'a') != DiffersAtKey(d1, d2, 'b')
    assert DiffersAtKey(d1, d2, 'a') != DiffersAtKey(d1, d1, 'a')

    l1 = [1, 2]
    l2 = [1, 2, 3]
    n = DifferentLength(l1, l2)
    assert n.message() == 'Different lengths: 2 != 3'

    l1 = [1, 2]
    l2 = (1, 2, 3)
    n = DifferentTypes(l1, l2)
    assert n.message() == ('Different types: {} != {}'
                           .format(str(list), str(tuple)))

    s1 = {1, 2, 3}
    s2 = {2, 3, 4}
    n = SetDifferences(s1, s2)
    assert n.message() == 'Exclusive items: {}'.format({1, 4})

    d1 = dict(a=1, b=2)
    d2 = dict(b=2)
    n = KeyDifferences(d1, d2)
    assert n.message() == 'Exclusive keys: {}'.format({'a'})
    n = KeyDifferences(d2, d1)
    assert n.message() == 'Exclusive keys: {}'.format({'a'})

    f1 = Foo(1)
    f2 = Foo(2)
    n = DiffersAtAttribute(f1, f2, 'a')
    assert n.message() == 'Objects differ at attribute "a": 1 != 2'
    assert n != NotEqual(1, 2)
    assert n.explorer().current_value == (1, 2)
コード例 #27
0
ファイル: test_main.py プロジェクト: pombredanne/yatr
def test_main():
    assert_raises(RuntimeError, search_rootward, '/')

    with assign(sys, 'exit', lambda *args, **kwargs: None):
        with capture() as (out, err):
            _main('--version')
        assert out.getvalue() == 'yatr {}\n'.format(yver)

        with assign(sys, 'argv', ['', '--version']):
            with capture() as (out, err):
                main()
        assert out.getvalue() == 'yatr {}\n'.format(yver)

        with capture() as (out, err):
            _main('-f', os.path.join(DIR, 'yatrfile1.yml'), '--validate')
        assert out.getvalue() == 'Validation successful\n'

        # Test default task
        with capture() as (out, err):
            _main('-f', TEST2, '-p')
        assert out.getvalue() == 'true\n'

        # Test --dump and task command-line arg passing
        with chdir(os.path.join(DIR, 'foo')):
            with capture() as (out, err):
                _main('--dump')
            assert ('a = abc\nb = abcdef\nc = abcdefghi\n'
                    'pwd = {}\n'.format(DIR)) in out.getvalue()

            _main('print', '5')
            assert read(OUT) == 'abcdefghi 5\n'

            with capture() as (out, err):
                _main()
            assert out.getvalue() == USAGE + '\n'

        # Test task referencing in task definition
        _main('-f', TEST3, 'a')
        assert read(OUT) == 'abc\n'

        _main('-f', TEST3, 'b')
        assert read(OUT) == 'abc\ndef\n'

        # Test --render
        with chdir(DIR):
            with capture() as (out, err):
                _main('--render', '-i', os.path.join(DIR, 'test1.j2'), '-o',
                      os.path.join(DIR, 'test1.txt'))
            assert out.getvalue() == ''
            assert err.getvalue() == ''

            with open(os.path.join(DIR, 'test1.txt'), 'r') as f:
                txt = f.read()
            assert txt == 'First abc, then abcdef, then abcdefghi.'

            assert_raises(RuntimeError, _main, '--render')
            assert_raises(RuntimeError, _main, '--render', '-i',
                          '/foo/bar/baz')
            assert_raises(RuntimeError, _main, '--render', '-i',
                          os.path.join(DIR, 'test1.j2'))

        # Test --pull
        ybase.resolve_url(URL)
        with assign(ybase, 'download', MagicMock()):
            ybase.resolve_url(URL)
            assert ybase.download.call_count == 0

            _main('-f', TEST1)
            assert ybase.download.call_count == 0

            _main('-f', TEST1, '--pull')
            assert ybase.download.call_count == 1

            with tempdir() as d:
                assert_raises(ValidationError, _main, '-f', TEST1,
                              '--cache-dir', d)
            assert ybase.download.call_count == 2

        # Test --validate
        with capture() as (out, err):
            _main('-f', TEST4)
        assert out.getvalue() == ''
        assert err.getvalue() == ''
        assert_raises(UndefinedError, _main, '-f', TEST4, '--validate')

        # Test default_task inheritance
        with capture() as (out, err):
            _main('-f', TEST5, '-p')
        assert out.getvalue() == 'true\n'

        with capture() as (out, err):
            _main('-f', TEST6, '-p')
        assert out.getvalue() == 'false\ntrue\n'

        # Test for and list macros
        with capture() as (out, err):
            _main('-f', TEST7, '-p', 'foo')
        assert out.getvalue() == 'echo x x w 0\n' \
            'echo x x z 1\n' \
            'echo x y w 2\n' \
            'echo x y z 3\n'

        with capture() as (out, err):
            _main('-f', TEST7, '-p', '-s', 'loop_count_macro=count', 'bar')
        assert out.getvalue() == 'echo 1 0\n' \
            'echo 2 1\n' \
            'echo 3 2\n' \
            'echo 4 3\n'

        # Test commands function
        with capture() as (out, err):
            _main('-f', TEST8, '-i', TEST8_IN, '-o', TEST8_OUT, '--render')
        with open(TEST8_OUT, 'r') as f:
            txt = f.read()
            assert txt == '#!/bin/bash\necho foo\necho bar\necho baz\n'

        # Test env function
        with capture() as (out, err):
            _main('-f', TEST8, '-p', 'home')
        assert out.getvalue() == 'echo {}\n'.format(os.environ['PATH'])

        with capture() as (out, err):
            _main('-f', TEST8, '-p', 'bar')
        assert out.getvalue() == 'echo baz\n'

        with capture() as (out, err):
            with setitem(os.environ, 'YATR_BAR', 'foo'):
                _main('-f', TEST8, '-p', 'bar')
        assert out.getvalue() == 'echo foo\n'

        with capture() as (out, err):
            _main('-f', TEST8, '-p', 'baz')
        assert out.getvalue() == 'echo baz_foo foo_bar\n'

        # Test files
        with tempdir() as d:
            _main('-f', TEST9, 'foo', d)
            path = os.path.join(d, 'test1.txt')
            assert os.path.isfile(path)
            with open(path, 'r') as f:
                txt = f.read()
            assert txt == 'foo\n'

            with capture() as (out, err):
                _main('-f', TEST9, '-p', 'bar', d)
            assert out.getvalue() == 'cat "{}/test1.txt"\n'.format(d)

        # Test task calling with arguments
        with capture() as (out, err):
            _main('-f', TEST10, '-p', 'x')
        assert out.getvalue() == 'echo 5 3 10\n'

        with capture() as (out, err):
            _main('-f', TEST10, '-p', 'x', '4')
        assert out.getvalue() == 'echo 5 4 4\n'

        with capture() as (out, err):
            _main('-f', TEST10, '-p', 'y', '4')
        assert out.getvalue() == 'echo 7 1 4\n'

        with capture() as (out, err):
            _main('-f', TEST10, '-p', 'z', '4')
        assert out.getvalue() == 'echo 1 6 4\n' \
            'echo 2 6 4\n' \
            'echo 3 6 4\n'

        with capture() as (out, err):
            _main('-f', TEST10, '-p', 'w', '4')
        assert out.getvalue() == 'echo 5 4 4\n' \
            'echo 7 1 4\n' \
            'echo 5 4 4\n'

        # Test _main() calls
        with capture() as (out, err):
            _main('-f', TEST11, 'err')
        assert out.getvalue() == ''
        assert 'does not exist' in err.getvalue()

        with tempfile() as f:
            _main('-f', TEST11, 'bar', 'b', f)
            with open(f, 'r') as f_:
                txt = f_.read().rstrip()
            assert txt == 'b\na\nc\nd\nb'

        with tempfile() as f:
            with capture() as (out, err):
                _main('-f', TEST11, 'baz', 'a', f)
            assert out.getvalue() == ''
            assert 'does not exist' in err.getvalue()

            with capture():
                _main('-f', TEST11, '-s', 'exit_on_error=false', 'baz', 'a', f)
            with open(f, 'r') as f_:
                txt = f_.read().rstrip()
            assert txt == 'a'

        # Test command-line arg validation
        assert_raises(Exception, _main, '-f', TEST12, '--validate')
        with capture() as (out, err):
            _main('-f', TEST12, '--validate', 'NONE', 'a')
        assert out.getvalue() == 'Validation successful\n'

        # Test --cache
        with tempdir() as d:
            txt = 'abc123'
            url = 'http://foo.com'
            tmpfile = os.path.join(d, 'temp.txt')
            cached = cached_path(url, d)

            with open(tmpfile, 'w') as f:
                f.write(txt)

            _main('--cache-dir', d, '--cache', '-i', tmpfile, '-o', url)

            with open(cached, 'r') as f:
                out = f.read()
            assert out == txt

            assert_raises(RuntimeError, _main, '--cache')
            assert_raises(RuntimeError, _main, '--cache', '-i', tmpfile)

        # Test dict macros
        with capture() as (out, err):
            _main('-f', TEST13, '--validate')

        with capture() as (out, err):
            _main('-f', TEST13, '-p', 'foo')
            assert out.getvalue() == 'echo def abc\n'

        # Verify example
        with chdir(os.path.join(DIR, 'example')):
            # Test --dump-path
            with capture() as (out, err):
                _main('--dump-path')
            assert out.getvalue() == \
                '{}\n'.format(os.path.join(DIR, 'example/yatrfile.yml'))

            # Test -p
            with capture() as (out, err):
                _main('-p', 'bar', 'foo')
            assert out.getvalue() == 'echo bar\necho bar baz foo\n'

            with capture() as (out, err):
                _main('-p', 'cond4')
            assert out.getvalue() == 'ifnot: false\n\techo bar\n'

            # Test --dump
            with capture() as (out, err):
                _main('-f', 'C.yml', '--dump')
            assert 'a = baz\nb = ghi\nc = xyz\n' in out.getvalue()

            # Test -m
            with capture() as (out, err):
                _main('-f', 'C.yml', '-m' 'a=zab', '-m', 'd=jkl', '--dump')
            assert 'a = zab\nb = ghi\nc = xyz\nd = jkl\n' in out.getvalue()

            # Cover settings
            _main('-f', 'D.yml', '-s', 'silent=false')

        # Verify render example
        with chdir(os.path.join(DIR, 'example/render')):
            with capture() as (out, err):
                _main('render')
            assert out.getvalue() == ''

            with open(DOCKERFILE, 'r') as f:
                txt = f.read()
            assert txt == DFRENDER

            with capture() as (out, err):
                _main('-p', 'build')
            assert out.getvalue() == \
                ('yatr --render -i Dockerfile.j2 -o Dockerfile\n'
                 'docker build -t foo:latest .\n')