diff --git a/scripts/test.sh b/scripts/test.sh index c5526986d..2b3775634 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -153,14 +153,28 @@ test_python() local MESSAGE="Zserio Python tests" echo "STARTING - ${MESSAGE}" - activate_python_virtualenv "${ZSERIO_PROJECT_ROOT}" "${ZSERIO_BUILD_DIR}" - if [ $? -ne 0 ] ; then - return 1 - fi - if [[ ${SWITCH_CLEAN} == 1 ]] ; then rm -rf "${TEST_OUT_DIR}/python" else + activate_python_virtualenv "${ZSERIO_PROJECT_ROOT}" "${ZSERIO_BUILD_DIR}" + if [ $? -ne 0 ] ; then + return 1 + fi + + python "${UNPACKED_ZSERIO_RELEASE_DIR}"/runtime_libs/python/zserio_cpp/setup.py build \ + --build-base="${TEST_OUT_DIR}/python/zserio_cpp" \ + --cpp-runtime-dir="${UNPACKED_ZSERIO_RELEASE_DIR}/runtime_libs/cpp/" + if [ $? -ne 0 ] ; then + stderr_echo "Failed to build C++ runtime binding to Python!" + return 1 + fi + local ZSERIO_CPP_DIR + ZSERIO_CPP_DIR=$(ls -d1 "${TEST_OUT_DIR}/python/zserio_cpp/lib"*) + if [ $? -ne 0 ] ; then + stderr_echo "Failed to locate C++ runtime binding to Python!" + return 1 + fi + local TEST_FILTER="" for i in ${!TEST_SUITES[@]} ; do if [ $i -gt 0 ] ; then @@ -183,7 +197,8 @@ test_python() echo python "${TEST_FILE}" "${TEST_ARGS[@]}" --pylint_rcfile="${PYLINT_RCFILE}" \ - --pylint_rcfile_test="${PYLINT_RCFILE_FOR_TESTS}" --mypy_config_file="${MYPY_CONFIG_FILE}" + --pylint_rcfile_test="${PYLINT_RCFILE_FOR_TESTS}" --mypy_config_file="${MYPY_CONFIG_FILE}" \ + --zserio_cpp_dir="${ZSERIO_CPP_DIR}" local PYTHON_RESULT=$? if [ ${PYTHON_RESULT} -ne 0 ] ; then stderr_echo "Running python failed with return code ${PYTHON_RESULT}!" diff --git a/test/language/choice_types/python/EmptyChoiceTest.py b/test/language/choice_types/python/EmptyChoiceTest.py index f6d52c91f..fd1aa7c2f 100644 --- a/test/language/choice_types/python/EmptyChoiceTest.py +++ b/test/language/choice_types/python/EmptyChoiceTest.py @@ -15,7 +15,7 @@ def testSelectorConstructor(self): def testFromReader(self): selector = 1 - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyChoice = self.api.EmptyChoice.from_reader(reader, selector) self.assertEqual(selector, emptyChoice.selector) self.assertEqual(0, emptyChoice.bitsizeof()) @@ -59,7 +59,7 @@ def testInitializeOffsets(self): def testRead(self): selector = 1 - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyChoice = self.api.EmptyChoice(selector) emptyChoice.read(reader) self.assertEqual(selector, emptyChoice.selector) diff --git a/test/language/choice_types/python/EmptyChoiceWithCaseTest.py b/test/language/choice_types/python/EmptyChoiceWithCaseTest.py index 6a6f5a8b0..1f8673ff3 100644 --- a/test/language/choice_types/python/EmptyChoiceWithCaseTest.py +++ b/test/language/choice_types/python/EmptyChoiceWithCaseTest.py @@ -14,7 +14,7 @@ def testSelectorConstructor(self): def testFromReader(self): selector = 1 - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyChoiceWithCase = self.api.EmptyChoiceWithCase.from_reader(reader, selector) self.assertEqual(selector, emptyChoiceWithCase.selector) self.assertEqual(0, emptyChoiceWithCase.bitsizeof()) @@ -61,7 +61,7 @@ def testInitializeOffsets(self): def testRead(self): selector = 1 - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyChoiceWithCase = self.api.EmptyChoiceWithCase(selector) emptyChoiceWithCase.read(reader) self.assertEqual(selector, emptyChoiceWithCase.selector) diff --git a/test/language/choice_types/python/EmptyChoiceWithDefaultTest.py b/test/language/choice_types/python/EmptyChoiceWithDefaultTest.py index 0c699ce97..a779c1812 100644 --- a/test/language/choice_types/python/EmptyChoiceWithDefaultTest.py +++ b/test/language/choice_types/python/EmptyChoiceWithDefaultTest.py @@ -14,7 +14,7 @@ def testSelectorConstructor(self): def testFromReader(self): selector = 1 - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyChoiceWithDefault = self.api.EmptyChoiceWithDefault.from_reader(reader, selector) self.assertEqual(selector, emptyChoiceWithDefault.selector) self.assertEqual(0, emptyChoiceWithDefault.bitsizeof()) @@ -61,7 +61,7 @@ def testInitializeOffsets(self): def testRead(self): selector = 1 - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyChoiceWithDefault = self.api.EmptyChoiceWithDefault(selector) emptyChoiceWithDefault.read(reader) self.assertEqual(selector, emptyChoiceWithDefault.selector) diff --git a/test/language/structure_types/python/EmptyStructureTest.py b/test/language/structure_types/python/EmptyStructureTest.py index ba5930a5a..e64b9ef0d 100644 --- a/test/language/structure_types/python/EmptyStructureTest.py +++ b/test/language/structure_types/python/EmptyStructureTest.py @@ -9,7 +9,7 @@ def setUpClass(cls): cls.api = getZserioApi(__file__, "structure_types.zs").empty_structure def testFromReader(self): - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyStructure = self.api.EmptyStructure.from_reader(reader) self.assertEqual(0, emptyStructure.bitsizeof()) @@ -38,7 +38,7 @@ def testInitializeOffsets(self): self.assertEqual(bitPosition, emptyStructure.initialize_offsets(bitPosition)) def testRead(self): - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyStructure = self.api.EmptyStructure() emptyStructure.read(reader) self.assertEqual(0, emptyStructure.bitsizeof()) diff --git a/test/language/structure_types/python/EmptyStructureWithParameterTest.py b/test/language/structure_types/python/EmptyStructureWithParameterTest.py index bcb0942e8..78db4fbb0 100644 --- a/test/language/structure_types/python/EmptyStructureWithParameterTest.py +++ b/test/language/structure_types/python/EmptyStructureWithParameterTest.py @@ -14,7 +14,7 @@ def testParamConstructor(self): def testFromReader(self): param = 1 - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyStructureWithParameter = self.api.EmptyStructureWithParameter.from_reader(reader, param) self.assertEqual(param, emptyStructureWithParameter.param) self.assertEqual(0, emptyStructureWithParameter.bitsizeof()) @@ -54,7 +54,7 @@ def testInitializeOffsets(self): def testRead(self): param = 1 - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyStructureWithParameter = self.api.EmptyStructureWithParameter(param) emptyStructureWithParameter.read(reader) self.assertEqual(param, emptyStructureWithParameter.param) diff --git a/test/language/union_types/python/EmptyUnionTest.py b/test/language/union_types/python/EmptyUnionTest.py index 712c4cd9c..acc341a20 100644 --- a/test/language/union_types/python/EmptyUnionTest.py +++ b/test/language/union_types/python/EmptyUnionTest.py @@ -13,7 +13,7 @@ def testEmptyConstructor(self): self.assertEqual(0, emptyUnion.bitsizeof()) def testFromReader(self): - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyUnion = self.api.EmptyUnion.from_reader(reader) self.assertEqual(0, emptyUnion.bitsizeof()) @@ -47,7 +47,7 @@ def testInitializeOffsets(self): def testRead(self): emptyUnion = self.api.EmptyUnion() - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyUnion.read(reader) self.assertEqual(0, emptyUnion.bitsizeof()) diff --git a/test/language/union_types/python/EmptyUnionWithParameterTest.py b/test/language/union_types/python/EmptyUnionWithParameterTest.py index 2ff3e9881..2e2256c65 100644 --- a/test/language/union_types/python/EmptyUnionWithParameterTest.py +++ b/test/language/union_types/python/EmptyUnionWithParameterTest.py @@ -13,7 +13,7 @@ def testParamConstructor(self): self.assertEqual(self.PARAM_VALUE1, emptyUnionWithParameter.param) def testFromReader(self): - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyUnionWithParameter = self.api.EmptyUnionWithParameter.from_reader(reader, self.PARAM_VALUE1) self.assertEqual(self.PARAM_VALUE1, emptyUnionWithParameter.param) self.assertEqual(0, emptyUnionWithParameter.bitsizeof()) @@ -53,7 +53,7 @@ def testInitializeOffsets(self): def testRead(self): emptyUnionWithParameter = self.api.EmptyUnionWithParameter(self.PARAM_VALUE1) - reader = zserio.BitStreamReader([]) + reader = zserio.BitStreamReader(bytes()) emptyUnionWithParameter.read(reader) self.assertEqual(self.PARAM_VALUE1, emptyUnionWithParameter.param) self.assertEqual(0, emptyUnionWithParameter.bitsizeof()) diff --git a/test/tests.py b/test/tests.py index 0f7e6e25a..eafa95395 100644 --- a/test/tests.py +++ b/test/tests.py @@ -9,6 +9,7 @@ import argparse import glob import pylint.lint +from multiprocessing import Process def main(): testRoot = os.path.dirname(os.path.realpath(__file__)) @@ -26,6 +27,7 @@ def main(): argParser.add_argument("--pylint_rcfile") argParser.add_argument("--pylint_rcfile_test") argParser.add_argument("--mypy_config_file") + argParser.add_argument("--zserio_cpp_dir") argParser.set_defaults(filter="**", verbosity=2) args = argParser.parse_args() if args.build_dir: @@ -38,8 +40,7 @@ def main(): # path to zserio runtime release runtimePath = os.path.join(TEST_ARGS["release_dir"], "runtime_libs", "python") sys.path.append(runtimePath) - - sysPathBeforeTests = list(sys.path) + sys.path.append(args.zserio_cpp_dir) # detect test directories testPattern = "*Test.py" @@ -60,20 +61,17 @@ def main(): # sort test dirs testDirs = sorted(testDirs) - # load tests - loader = unittest.TestLoader() - testSuite = unittest.TestSuite() - for testDir in testDirs: - loadedTests = loader.discover(testDir, pattern=testPattern, top_level_dir=testDir) - testSuite.addTest(loadedTests) - - runner = unittest.TextTestRunner(verbosity=args.verbosity) - testResult = runner.run(testSuite) - if not testResult.wasSuccessful(): + # run tests with pure python runtime + print("\nRunning python language tests with pure python runtime.") + os.environ["ZSERIO_PYTHON_IMPLEMENTATION"] = "python" + if not _runTests(args, testDirs, testPattern) : return 1 - # restore sys.path to get rid of what test runner recently added - sys.path = sysPathBeforeTests + # run tests with python runtime and optimized zserio_cpp + print("\nRunning python language tests with C++ optimized runtime.") + os.environ["ZSERIO_PYTHON_IMPLEMENTATION"] = "cpp" + if not _runTests(args, testDirs, testPattern): + return 1 # run pylint pylintResult = _runPylintOnAllSources(args, testDirs) @@ -82,6 +80,24 @@ def main(): return _runMypyOnAllSources(args, testDirs, runtimePath, testutilsPath) +def _runTests(args, testDirs, testPattern): + p = Process(target=_runTestsProcess, args=(args, testDirs, testPattern)) + p.start() + p.join() + return p.exitcode == 0 + +def _runTestsProcess(args, testDirs, testPattern): + loader = unittest.TestLoader() + testSuite = unittest.TestSuite() + for testDir in testDirs: + loadedTests = loader.discover(testDir, pattern=testPattern, top_level_dir=testDir) + testSuite.addTest(loadedTests) + + runner = unittest.TextTestRunner(verbosity=args.verbosity) + testResult = runner.run(testSuite) + + sys.exit(0 if testResult.wasSuccessful() else 1) + def _runPylintOnAllSources(args, testDirs): print("\nRunning pylint on python tests")