def test_ssh(): env = Env() assert_raises(ValidationError, Context.from_yaml, 'foo', dict(instanceof='ssh')) s = Context.from_yaml( 'foo', dict(instanceof='ssh', opts=dict(user='******', hostname='bar'))) assert s.run_command('ls', env) == 'ssh foo@bar "ls"' env.contexts['foo'] = s sb = Context.from_yaml('bar', dict(instanceof='bash', inside='foo')) assert sb.run_command('ls', env) == 'ssh foo@bar "bash -c \"ls\""'
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)
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'
def test_bash(): env = Env() b = Context.from_yaml( 'foo', dict(instanceof='bash', env=dict(a='1', b='{{bar}}'), opts=dict(v=True, foo='{{a}}'))) assert type(b) is Bash assert b.envvars == dict(a='1', b='{{bar}}') assert b.opts == dict(v=True, foo='{{a}}') envvars, opts = b.resolve_macros(Env(env=dict(a='4', bar='foo'))) assert envvars == dict(a='1', b='foo') assert opts == dict(v=True, foo='4') assert b.run_command('ls', env) == 'bash -c "ls"'
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()
def test_command(): env = Env() c = Command('ls', context='bash') assert _run_command(c, env) == 'bash -c "ls"' env = Env() c = Command('ls') assert _run_command(c, env) == 'ls' env = Env(macros=dict(a='foo', b='bash')) env.resolve_macros() c = Command('ls {{a}}', context='{{b}}') assert _run_command(c, env) == 'bash -c "ls foo"' assert c.command == 'ls {{a}}'
def test_task(): env = Env() with tempfile() as f: c = Command('python --version > ' + f + ' 2>&1') t = Task(name='foo', commands=[c]) codes = t.run(env) strs = read(f) assert codes == [0] assert strs.split()[0] == 'Python' assert strs.split()[1] == sys.version.split()[0] # Test conditional execution t = Task.from_yaml('foo', {'command': 'pwd 2>&1 > ' + f, 'if': 'true'}) codes = t.run(env) assert read(f) == os.getcwd() + '\n' assert codes == [0] t = Task.from_yaml('foo', {'command': 'pwd 2>&1 > ' + f, 'if': 'false'}) codes = t.run(env) assert codes == [] t = Task.from_yaml('foo', {'command': 'pwd 2>&1 > ' + f, 'ifnot': 'true'}) codes = t.run(env) assert codes == [] t = Task.from_yaml('foo', {'command': 'pwd 2>&1 > ' + f, 'ifnot': 'false'}) codes = t.run(env) assert read(f) == os.getcwd() + '\n' assert codes == [0] # Test exit_on_error t = Task.from_yaml('foo', ['true', 'false', 'true']) codes = t.run(env) assert codes == [0, 1]
from yatr import Context, Env class Foo(Context): context_name = 'foo' env = Env(contexts=dict(foo=Foo()))
def test_for(): f = For.from_yaml('x in y') assert f.var == 'x' assert f.in_ == 'y' env = Env(env=dict(x='abc', y='def')) assert_raises(ValidationError, f.resolve_macros, env) env = Env(env=dict(x='abc', y=['d', 'e'])) assert list(f.resolve_macros(env)) == [['x'], [['d', 'e']]] assert list(f.loop(env)) == [ Env(env=dict(x='d', y=['d', 'e'])), Env(env=dict(x='e', y=['d', 'e'])) ] assert_raises(ValidationError, For.from_yaml, '-x in y') assert_raises(ValidationError, For.from_yaml, { 'var': ['x'], 'in': ['x', 'y'] }) f = For.from_yaml({'var': ['x', 'y'], 'in': ['A', 'B']}) env = Env(env=dict(A=['a', 'b'], B=['c', 'd'])) assert list(f.resolve_macros(env)) == [['x', 'y'], [['a', 'b'], ['c', 'd']]] assert list(f.loop(env)) == [ Env(env=dict(x='a', y='c', A=['a', 'b'], B=['c', 'd'])), Env(env=dict(x='a', y='d', A=['a', 'b'], B=['c', 'd'])), Env(env=dict(x='b', y='c', A=['a', 'b'], B=['c', 'd'])), Env(env=dict(x='b', y='d', A=['a', 'b'], B=['c', 'd'])) ] f = For.from_yaml({'var': ['x', 'y'], 'in': ['A', [1, 2]]}) env = Env(env=dict(A=['a', 'b'])) assert list(f.resolve_macros(env)) == [['x', 'y'], [['a', 'b'], [1, 2]]] assert list(f.loop(env)) == [ Env(env=dict(x='a', y=1, A=['a', 'b'])), Env(env=dict(x='a', y=2, A=['a', 'b'])), Env(env=dict(x='b', y=1, A=['a', 'b'])), Env(env=dict(x='b', y=2, A=['a', 'b'])) ] assert_raises(TypeError, For.from_yaml, {}) assert_raises(ValidationError, For.from_yaml, { 'var': ['x', 'y'], 'in': ['A', 'B'], 'bar': 2 })
def test_env(): Updateable()._update_pre({}) Updateable()._update_post({}) with assign(ye, 'INITIAL_MACROS', dict(a='1', b='2')): e = Env() assert e.macros assert e.contexts assert e.default_context assert not e.tasks assert not e.secret_values e1 = Env(macros=dict(a='b', b='{{a}}c', c='d{{b}}')) e2 = Env(macros=dict(a='a', d='{{c}}e')) e3 = Env(macros=dict(a='{{d}}')) e1c = e1.copy() assert_equivalent(e1, e1c) assert_equivalent(e1.macros, e1c.macros) e0 = deepcopy(e1) e0.resolve_macros() assert e0.env == dict(a='b', b='bc', c='dbc') e1.update(e2) e1c = deepcopy(e1) e1.resolve_macros() assert e1.env == dict(a='a', b='ac', c='dac', d='dace') e2.macros['a'] = 'c' e1c.update(e2) e1c2 = deepcopy(e1c) e1c.resolve_macros() assert e1c.env == dict(a='c', b='cc', c='dcc', d='dcce') e1c2.update(e3) assert_raises(ValueError, e1c2.resolve_macros) e = Env(default_task='foo') e.update(Env()) assert e.default_task == 'foo' e.update(Env(default_task='bar')) assert e.default_task == 'bar'
def test_context(): env = Env() c = Context.from_yaml('foo', {}) assert type(c) is Null assert c.run_command('ls', env) == 'ls'
from yatr import Env def foo(value, **kwargs): return '{}_foo'.format(value) def bar(value, **kwargs): return '{}_bar'.format(value) env = Env(jinja_filters=dict(foo=foo), jinja_functions=dict(bar=bar))