def _exec(*args, **kwargs): if sys.version_info[0] == 2: no_command_error = OSError # noqa pylint: disable=undefined-variable,invalid-name else: no_command_error = FileNotFoundError # noqa pylint: disable=undefined-variable pipe = subprocess.PIPE popen_kwargs = {'stdout': pipe, 'stderr': pipe, 'shell': kwargs.get('_tty_out', False)} if '_cwd' in kwargs: popen_kwargs['cwd'] = kwargs['_cwd'] try: p = subprocess.Popen(args, **popen_kwargs) result = p.communicate() except no_command_error: raise CommandNotFound exit_code = p.returncode stdout = ustr(result[0]) stderr = result[1] # 'sh' does not decode the stderr bytes to unicode full_cmd = '' if args is None else ' '.join(args) # If not _ok_code is specified, then only a 0 exit code is allowed ok_exit_codes = kwargs.get('_ok_code', [0]) if exit_code in ok_exit_codes: return ShResult(full_cmd, stdout, stderr, exit_code) # Unexpected error code => raise ErrorReturnCode raise ErrorReturnCode(full_cmd, stdout, stderr, p.returncode)
def __init__(self, full_cmd, stdout, stderr='', exitcode=0): self.full_cmd = full_cmd # TODO(jorisroovers): The 'sh' library by default will merge stdout and stderr. We mimic this behavior # for now until we fully remove the 'sh' library. self.stdout = stdout + ustr(stderr) self.stderr = stderr self.exit_code = exitcode
def run_command(command, *args, **kwargs): args = [command] + list(args) result = _exec(*args, **kwargs) # If we reach this point and the result has an exit_code that is larger than 0, this means that we didn't # get an exception (which is the default sh behavior for non-zero exit codes) and so the user is expecting # a non-zero exit code -> just return the entire result if hasattr(result, 'exit_code') and result.exit_code > 0: return result return ustr(result)
def test_stdin_file(self): """ Test the scenario where STDIN is a regular file (stat.S_ISREG = True) This is the equivalent of doing: $ gitlint < myfile """ tmp_commit_msg_file = self.create_tmpfile("WIP: STDIN ïs a file test.") with io.open(tmp_commit_msg_file, encoding=DEFAULT_ENCODING) as file_handle: # We need to use subprocess.Popen() here instead of sh because when passing a file_handle to sh, it will # deal with reading the file itself instead of passing it on to gitlint as a STDIN. Since we're trying to # test for the condition where stat.S_ISREG == True that won't work for us here. p = subprocess.Popen(u"gitlint", stdin=file_handle, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = p.communicate() self.assertEqual(ustr(output), self.get_expected("test_stdin/test_stdin_file_1"))
def test_stdin_pipe_empty(self): """ Test the scenario where no TTY is attached an nothing is piped into gitlint. This occurs in CI runners like Jenkins and Gitlab, see https://github.com/jorisroovers/gitlint/issues/42 for details. This is the equivalent of doing: $ echo -n "" | gitlint """ commit_msg = u"WIP: This ïs a title.\nContent on the sëcond line" self._create_simple_commit(commit_msg) # We need to set _err_to_out explicitly for sh to merge stdout and stderr output in case there's # no TTY attached to STDIN # http://amoffat.github.io/sh/sections/special_arguments.html?highlight=_tty_in#err-to-out output = gitlint(echo("-n", ""), _cwd=self.tmp_git_repo, _tty_in=False, _err_to_out=True, _ok_code=[3]) self.assertEqual( ustr(output), self.get_expected("test_stdin/test_stdin_pipe_empty_1"))
def assertEqualStdout(self, output, expected): # pylint: disable=invalid-name self.assertIsInstance(output, RunningCommand) output = ustr(output.stdout) output = output.replace('\r', '') self.assertMultiLineEqual(output, expected)