Debian Codegraph @

qa/test_stdin.py

raw symbols SCIP
1 import subprocess
2
3 from qa.base import BaseTestCase
4 from qa.shell import echo, gitlint
5 from qa.utils import FILE_ENCODING, TERMINAL_ENCODING
6
7
8 class StdInTests(BaseTestCase):
9 """Integration tests for various STDIN scenarios for gitlint"""
10
11 def test_stdin_pipe(self):
12 """Test piping input into gitlint.
13 This is the equivalent of doing:
14 $ echo "foo" | gitlint
15 """
16 # NOTE: There is no use in testing this with _tty_in=True, because if you pipe something into a command
17 # there never is a TTY connected to stdin (per definition). We're setting _tty_in=False here to be explicit
18 # but note that this is always true when piping something into a command.
19 output = gitlint(echo("WIP: Pïpe test."), _cwd=self.tmp_git_repo, _tty_in=False, _err_to_out=True, _ok_code=[3])
20 self.assertEqualStdout(output, self.get_expected("test_stdin/test_stdin_pipe_1"))
21
22 def test_stdin_pipe_empty(self):
23 """Test the scenario where no TTY is attached and nothing is piped into gitlint. This occurs in
24 CI runners like Jenkins and Gitlab, see https://github.com/jorisroovers/gitlint/issues/42 for details.
25 This is the equivalent of doing:
26 $ echo -n "" | gitlint
27 """
28 commit_msg = "WIP: This ïs a title.\nContent on the sëcond line"
29 self.create_simple_commit(commit_msg)
30
31 # We need to set _err_to_out explicitly for sh to merge stdout and stderr output in case there's
32 # no TTY attached to STDIN
33 # http://amoffat.github.io/sh/sections/special_arguments.html?highlight=_tty_in#err-to-out
34 output = gitlint(echo("-n", ""), _cwd=self.tmp_git_repo, _tty_in=False, _err_to_out=True, _ok_code=[3])
35
36 self.assertEqualStdout(output, self.get_expected("test_stdin/test_stdin_pipe_empty_1"))
37
38 def test_stdin_file(self):
39 """Test the scenario where STDIN is a regular file (stat.S_ISREG = True)
40 This is the equivalent of doing:
41 $ gitlint < myfile
42 """
43 tmp_commit_msg_file = self.create_tmpfile("WIP: STDIN ïs a file test.")
44
45 with open(tmp_commit_msg_file, encoding=FILE_ENCODING) as file_handle: # noqa: SIM117
46 # We need to use subprocess.Popen() here instead of sh because when passing a file_handle to sh, it will
47 # deal with reading the file itself instead of passing it on to gitlint as a STDIN. Since we're trying to
48 # test for the condition where stat.S_ISREG == True that won't work for us here.
49 with subprocess.Popen(
50 "gitlint", stdin=file_handle, cwd=self.tmp_git_repo, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
51 ) as p:
52 output, _ = p.communicate()
53 self.assertEqual(output.decode(TERMINAL_ENCODING), self.get_expected("test_stdin/test_stdin_file_1"))