|
9 | 9 | import os.path |
10 | 10 | import errno |
11 | 11 | import functools |
| 12 | +import subprocess |
12 | 13 | from test import support |
13 | 14 | from test.support import TESTFN |
14 | 15 | from os.path import splitdrive |
@@ -1267,10 +1268,55 @@ def test_move_dir_caseinsensitive(self): |
1267 | 1268 | finally: |
1268 | 1269 | os.rmdir(dst_dir) |
1269 | 1270 |
|
| 1271 | +class TermsizeTests(unittest.TestCase): |
| 1272 | + def test_does_not_crash(self): |
| 1273 | + """Check if get_terminal_size() returns a meaningful value. |
| 1274 | +
|
| 1275 | + There's no easy portable way to actually check the size of the |
| 1276 | + terminal, so let's check if it returns something sensible instead. |
| 1277 | + """ |
| 1278 | + size = shutil.get_terminal_size() |
| 1279 | + self.assertGreater(size.columns, 0) |
| 1280 | + self.assertGreater(size.lines, 0) |
| 1281 | + |
| 1282 | + def test_os_environ_first(self): |
| 1283 | + "Check if environment variables have precedence" |
| 1284 | + |
| 1285 | + with support.EnvironmentVarGuard() as env: |
| 1286 | + env['COLUMNS'] = '777' |
| 1287 | + size = shutil.get_terminal_size() |
| 1288 | + self.assertEqual(size.columns, 777) |
| 1289 | + |
| 1290 | + with support.EnvironmentVarGuard() as env: |
| 1291 | + env['LINES'] = '888' |
| 1292 | + size = shutil.get_terminal_size() |
| 1293 | + self.assertEqual(size.lines, 888) |
| 1294 | + |
| 1295 | + @unittest.skipUnless(os.isatty(sys.__stdout__.fileno()), "not on tty") |
| 1296 | + def test_stty_match(self): |
| 1297 | + """Check if stty returns the same results ignoring env |
| 1298 | +
|
| 1299 | + This test will fail if stdin and stdout are connected to |
| 1300 | + different terminals with different sizes. Nevertheless, such |
| 1301 | + situations should be pretty rare. |
| 1302 | + """ |
| 1303 | + try: |
| 1304 | + size = subprocess.check_output(['stty', 'size']).decode().split() |
| 1305 | + except (FileNotFoundError, subprocess.CalledProcessError): |
| 1306 | + self.skipTest("stty invocation failed") |
| 1307 | + expected = (int(size[1]), int(size[0])) # reversed order |
| 1308 | + |
| 1309 | + with support.EnvironmentVarGuard() as env: |
| 1310 | + del env['LINES'] |
| 1311 | + del env['COLUMNS'] |
| 1312 | + actual = shutil.get_terminal_size() |
| 1313 | + |
| 1314 | + self.assertEqual(expected, actual) |
1270 | 1315 |
|
1271 | 1316 |
|
1272 | 1317 | def test_main(): |
1273 | | - support.run_unittest(TestShutil, TestMove, TestCopyFile) |
| 1318 | + support.run_unittest(TestShutil, TestMove, TestCopyFile, |
| 1319 | + TermsizeTests) |
1274 | 1320 |
|
1275 | 1321 | if __name__ == '__main__': |
1276 | 1322 | test_main() |
0 commit comments