Skip to content

Commit

Permalink
tests: Avoid side effect of COLUMNS env var (#9813)
Browse files Browse the repository at this point in the history
Several tests rely on getting the default width of a terminal for
rendering test output. Some PTY environments (in my case the Emacs
shell) set the env var `COLUMNS` to the terminal width, which can
cause several tests to fail due to formatting mismatches.

Prior to running thesee tests, mock out `sys.environ`, removing the
`COLUMNS` env var when present.
  • Loading branch information
gmc444-b authored Dec 17, 2020
1 parent 990b6a6 commit 38409ba
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions mypy/test/testcmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
# Type check the program.
fixed = [python3_path, '-m', 'mypy']
env = os.environ.copy()
env.pop('COLUMNS', None)
env['PYTHONPATH'] = PREFIX
process = subprocess.Popen(fixed + args,
stdout=subprocess.PIPE,
Expand Down
5 changes: 4 additions & 1 deletion mypy/test/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ class TestGetTerminalSize(TestCase):
def test_get_terminal_size_in_pty_defaults_to_80(self) -> None:
# when run using a pty, `os.get_terminal_size()` returns `0, 0`
ret = os.terminal_size((0, 0))
mock_environ = os.environ.copy()
mock_environ.pop('COLUMNS', None)
with mock.patch.object(os, 'get_terminal_size', return_value=ret):
assert get_terminal_width() == 80
with mock.patch.dict(os.environ, values=mock_environ, clear=True):
assert get_terminal_width() == 80

0 comments on commit 38409ba

Please sign in to comment.