def test_mixed_args(self, task_depchanged): got = [] def py_callable(a, b, changed): got.append(a) got.append(b) got.append(changed) my_action = action.PythonAction(py_callable, ('a', 'b'), task=task_depchanged) my_action.execute() assert got == ['a', 'b', ['changed']]
def test_extra_arg_overwritten(self, task_depchanged): got = [] def py_callable(a, b, changed): got.append(a) got.append(b) got.append(changed) my_action = action.PythonAction(py_callable, ('a', 'b', 'c'), task=task_depchanged) my_action.execute() assert got == ['a', 'b', 'c']
def test_extra_kwarg_overwritten(self, task_depchanged): got = [] def py_callable(a, b, **kwargs): got.append(a) got.append(b) got.append(kwargs['changed']) my_action = action.PythonAction(py_callable, ('a', 'b'), {'changed': 'c'}, task_depchanged) my_action.execute() assert got == ['a', 'b', 'c']
def test_callable_obj(self, task_depchanged): got = [] class CallMe(object): def __call__(self, a, b, changed): got.append(a) got.append(b) got.append(changed) my_action = action.PythonAction(CallMe(), ('a', 'b'), task=task_depchanged) my_action.execute() assert got == ['a', 'b', ['changed']]
def test_init(self): # default values action1 = action.PythonAction(self._func_par) assert action1.args == [] assert action1.kwargs == {} # not a callable pytest.raises(action.InvalidTask, action.PythonAction, "abc") # args not a list pytest.raises(action.InvalidTask, action.PythonAction, self._func_par, "c") # kwargs not a list pytest.raises(action.InvalidTask, action.PythonAction, self._func_par, None, "a")
def test_named_extra_args(self, task_depchanged): got = [] def py_callable(targets, dependencies, changed, task): got.append(targets) got.append(dependencies) got.append(changed) got.append(task) my_action = action.PythonAction(py_callable, task=task_depchanged) my_action.execute() assert got == [['targets'], ['dependencies'], ['changed'], task_depchanged]
def test_action_modifies_task_attributes(self, task_depchanged): def py_callable(targets, dependencies, changed, task): targets.append('new_target') dependencies.append('new_dependency') changed.append('new_changed') my_action = action.PythonAction(py_callable, task=task_depchanged) my_action.execute() assert task_depchanged.file_dep == ['dependencies', 'new_dependency'] assert task_depchanged.targets == ['targets', 'new_target'] assert task_depchanged.dep_changed == ['changed', 'new_changed']
def test_kwonlyargs_minimal(self, task_depchanged): got = [] scope = {'got': got} exec(textwrap.dedent(''' def py_callable(*args, kwonly=None): got.append(args) got.append(kwonly) '''), scope) my_action = action.PythonAction(scope['py_callable'], (1, 2, 3), {'kwonly': 4}, task=task_depchanged) my_action.execute() assert [(1, 2, 3), 4] == got, repr(got)
def test_method(self, task_depchanged): got = [] class CallMe(object): def xxx(self, a, b, changed): got.append(a) got.append(b) got.append(changed) my_action = action.PythonAction(CallMe().xxx, ('a', 'b'), task=task_depchanged) my_action.execute() assert got == ['a', 'b', ['changed']]
def test_kwonlyargs_full(self, task_depchanged): got = [] scope = {'got': got} exec(textwrap.dedent(''' def py_callable(pos, *args, kwonly=None, **kwargs): got.append(pos) got.append(args) got.append(kwonly) got.append(kwargs['foo']) '''), scope) my_action = action.PythonAction(scope['py_callable'], [1,2,3], {'kwonly': 4, 'foo': 5}, task=task_depchanged) my_action.execute() assert [1, (2, 3), 4, 5] == got, repr(got)
def test_task_options(self): got = [] def py_callable(opt1, opt3): got.append(opt1) got.append(opt3) task = FakeTask([], [], [], { 'opt1': '1', 'opt2': 'abc def', 'opt3': 3 }) my_action = action.PythonAction(py_callable, task=task) my_action.execute() assert ['1', 3] == got, repr(got)
def test_functionParametersArgs(self): my_action = action.PythonAction(self._func_par,args=(2,2,25)) my_action.execute()
def test_functionParametersKwargs(self): my_action = action.PythonAction(self._func_par, kwargs={'par1':2,'par2':2,'par3':25}) my_action.execute()
def test_fail_bool(self): def fail_sample():return False my_action = action.PythonAction(fail_sample) got = my_action.execute() assert isinstance(got, TaskFailed)
def test_no_extra_args(self, task_depchanged): def py_callable(): return True my_action = action.PythonAction(py_callable, task=task_depchanged) my_action.execute()
def test_functionParameters(self): my_action = action.PythonAction(self._func_par,args=(2,2), kwargs={'par3':25}) my_action.execute()
def test_captureStdout(self): my_action = action.PythonAction(self.write_stdout) my_action.execute() assert "this is stdout S\n" == my_action.out, repr(my_action.out)
def test_values(self): def vvv(): return {'x': 5, 'y':10} my_action = action.PythonAction(vvv) my_action.execute() assert {'x': 5, 'y':10} == my_action.values
def test_result(self): def vvv(): return "my value" my_action = action.PythonAction(vvv) my_action.execute() assert "my value" == my_action.result
def test_str(self): def str_sample(): return True my_action = action.PythonAction(str_sample) assert "Python: function" in str(my_action) assert "str_sample" in str(my_action)
def test_functionParametersFail(self): my_action = action.PythonAction(self._func_par, args=(2,3), kwargs={'par3':25}) got = my_action.execute() assert isinstance(got, TaskFailed)
def test_success_dict(self): def success_sample():return {} my_action = action.PythonAction(success_sample) # nothing raised it was successful my_action.execute()
def test_repr(self): def repr_sample(): return True my_action = action.PythonAction(repr_sample) assert "<PythonAction: '%s'>" % repr(repr_sample) == repr(my_action)
def test_error_object(self): # anthing but None, bool, string or dict def error_sample(): return object() my_action = action.PythonAction(error_sample) got = my_action.execute() assert isinstance(got, TaskError)
def test_result_dict(self): def vvv(): return {'xxx': "my value"} my_action = action.PythonAction(vvv) my_action.execute() assert {'xxx': "my value"} == my_action.result
def test_error_taskerror(self): def error_sample(): return TaskError("so sad") ye_olde_action = action.PythonAction(error_sample) ret = ye_olde_action.execute() assert str(ret).endswith("so sad\n")
def test_captureStderr(self): my_action = action.PythonAction(self.write_stderr) my_action.execute() assert "this is stderr S\n" == my_action.err, repr(my_action.err)
def test_error_exception(self): def error_sample(): raise Exception("asdf") my_action = action.PythonAction(error_sample) got = my_action.execute() assert isinstance(got, TaskError)
def test_noCaptureStdout(self, capsys): my_action = action.PythonAction(self.write_stdout) my_action.execute(out=sys.stdout) got = capsys.readouterr()[0] assert "this is stdout S\n" == got, repr(got)
def test_meta_arg_default_disallowed(self, task_depchanged): def py_callable(a, b, changed=None): pass my_action = action.PythonAction(py_callable, ('a', 'b'), task=task_depchanged) pytest.raises(action.InvalidTask, my_action.execute)