Fix test by prevent bash from sourcing profile
authorJeff Quast <contact@jeffquast.com>
Sat, 22 Nov 2014 08:13:01 +0000 (00:13 -0800)
committerJeff Quast <contact@jeffquast.com>
Sat, 22 Nov 2014 08:13:01 +0000 (00:13 -0800)
Instead, create a temporary file for use with
--rcfile argument with only the contents
``PS1='GO: '``, ensuring that the tests that
expect some form of '[$#]' succeeds even where
a user's default PS1 prompt does not contain it.

tests/test_run.py

index 077f8573ab9d6259531e9dcb8993eb09cc4c90d7..8b12f4add05a15be6df36a2f5f2f498097255f8e 100755 (executable)
@@ -22,7 +22,9 @@ PEXPECT LICENSE
 import pexpect
 import unittest
 import subprocess
+import tempfile
 import sys
+import os
 from . import PexpectTestCase
 
 # TODO Many of these test cases blindly assume that sequential
@@ -44,6 +46,14 @@ class RunFuncTestCase(PexpectTestCase.PexpectTestCase):
     empty = b''
     prep_subprocess_out = staticmethod(lambda x: x)
 
+    def setUp(self):
+        fd, self.rcfile = tempfile.mkstemp()
+        os.write(fd, 'PS1=GO: \n')
+        os.close(fd)
+
+    def tearDown(self):
+        os.unlink(self.rcfile)
+
     def test_run_exit (self):
         (data, exitstatus) = self.runfunc('python exit1.py', withexitstatus=1)
         assert exitstatus == 1, "Exit status of 'python exit1.py' should be 1."
@@ -65,11 +75,20 @@ class RunFuncTestCase(PexpectTestCase.PexpectTestCase):
         assert exitstatus != 0
 
     def test_run_tuple_list (self):
-        events = [('abc\r\n.*[$#]','echo "def"\n'),
-                  ('def\r\n.*[$#]','exit\n'),
-                  ('[$#] ','echo "abc"\n')]
-        (data, exitstatus) = pexpect.run('bash', withexitstatus=1,
-                                          events=events, timeout=10)
+        events = [
+            # second match on 'abc', echo 'def'
+            ('abc\r\n.*$', 'echo "def"\n'),
+            # final match on 'def': exit
+            ('def\r\n.*$', 'exit\n'),
+            # first match on 'GO:' prompt, echo 'abc'
+            ('GO:', 'echo "abc"\n')
+        ]
+
+        (data, exitstatus) = pexpect.run(
+            'bash --rcfile {0}'.format(self.rcfile),
+            withexitstatus=True,
+            events=events,
+            timeout=10)
         assert exitstatus == 0
 
 class RunUnicodeFuncTestCase(RunFuncTestCase):