From b3a11ac3dba190dce355620935ef45260d6e721e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20E=C3=9Fer?= Date: Wed, 22 Sep 2021 14:57:11 +0200 Subject: Use only POSIX commands and options The hard-coded use of /bin/bash and of long options for nl does only work on Linux based systems with GNU textutils. If POSIX commands and options are used, this test becomes portable to other systems than Linux. --- tests/test_expect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_expect.py b/tests/test_expect.py index c37e159..243bc34 100755 --- a/tests/test_expect.py +++ b/tests/test_expect.py @@ -411,7 +411,7 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def test_before_across_chunks(self): # https://github.com/pexpect/pexpect/issues/478 child = pexpect.spawn( - '''/bin/bash -c "openssl rand -base64 {} 2>/dev/null | head -500 | nl --number-format=rz --number-width=5 2>&1 ; echo 'PATTERN!!!'"'''.format(1024 * 1024 * 2), + '''/bin/sh -c "openssl rand -base64 {} 2>/dev/null | head -500 | nl -n rz -w 5 2>&1 ; echo 'PATTERN!!!'"'''.format(1024 * 1024 * 2), searchwindowsize=128 ) child.expect(['PATTERN']) -- cgit v1.2.1 From a48c3021c4c4cffc537122ac14d2c340ae75ad62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20E=C3=9Fer?= Date: Wed, 22 Sep 2021 15:00:08 +0200 Subject: Do not send more characters than can be buffered by cat The cat command on FreeBSD blocks if the default input buffer fills and not output can be sent. The test used to generate more than 10 KB of data before accepting the output generated by the cat program, causing cat to block (stop reading input) until the output buffer has been drained. There is no reason to send long lines in this test, therefore it is easily possible to perform the tests with lines that do not cause more than 4 KB (the default input buffer size on FreeBSD) to be sent before output from the cat program is checked. In order to not depend on such buffer sizes pexpect needs to check for output from the spawned command and buffer it, before it gets to execute the expect() function. Apparently this is not happening and thus the system dependent buffer size limit makes this test fail if its capacity is exceeded. --- tests/test_expect.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_expect.py b/tests/test_expect.py index 243bc34..c9087f4 100755 --- a/tests/test_expect.py +++ b/tests/test_expect.py @@ -456,7 +456,7 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): child = pexpect.spawn('cat', echo=False) child.sendline('BEGIN') for i in range(100): - child.sendline('foo' * 100) + child.sendline('foo' * 10) e = child.expect([b'xyzzy', pexpect.TIMEOUT], searchwindowsize=10, timeout=0.001) self.assertEqual(e, 1) @@ -473,7 +473,7 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): child = pexpect.spawn('cat', echo=False) child.sendline('BEGIN') for i in range(100): - child.sendline('foo' * 100) + child.sendline('foo' * 10) e = child.expect([b'xyzzy', pexpect.TIMEOUT], searchwindowsize=10, timeout=0.5) self.assertEqual(e, 1) -- cgit v1.2.1 From b717990ac74691f2081f4ab0c63d8829db9e8fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20E=C3=9Fer?= Date: Wed, 22 Sep 2021 17:11:46 +0200 Subject: Replace deprecated assertRaisesRegexp with assertRaisesRegex The assertRaisesRegexp function appeared in Python-3.1 and was renamed to assertRaisesRegex in Python-3.2. --- tests/PexpectTestCase.py | 2 +- tests/test_expect.py | 8 ++++---- tests/test_misc.py | 4 ++-- tests/test_popen_spawn.py | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/PexpectTestCase.py b/tests/PexpectTestCase.py index 307437e..5d7a168 100644 --- a/tests/PexpectTestCase.py +++ b/tests/PexpectTestCase.py @@ -97,7 +97,7 @@ class PexpectTestCase(unittest.TestCase): raise AssertionError("%s was not raised" % excClass) @contextlib.contextmanager - def assertRaisesRegexp(self, excClass, pattern): + def assertRaisesRegex(self, excClass, pattern): import re try: yield diff --git a/tests/test_expect.py b/tests/test_expect.py index c9087f4..5553d28 100755 --- a/tests/test_expect.py +++ b/tests/test_expect.py @@ -569,13 +569,13 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def test_bad_arg(self): p = pexpect.spawn('cat') - with self.assertRaisesRegexp(TypeError, '.*must be one of'): + with self.assertRaisesRegex(TypeError, '.*must be one of'): p.expect(1) - with self.assertRaisesRegexp(TypeError, '.*must be one of'): + with self.assertRaisesRegex(TypeError, '.*must be one of'): p.expect([1, b'2']) - with self.assertRaisesRegexp(TypeError, '.*must be one of'): + with self.assertRaisesRegex(TypeError, '.*must be one of'): p.expect_exact(1) - with self.assertRaisesRegexp(TypeError, '.*must be one of'): + with self.assertRaisesRegex(TypeError, '.*must be one of'): p.expect_exact([1, b'2']) def test_timeout_none(self): diff --git a/tests/test_misc.py b/tests/test_misc.py index 7784759..3dc2b6e 100755 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -214,7 +214,7 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase): # Force an invalid state to test isalive child.ptyproc.terminated = 0 try: - with self.assertRaisesRegexp(pexpect.ExceptionPexpect, + with self.assertRaisesRegex(pexpect.ExceptionPexpect, ".*" + expect_errmsg): child.isalive() finally: @@ -224,7 +224,7 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase): def test_bad_arguments_suggest_fdpsawn(self): " assert custom exception for spawn(int). " expect_errmsg = "maybe you want to use fdpexpect.fdspawn" - with self.assertRaisesRegexp(pexpect.ExceptionPexpect, + with self.assertRaisesRegex(pexpect.ExceptionPexpect, ".*" + expect_errmsg): pexpect.spawn(1) diff --git a/tests/test_popen_spawn.py b/tests/test_popen_spawn.py index fca7493..10d7032 100644 --- a/tests/test_popen_spawn.py +++ b/tests/test_popen_spawn.py @@ -110,13 +110,13 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def test_bad_arg(self): p = PopenSpawn('cat') - with self.assertRaisesRegexp(TypeError, '.*must be one of'): + with self.assertRaisesRegex(TypeError, '.*must be one of'): p.expect(1) - with self.assertRaisesRegexp(TypeError, '.*must be one of'): + with self.assertRaisesRegex(TypeError, '.*must be one of'): p.expect([1, b'2']) - with self.assertRaisesRegexp(TypeError, '.*must be one of'): + with self.assertRaisesRegex(TypeError, '.*must be one of'): p.expect_exact(1) - with self.assertRaisesRegexp(TypeError, '.*must be one of'): + with self.assertRaisesRegex(TypeError, '.*must be one of'): p.expect_exact([1, b'2']) def test_timeout_none(self): -- cgit v1.2.1 From 242c367ad64b275e372a66e1e6831b54905b60d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20E=C3=9Fer?= Date: Wed, 22 Sep 2021 17:26:08 +0200 Subject: Disable baacketed-paste mode in bash 5.1 and newer (issue #669) The bash versions starting with 5.1 have bracketed paste mode enabled by default, leading to test failures in replwrap.bash(), e.g.: self = def test_multiline(self): bash = replwrap.bash() res = bash.run_command("echo '1 2\n3 4'") > self.assertEqual(res.strip().splitlines(), ['1 2', '3 4']) E AssertionError: Lists differ: ['\x1b[?2004l', '\x1b[?2004h\x1b[?2004l', '1 2', '3 4', '\x1b[?2004h'] != ['1 2', '3 4'] E E First differing element 0: E '\x1b[?2004l' E '1 2' E E First list contains 3 additional elements. E First extra element 2: E '1 2' E E - ['\x1b[?2004l', '\x1b[?2004h\x1b[?2004l', '1 2', '3 4', '\x1b[?2004h'] E + ['1 2', '3 4'] With bracketed test mode disabled, the tests that were affected by the inserted escape sequences succeed. --- pexpect/bashrc.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pexpect/bashrc.sh b/pexpect/bashrc.sh index c734ac9..d75d1a5 100644 --- a/pexpect/bashrc.sh +++ b/pexpect/bashrc.sh @@ -14,3 +14,5 @@ PS1="$" # Unset PROMPT_COMMAND, so that it can't change PS1 to something unexpected. unset PROMPT_COMMAND + +bind 'set enable-bracketed-paste off' -- cgit v1.2.1 From 000a8684bd109c3f9c9b5fabd95add4743b7f45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20E=C3=9Fer?= Date: Wed, 22 Sep 2021 17:48:07 +0200 Subject: Do not fail if no alias has been defined There is no output if no alias has been defined and the test fails in that case. Adding an alias definition makes the alias command return at least that just added entry. --- tests/test_replwrap.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_replwrap.py b/tests/test_replwrap.py index 5e50ea2..73c82e8 100644 --- a/tests/test_replwrap.py +++ b/tests/test_replwrap.py @@ -25,7 +25,7 @@ class REPLWrapTestCase(unittest.TestCase): def test_bash(self): bash = replwrap.bash() - res = bash.run_command("alias") + res = bash.run_command("alias xyzzy=true; alias") assert 'alias' in res, res try: @@ -92,7 +92,9 @@ class REPLWrapTestCase(unittest.TestCase): "PS1='{0}' PS2='{1}' " "PROMPT_COMMAND=''") + print(repl) res = repl.run_command("echo $HOME") + print(res) assert res.startswith('/'), res def test_python(self): -- cgit v1.2.1