| 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 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 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: |
| 46 |
|
| 47 |
|
| 48 |
|
| 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")) |