Skip to content

Commit 1c85dce

Browse files
rossburtonrpurdie
authored andcommitted
oeqa/selftest: add test case for oeqa.utils.subprocesstweak
This class has a monkey-patched CalledProcessError instance that extends the __str__ method. Add a test case to ensure that it behaves as expected. Signed-off-by: Ross Burton <ross.burton@arm.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
1 parent 9067fe9 commit 1c85dce

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

meta/lib/oeqa/selftest/cases/liboe.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import oe.path
1010
import os
1111

12-
class LibOE(OESelftestTestCase):
12+
class CopyTreeTests(OESelftestTestCase):
1313

1414
@classmethod
1515
def setUpClass(cls):
@@ -102,3 +102,36 @@ def touchfile(tf):
102102
self.assertEqual(dstcnt, len(testfiles), "Number of files in dst (%s) differs from number of files in src(%s)." % (dstcnt, srccnt))
103103

104104
oe.path.remove(testloc)
105+
106+
class SubprocessTests(OESelftestTestCase):
107+
108+
def test_subprocess_tweak(self):
109+
"""
110+
Test that the string representation of
111+
oeqa.utils.subprocesstweak.OETestCalledProcessError includes stdout and
112+
stderr, as expected.
113+
"""
114+
script = """
115+
#! /bin/sh
116+
echo Ivn fgqbhg | tr '[a-zA-Z]' '[n-za-mN-ZA-M]'
117+
echo Ivn fgqree | tr '[a-zA-Z]' '[n-za-mN-ZA-M]' >&2
118+
exit 42
119+
"""
120+
121+
import subprocess
122+
import unittest.mock
123+
from oeqa.utils.subprocesstweak import OETestCalledProcessError
124+
125+
with self.assertRaises(OETestCalledProcessError) as cm:
126+
with unittest.mock.patch("subprocess.CalledProcessError", OETestCalledProcessError):
127+
subprocess.run(["bash", "-"], input=script, text=True, capture_output=True, check=True)
128+
129+
e = cm.exception
130+
self.assertEqual(e.returncode, 42)
131+
self.assertEqual("Via stdout\n", e.stdout)
132+
self.assertEqual("Via stderr\n", e.stderr)
133+
134+
string = str(e)
135+
self.assertIn("exit status 42", string)
136+
self.assertIn("Standard Output: Via stdout", string)
137+
self.assertIn("Standard Error: Via stderr", string)

0 commit comments

Comments
 (0)