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