def test_lint_staged_stdin(self): """ Tests linting a staged commit. Gitint should lint the passed commit message andfetch additional meta-data from the underlying repository. The easiest way to test this is by inspecting `--debug` output. This is the equivalent of doing: echo "WIP: Pïpe test." | gitlint --staged --debug """ # Create a commit first, before we stage changes. This ensures the repo is properly initialized. self.create_simple_commit(u"Sïmple title.\n") # Add some files, stage them: they should show up in the debug output as changed file filename1 = self.create_file(self.tmp_git_repo) git("add", filename1, _cwd=self.tmp_git_repo) filename2 = self.create_file(self.tmp_git_repo) git("add", filename2, _cwd=self.tmp_git_repo) output = gitlint(echo(u"WIP: Pïpe test."), "--staged", "--debug", _cwd=self.tmp_git_repo, _tty_in=False, _err_to_out=True, _ok_code=[3]) # Determine variable parts of expected output expected_kwargs = self.get_debug_vars_last_commit() expected_kwargs.update({'changed_files': sstr(sorted([filename1, filename2]))}) # It's not really possible to determine the "Date: ..." line that is part of the debug output as this date # is not taken from git but instead generated by gitlint itself. As a workaround, we extract the date from the # gitlint output using a regex, parse the date to ensure the format is correct, and then pass that as an # expected variable. matches = re.search(r'^Date:\s+(.*)', str(output), re.MULTILINE) if matches: expected_date = arrow.get(str(matches.group(1)), "YYYY-MM-DD HH:mm:ss Z").format("YYYY-MM-DD HH:mm:ss Z") expected_kwargs['staged_date'] = expected_date self.assertEqualStdout(output, self.get_expected("test_commits/test_lint_staged_stdin_1", expected_kwargs)) self.assertEqual(output.exit_code, 3)
def test_stdin_pipe(self): """ Test piping input into gitlint. This is the equivalent of doing: $ echo "foo" | gitlint """ # NOTE: There is no use in testing this with _tty_in=True, because if you pipe something into a command # there never is a TTY connected to stdin (per definition). We're setting _tty_in=False here to be explicit # but note that this is always true when piping something into a command. output = gitlint(echo(u"WIP: Pïpe test."), _tty_in=False, _err_to_out=True, _ok_code=[3]) self.assertEqualStdout( output, self.get_expected("test_stdin/test_stdin_pipe_1"))
def test_git_errors(self): # Repo has no commits: caused by `git log` empty_git_repo = self.create_tmp_git_repo() output = gitlint(_cwd=empty_git_repo, _tty_in=True, _ok_code=[self.GIT_CONTEXT_ERROR_CODE]) expected = u"Current branch has no commits. Gitlint requires at least one commit to function.\n" self.assertEqualStdout(output, expected) # Repo has no commits: caused by `git rev-parse` output = gitlint(echo(u"WIP: Pïpe test."), "--staged", _cwd=empty_git_repo, _tty_in=False, _err_to_out=True, _ok_code=[self.GIT_CONTEXT_ERROR_CODE]) self.assertEqualStdout(output, expected)
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 = "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( output, self.get_expected("test_stdin/test_stdin_pipe_empty_1"))