From d15065cdcfccf8943fa3ede7b225b698f3aec707 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Wed, 25 Jul 2018 16:46:25 -0600 Subject: Testing: python 3 fix for must_contain TestCommon defines a method must_contain which checks for a file including a given string. With Python 3, the test runs into some typing problems. This could be fixed either by changing all the tests which call the routine either omitting the mode argument (which then defaults to 'rb'), or specifying a mode which includes 'b'; or by modifying must_contain to align the types of the file data and the data to check for. This patch uses the latter approach. This is a test-only change, no run-time scons code is modified. Signed-off-by: Mats Wichmann --- src/CHANGES.txt | 5 +++-- testing/framework/TestCommon.py | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/CHANGES.txt b/src/CHANGES.txt index c875f1f..cc8e5c6 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -103,8 +103,9 @@ RELEASE 3.1.0.alpha.yyyymmdd - NEW DATE WILL BE INSERTED HERE - update wiki links to new github location - update bug links to new github location - convert TestCmd.read to use with statement on open (quiets 17 py3 warnings) - - quiet warning in UtilTests.py - - fix tests specifying octal constants for Py3 + - quiet py3 warning in UtilTests.py + - fix tests specifying octal constants for py3 + - fix must_contain tests for py3 From Hao Wu - typo in customized decider example in user guide diff --git a/testing/framework/TestCommon.py b/testing/framework/TestCommon.py index 47a149b..e551cce 100644 --- a/testing/framework/TestCommon.py +++ b/testing/framework/TestCommon.py @@ -268,6 +268,15 @@ class TestCommon(TestCmd): def must_contain(self, file, required, mode = 'rb', find = None): """Ensures that the specified file contains the required text. """ + 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 file_contents = self.read(file, mode) if find is None: def find(o, l): -- cgit v0.12 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