From ea787b6b55499e8f931201f8f49daaaeb2e4409f Mon Sep 17 00:00:00 2001 From: Yunhua Feng Date: Fri, 12 Oct 2018 09:19:03 +0800 Subject: [PATCH] BaseTools: update Test scripts support python3 update Test scripts support python3 Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yunhua Feng Reviewed-by: Liming Gao --- BaseTools/Tests/CheckUnicodeSourceFiles.py | 6 +++--- BaseTools/Tests/GNUmakefile | 2 +- BaseTools/Tests/TestTools.py | 14 ++++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/BaseTools/Tests/CheckUnicodeSourceFiles.py b/BaseTools/Tests/CheckUnicodeSourceFiles.py index 6ae62f180a..c76b2bc20e 100644 --- a/BaseTools/Tests/CheckUnicodeSourceFiles.py +++ b/BaseTools/Tests/CheckUnicodeSourceFiles.py @@ -110,7 +110,7 @@ class Tests(TestTools.BaseToolsTest): # This test makes sure that BaseTools rejects these characters # if seen in a .uni file. # - data = codecs.BOM_UTF16_LE + '//\x01\xd8 ' + data = codecs.BOM_UTF16_LE + b'//\x01\xd8 ' self.CheckFile(encoding=None, shouldPass=False, string=data) @@ -161,7 +161,7 @@ class Tests(TestTools.BaseToolsTest): # This test makes sure that BaseTools rejects these characters # if seen in a .uni file. # - data = '\xed\xa0\x81' + data = b'\xed\xa0\x81' self.CheckFile(encoding=None, shouldPass=False, string=data) @@ -170,7 +170,7 @@ class Tests(TestTools.BaseToolsTest): # Same test as testSurrogatePairUnicodeCharInUtf8File, but add # the UTF-8 BOM # - data = codecs.BOM_UTF8 + '\xed\xa0\x81' + data = codecs.BOM_UTF8 + b'\xed\xa0\x81' self.CheckFile(encoding=None, shouldPass=False, string=data) diff --git a/BaseTools/Tests/GNUmakefile b/BaseTools/Tests/GNUmakefile index 0c11f6aae9..af334a8ac2 100644 --- a/BaseTools/Tests/GNUmakefile +++ b/BaseTools/Tests/GNUmakefile @@ -14,7 +14,7 @@ all: test test: - @if command -v python2 >/dev/null 2>&1; then python2 RunTests.py; else python RunTests.py; fi + @if command -v $(PYTHON3) >/dev/null 2>&1; then $(PYTHON3) RunTests.py; else python RunTests.py; fi clean: find . -name '*.pyc' -exec rm '{}' ';' diff --git a/BaseTools/Tests/TestTools.py b/BaseTools/Tests/TestTools.py index e16e993048..65948a7ae2 100644 --- a/BaseTools/Tests/TestTools.py +++ b/BaseTools/Tests/TestTools.py @@ -40,7 +40,7 @@ if PythonSourceDir not in sys.path: def MakeTheTestSuite(localItems): tests = [] - for name, item in localItems.iteritems(): + for name, item in localItems.items(): if isinstance(item, type): if issubclass(item, unittest.TestCase): tests.append(unittest.TestLoader().loadTestsFromTestCase(item)) @@ -146,9 +146,12 @@ class BaseToolsTest(unittest.TestCase): return data def WriteTmpFile(self, fileName, data): - f = open(self.GetTmpFilePath(fileName), 'w') - f.write(data) - f.close() + if isinstance(data, bytes): + with open(self.GetTmpFilePath(fileName), 'wb') as f: + f.write(data) + else: + with open(self.GetTmpFilePath(fileName), 'w') as f: + f.write(data) def GenRandomFileData(self, fileName, minlen = None, maxlen = None): if maxlen is None: maxlen = minlen @@ -161,7 +164,7 @@ class BaseToolsTest(unittest.TestCase): if maxlen is None: maxlen = minlen return ''.join( [chr(random.randint(0, 255)) - for x in xrange(random.randint(minlen, maxlen)) + for x in range(random.randint(minlen, maxlen)) ]) def setUp(self): @@ -183,4 +186,3 @@ class BaseToolsTest(unittest.TestCase): os.environ['PATH'] = self.savedEnvPath sys.path = self.savedSysPath -