예제 #1
0
파일: conftest.py 프로젝트: codrsquad/runez
    def run(self, *args, exe=None, main=UNSET, trace=UNSET):
        """
        Args:
            *args: Command line args
            exe (str | None): Optional, override sys.argv[0] just for this run
            main (callable | None): Optional, override current self.main just for this run
            trace (bool): If True, enable trace logging
        """
        main = _R.rdefault(main, self.main or cli.default_main)
        if len(args) == 1 and hasattr(args[0], "split"):
            # Convenience: allow to provide full command as one string argument
            args = args[0].split()

        self.args = flattened(args, shellify=True)
        with IsolatedLogSetup(adjust_tmp=False):
            with CaptureOutput(dryrun=_R.is_dryrun(), seed_logging=True, trace=_R.rdefault(trace, self.trace)) as logged:
                self.logged = logged
                with TempArgv(self.args, exe=exe):
                    result = self._run_main(main, self.args)
                    if isinstance(result.exception, AssertionError):
                        raise result.exception

                    if result.stdout:
                        logged.stdout.buffer.write(result.stdout)

                    if result.stderr:
                        logged.stderr.buffer.write(result.stderr)

                    if result.exception and not isinstance(result.exception, SystemExit):
                        try:
                            raise result.exception

                        except Exception:
                            LOG.exception("Exited with stacktrace:")

                    self.exit_code = result.exit_code

        if self.logged:
            WrappedHandler.clean_accumulated_logs()
            title = Header.aerated("Captured output for: %s" % quoted(self.args), border="==")
            LOG.info("\n%s\nmain: %s\nexit_code: %s\n%s\n", title, main, self.exit_code, self.logged)
예제 #2
0
    def run_description(self, short_exe=UNSET):
        """
        Args:
            short_exe (str | bool | None): Try to log a compact representation of executable

        Returns:
            (str): Description for self.program run with self.args
        """
        program = self.program
        args = self.args
        if short_exe is UNSET and program and SYS_INFO.venv_bin_folder:
            short_exe = program.startswith(SYS_INFO.venv_bin_folder)

        if isinstance(short_exe, str):
            program = short_exe

        elif short_exe is True:
            program, args = self.shortened_program(program, args)

        else:
            program = short(program)

        return "%s %s" % (program,
                          quoted(args)) if args and program else program
예제 #3
0
 def argv(self):
     """str: Command line invocation, represented to show as greeting"""
     return quoted(sys.argv)
예제 #4
0
파일: conftest.py 프로젝트: codrsquad/runez
 def expect_failure(self, args, *expected, **kwargs):
     spec = RunSpec()
     spec.pop(kwargs)
     self.run(args, **kwargs)
     assert self.failed, "%s succeeded, was expecting failure" % quoted(self.args)
     self.expect_messages(*expected, **spec.to_dict())