1#!/bin/bash 2# 3# Copyright 2014 Google Inc. All rights reserved. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17set -eu 18 19pushd "$(dirname $0)" >/dev/null 20test_dir="$(pwd)" 21gen_code_path=${test_dir} 22runtime_library_dir=${test_dir}/../python 23 24# Emit Python code for the example schema in the test dir: 25${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_test.fbs --gen-object-api 26${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_test.fbs --gen-object-api --gen-onefile 27${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_extra.fbs --gen-object-api --python-typing --gen-compare 28${test_dir}/../flatc -p -o ${gen_code_path} -I include_test arrays_test.fbs --gen-object-api --python-typing 29${test_dir}/../flatc -p -o ${gen_code_path} -I include_test nested_union_test.fbs --gen-object-api --python-typing 30${test_dir}/../flatc -p -o ${gen_code_path} -I include_test service_test.fbs --grpc --grpc-python-typed-handlers --python-typing --no-python-gen-numpy --gen-onefile 31 32# Syntax: run_tests <interpreter> <benchmark vtable dedupes> 33# <benchmark read count> <benchmark build count> 34interpreters_tested=() 35function run_tests() { 36 if $(which ${1} >/dev/null); then 37 echo "Testing with interpreter: ${1}" 38 PYTHONDONTWRITEBYTECODE=1 \ 39 JYTHONDONTWRITEBYTECODE=1 \ 40 PYTHONPATH=${runtime_library_dir}:${gen_code_path} \ 41 JYTHONPATH=${runtime_library_dir}:${gen_code_path} \ 42 COMPARE_GENERATED_TO_GO=0 \ 43 COMPARE_GENERATED_TO_JAVA=0 \ 44 $1 py_test.py $2 $3 $4 $5 $6 45 if [ $1 = python3 ]; then 46 PYTHONDONTWRITEBYTECODE=1 \ 47 PYTHONPATH=${runtime_library_dir}:${gen_code_path} \ 48 $1 py_flexbuffers_test.py 49 fi 50 interpreters_tested+=(${1}) 51 echo 52 fi 53} 54 55# Run test suite with these interpreters. The arguments are benchmark counts. 56run_tests python2.6 100 100 100 100 false 57run_tests python2.7 100 100 100 100 false 58run_tests python2.7 100 100 100 100 true 59run_tests python3 100 100 100 100 false 60run_tests python3 100 100 100 100 true 61run_tests pypy 100 100 100 100 false 62 63# NOTE: We'd like to support python2.5 in the future. 64 65# NOTE: Jython 2.7.0 fails due to a bug in the stdlib `struct` library: 66# http://bugs.jython.org/issue2188 67 68if [ ${#interpreters_tested[@]} -eq 0 ]; then 69 echo "No Python interpeters found on this system, could not run tests." 70 exit 1 71fi 72 73# Run test suite with default python intereter. 74# (If the Python program `coverage` is available, it will be run, too. 75# Install `coverage` with `pip install coverage`.) 76if $(which coverage >/dev/null); then 77 echo 'Found coverage utility, running coverage with default Python:' 78 79 PYTHONDONTWRITEBYTECODE=1 \ 80 PYTHONPATH=${runtime_library_dir}:${gen_code_path} \ 81 coverage run --source=flatbuffers,MyGame py_test.py 0 0 0 0 false > /dev/null 82 83 echo 84 cov_result=`coverage report --omit="*flatbuffers/vendor*,*py_test*" \ 85 | tail -n 1 | awk ' { print $4 } '` 86 echo "Code coverage: ${cov_result}" 87else 88 echo -n "Did not find coverage utility for default Python, skipping. " 89 echo "Install with 'pip install coverage'." 90fi 91 92echo 93echo "OK: all tests passed for ${#interpreters_tested[@]} interpreters: ${interpreters_tested[@]}." 94