def step_env_print(context, config, step): try: env = get_project_environment(context, config) if not isinstance(step, str): env.add_env(step.get('env', []), 'step {}'.format(step.get('name', context.args.ref))) env = env.get_combined() if not env: print("Step environment is empty.") else: yaml_dump(env, stdout) except EnvError as err: exit(1, str(err))
def config_env_print(context): if context.args.global_ and not context.args.project: env = EnvDict((context.settings.get('environment', []), 'global settings')) else: env = get_project_environment(context) try: env = env.get_combined() except EnvError as err: exit(1, str(err)) if not env: print("Environment is empty.") else: yaml_dump(env, stdout)
def confirm_del(step): print('step:') print(' {}'.format(yaml_dump(step.get_data()).replace('\n', '\n '))) while True: i = input("Delete this step? (y/n) ").lower() if i == 'y': return True if i == 'n': return False
def config_print_action(context): all_ = not any( getattr(context.args, k, False) for k in ('global_', 'project')) if context.args.debug: print("---\n# arguments:") data = { k: v for k, v in vars(context.args).items() if k[0] != '_' and not callable(v) } yaml_dump(data, stdout) if all_ or context.args.global_: print("---\n# roman settings:") yaml_dump(context.settings._data, stdout) if all_ or context.args.project: print("---\n# project config") yaml_dump(get_config(context)._data, stdout)
def command_test(self, *command, config=None, config_fn=None, settings=None, exit_code=0): files = {} args = [] if config: if not isinstance(config, str): config = yaml_dump(config) files[config_fn or CONFIG_FN] = config if config_fn: args.extend(('--file', config_fn)) if settings: if not isinstance(settings, str): settings = yaml_dump(settings) files[SETTINGS_FN] = settings args.extend(('--config', SETTINGS_FN)) if len(command) == 1 and isinstance(command[0], str): command = shlex_split(command[0]) args.extend(command) class CliExit(Exception): pass with ExitStack() as ctx: _e = ctx.enter_context _p = lambda *a, **kw: _e(patch(*a, **kw)) # disable '_build' folder fix _p('apluslms_roman.builder.isdir', return_value=True) # mock cli exit, so SystemExit is not raised and we get return code directly exit_mock = _p('apluslms_roman.cli.exit', side_effect=CliExit) # mock files vfs = VFS(files) _p('builtins.open', vfs.mock_open) _p('apluslms_roman.configuration.listdir', vfs.mock_listdir) _p('apluslms_roman.configuration.isfile', vfs.mock_isfile) _p('apluslms_roman.configuration.isdir', return_value=True) _p('apluslms_yamlidator.document.exists', vfs.mock_exists) _p('apluslms_yamlidator.document.makedirs') # capture stdio out, err = ctx.enter_context(capture_output()) try: # call main with self.assertRaises(CliExit, msg="cli.main() didn't call cli.exit()"): cli.main(args=args) # check exit exit_mock.assert_called_once() exit_args = exit_mock.call_args[0] if exit_code is not None: returned_code = exit_args and exit_args[0] or 0 self.assertEqual(returned_code, exit_code, msg="cli.main exited with unexpect return code{}".format( ": {}".format(exit_args[1]) if len(exit_args) == 2 else "" )) # cli.exit writes message to stderr, but it was mocked.. if len(exit_args) == 2: print(exit_args[1], file=err) return self.CommandResult(out.getvalue(), err.getvalue(), vfs) except Exception as e: if exit_mock.called: exit_args = exit_mock.call_args[0] returned_code = exit_args and exit_args[0] or 0 else: returned_code = '?' raise AssertionError( ("cli.main({!r}) -> {} raised an exception\n\n{}\n" "Roman output:\n{}\n{}").format( args, returned_code, format_exc(limit=-10), out.getvalue().strip(), err.getvalue().strip(), ))