From 0cf044543fb089bcd997b3ad2d877b71026ce5bf Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Tue, 31 Jul 2018 16:57:26 -0600 Subject: Try a more scons-y file conversion for Py3 file reads Instead of custom conversion as in the previous iteration, use the to_bytes function. The two known tests which incorrectly let the text-mode xml file be opened in binary mode are adjusted to supply mode='r' Signed-off-by: Mats Wichmann --- test/Docbook/basic/xinclude/xinclude.py | 2 +- test/Docbook/dependencies/xinclude/xinclude.py | 2 +- testing/framework/TestCommon.py | 24 ++++++++++++++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/test/Docbook/basic/xinclude/xinclude.py b/test/Docbook/basic/xinclude/xinclude.py index 302c777..9b22c13 100644 --- a/test/Docbook/basic/xinclude/xinclude.py +++ b/test/Docbook/basic/xinclude/xinclude.py @@ -44,7 +44,7 @@ test.dir_fixture('image') # Normal invocation test.run() test.must_exist(test.workpath('manual_xi.xml')) -test.must_contain(test.workpath('manual_xi.xml'),'This is an included text.') +test.must_contain(test.workpath('manual_xi.xml'),'This is an included text.', mode='r') # Cleanup diff --git a/test/Docbook/dependencies/xinclude/xinclude.py b/test/Docbook/dependencies/xinclude/xinclude.py index 115163c..c3d9e25 100644 --- a/test/Docbook/dependencies/xinclude/xinclude.py +++ b/test/Docbook/dependencies/xinclude/xinclude.py @@ -44,7 +44,7 @@ test.dir_fixture('image') # Normal invocation test.run() test.must_exist(test.workpath('manual_xi.xml')) -test.must_contain(test.workpath('manual_xi.xml'),'This is an included text.') +test.must_contain(test.workpath('manual_xi.xml'),'This is an included text.', mode='r') # Change included file test.write('include.txt', 'This is another text.') diff --git a/testing/framework/TestCommon.py b/testing/framework/TestCommon.py index e551cce..e55b491 100644 --- a/testing/framework/TestCommon.py +++ b/testing/framework/TestCommon.py @@ -265,18 +265,26 @@ class TestCommon(TestCmd): print("Unwritable files: `%s'" % "', `".join(unwritable)) self.fail_test(missing + unwritable) - def must_contain(self, file, required, mode = 'rb', find = None): - """Ensures that the specified file contains the required text. + def must_contain(self, file, required, mode='rb', find=None): + """Ensures specified file contains the required text. + + Args: + file (string): name of file to search in. + required (string): text to search for. For the default + find function, type must match the return type from + reading the file; current implementation will convert. + mode (string): file open mode. + find (func): optional custom search routine. Must take the + form "find(output, line)" returning non-zero on success + and None on failure. + + Calling test exits FAILED if search result is false """ if 'b' in mode: # Python 3: reading a file in binary mode returns a # bytes object. We cannot find the index of a different - # (str) type in that, so encode "required". For Py2 - # it is all just strings, so it still works. - try: - required = required.encode() - except AttributeError: - pass # in case it's encoded already + # (str) type in that, so convert. + required = to_bytes(required) file_contents = self.read(file, mode) if find is None: def find(o, l): -- cgit v0.12