blob: 925bd95ebbee24e33aea918c2e2f27493028a402 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001#!/bin/bash -eu
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
17pushd "$(dirname $0)" >/dev/null
18test_dir="$(pwd)"
19gen_code_path=${test_dir}
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080020runtime_library_dir=${test_dir}/../../python
Austin Schuhe89fa2d2019-08-14 20:24:23 -070021
22# Emit Python code for the example schema in the test dir:
Austin Schuh272c6132020-11-14 16:37:52 -080023${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_test.fbs --gen-object-api
James Kuszmaul8e62b022022-03-22 09:33:25 -070024${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_test.fbs --gen-object-api --gen-onefile
25${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_extra.fbs --gen-object-api
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080026${test_dir}/../flatc -p -o ${gen_code_path} -I include_test arrays_test.fbs --gen-object-api
27${test_dir}/../flatc -p -o ${gen_code_path} -I include_test nested_union_test.fbs --gen-object-api
Austin Schuhe89fa2d2019-08-14 20:24:23 -070028
29# Syntax: run_tests <interpreter> <benchmark vtable dedupes>
30# <benchmark read count> <benchmark build count>
31interpreters_tested=()
32function run_tests() {
33 if $(which ${1} >/dev/null); then
34 echo "Testing with interpreter: ${1}"
35 PYTHONDONTWRITEBYTECODE=1 \
36 JYTHONDONTWRITEBYTECODE=1 \
37 PYTHONPATH=${runtime_library_dir}:${gen_code_path} \
38 JYTHONPATH=${runtime_library_dir}:${gen_code_path} \
39 COMPARE_GENERATED_TO_GO=0 \
40 COMPARE_GENERATED_TO_JAVA=0 \
James Kuszmaul8e62b022022-03-22 09:33:25 -070041 $1 py_test.py $2 $3 $4 $5
Austin Schuh272c6132020-11-14 16:37:52 -080042 if [ $1 = python3 ]; then
43 PYTHONDONTWRITEBYTECODE=1 \
44 PYTHONPATH=${runtime_library_dir}:${gen_code_path} \
45 $1 py_flexbuffers_test.py
46 fi
Austin Schuhe89fa2d2019-08-14 20:24:23 -070047 interpreters_tested+=(${1})
48 echo
49 fi
50}
51
52# Run test suite with these interpreters. The arguments are benchmark counts.
James Kuszmaul8e62b022022-03-22 09:33:25 -070053run_tests python2.6 100 100 100 false
54run_tests python2.7 100 100 100 false
55run_tests python2.7 100 100 100 true
56run_tests python3 100 100 100 false
57run_tests python3 100 100 100 true
58run_tests pypy 100 100 100 false
Austin Schuhe89fa2d2019-08-14 20:24:23 -070059
60# NOTE: We'd like to support python2.5 in the future.
61
62# NOTE: Jython 2.7.0 fails due to a bug in the stdlib `struct` library:
63# http://bugs.jython.org/issue2188
64
65if [ ${#interpreters_tested[@]} -eq 0 ]; then
66 echo "No Python interpeters found on this system, could not run tests."
67 exit 1
68fi
69
70# Run test suite with default python intereter.
71# (If the Python program `coverage` is available, it will be run, too.
72# Install `coverage` with `pip install coverage`.)
73if $(which coverage >/dev/null); then
74 echo 'Found coverage utility, running coverage with default Python:'
75
76 PYTHONDONTWRITEBYTECODE=1 \
77 PYTHONPATH=${runtime_library_dir}:${gen_code_path} \
James Kuszmaul3b15b0c2022-11-08 14:03:16 -080078 coverage run --source=flatbuffers,MyGame py_test.py 0 0 0 false > /dev/null
Austin Schuhe89fa2d2019-08-14 20:24:23 -070079
80 echo
81 cov_result=`coverage report --omit="*flatbuffers/vendor*,*py_test*" \
82 | tail -n 1 | awk ' { print $4 } '`
83 echo "Code coverage: ${cov_result}"
84else
85 echo -n "Did not find coverage utility for default Python, skipping. "
86 echo "Install with 'pip install coverage'."
87fi
88
89echo
90echo "OK: all tests passed for ${#interpreters_tested[@]} interpreters: ${interpreters_tested[@]}."