Skip to content

Commit

Permalink
Ensure test_run.py works when system-wide bashrc produces output
Browse files Browse the repository at this point in the history
The --rcfile option to bash does not prevent the system-wide
/etc/bash.bashrc script from running (contrary to the Debian man-page).
This causes test_run.py to fail when the system-wide script produces
output (as is the case in an Ubuntu image where the regular user belongs
to the "sudo" group and the system-wide script produces an informational
message about sudo usage).

Rather than rely on --rcfile, this patch changes the test to use --norc
(which does suppress the execution of the system-wide bashrc), and
changes PS1 via the inherited environment instead (this also slightly
simplifies the fixture setup too as tempfile is no longer required).
  • Loading branch information
waveform80 committed Aug 23, 2022
1 parent acb017a commit b14f156
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import pexpect
import unittest
import subprocess
import tempfile
import sys
import os
from . import PexpectTestCase
Expand Down Expand Up @@ -59,15 +58,10 @@ class RunFuncTestCase(PexpectTestCase.PexpectTestCase):
prep_subprocess_out = staticmethod(lambda x: x)

def setUp(self):
fd, self.rcfile = tempfile.mkstemp()
os.write(fd, b'PS1=GO: \n')
os.close(fd)
self.runenv = os.environ.copy()
self.runenv['PS1'] = 'GO:'
super(RunFuncTestCase, self).setUp()

def tearDown(self):
os.unlink(self.rcfile)
super(RunFuncTestCase, self).tearDown()

def test_run_exit(self):
(data, exitstatus) = self.runfunc(sys.executable + ' exit1.py', withexitstatus=1)
assert exitstatus == 1, "Exit status of 'python exit1.py' should be 1."
Expand Down Expand Up @@ -106,9 +100,10 @@ def test_run_event_as_string(self):
]

(data, exitstatus) = pexpect.run(
'bash --rcfile {0}'.format(self.rcfile),
'bash --norc',
withexitstatus=True,
events=events,
env=self.runenv,
timeout=10)
assert exitstatus == 0

Expand All @@ -118,9 +113,10 @@ def test_run_event_as_function(self):
]

(data, exitstatus) = pexpect.run(
'bash --rcfile {0}'.format(self.rcfile),
'bash --norc',
withexitstatus=True,
events=events,
env=self.runenv,
timeout=10)
assert exitstatus == 0

Expand All @@ -130,18 +126,20 @@ def test_run_event_as_method(self):
]

(data, exitstatus) = pexpect.run(
'bash --rcfile {0}'.format(self.rcfile),
'bash --norc',
withexitstatus=True,
events=events,
env=self.runenv,
timeout=10)
assert exitstatus == 0

def test_run_event_typeerror(self):
events = [('GO:', -1)]
with self.assertRaises(TypeError):
pexpect.run('bash --rcfile {0}'.format(self.rcfile),
pexpect.run('bash --norc',
withexitstatus=True,
events=events,
env=self.runenv,
timeout=10)

def _method_events_callback(self, values):
Expand Down

0 comments on commit b14f156

Please sign in to comment.