xref: /aosp_15_r20/external/flatbuffers/tests/GoTest.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 Workergo_path=${test_dir}/go_gen
20*890232f2SAndroid Build Coastguard Workergo_src=${go_path}/src
21*890232f2SAndroid Build Coastguard Worker
22*890232f2SAndroid Build Coastguard Worker# Emit Go code for the example schemas in the test dir:
23*890232f2SAndroid Build Coastguard Worker../flatc -g --gen-object-api -I include_test monster_test.fbs optional_scalars.fbs
24*890232f2SAndroid Build Coastguard Worker
25*890232f2SAndroid Build Coastguard Worker# Go requires a particular layout of files in order to link multiple packages.
26*890232f2SAndroid Build Coastguard Worker# Copy flatbuffer Go files to their own package directories to compile the
27*890232f2SAndroid Build Coastguard Worker# test binary:
28*890232f2SAndroid Build Coastguard Workermkdir -p ${go_src}/MyGame/Example
29*890232f2SAndroid Build Coastguard Workermkdir -p ${go_src}/MyGame/Example2
30*890232f2SAndroid Build Coastguard Workermkdir -p ${go_src}/github.com/google/flatbuffers/go
31*890232f2SAndroid Build Coastguard Workermkdir -p ${go_src}/flatbuffers_test
32*890232f2SAndroid Build Coastguard Workermkdir -p ${go_src}/optional_scalars
33*890232f2SAndroid Build Coastguard Worker
34*890232f2SAndroid Build Coastguard Workercp -a MyGame/*.go ./go_gen/src/MyGame/
35*890232f2SAndroid Build Coastguard Workercp -a MyGame/Example/*.go ./go_gen/src/MyGame/Example/
36*890232f2SAndroid Build Coastguard Workercp -a MyGame/Example2/*.go ./go_gen/src/MyGame/Example2/
37*890232f2SAndroid Build Coastguard Worker# do not compile the gRPC generated files, which are not tested by go_test.go
38*890232f2SAndroid Build Coastguard Worker# below, but have their own test.
39*890232f2SAndroid Build Coastguard Workerrm ./go_gen/src/MyGame/Example/*_grpc.go
40*890232f2SAndroid Build Coastguard Workercp -a ../go/* ./go_gen/src/github.com/google/flatbuffers/go
41*890232f2SAndroid Build Coastguard Workercp -a ./go_test.go ./go_gen/src/flatbuffers_test/
42*890232f2SAndroid Build Coastguard Workercp -a optional_scalars/*.go ./go_gen/src/optional_scalars
43*890232f2SAndroid Build Coastguard Worker
44*890232f2SAndroid Build Coastguard Worker# https://stackoverflow.com/a/63545857/7024978
45*890232f2SAndroid Build Coastguard Worker# We need to turn off go modules for this script
46*890232f2SAndroid Build Coastguard Worker# to work.
47*890232f2SAndroid Build Coastguard Workergo env -w  GO111MODULE=off
48*890232f2SAndroid Build Coastguard Worker
49*890232f2SAndroid Build Coastguard Worker# Run tests with necessary flags.
50*890232f2SAndroid Build Coastguard Worker# Developers may wish to see more detail by appending the verbosity flag
51*890232f2SAndroid Build Coastguard Worker# -test.v to arguments for this command, as in:
52*890232f2SAndroid Build Coastguard Worker#   go -test -test.v ...
53*890232f2SAndroid Build Coastguard Worker# Developers may also wish to run benchmarks, which may be achieved with the
54*890232f2SAndroid Build Coastguard Worker# flag -test.bench and the wildcard regexp ".":
55*890232f2SAndroid Build Coastguard Worker#   go -test -test.bench=. ...
56*890232f2SAndroid Build Coastguard WorkerGOPATH=${go_path} go test flatbuffers_test \
57*890232f2SAndroid Build Coastguard Worker                     --coverpkg=github.com/google/flatbuffers/go \
58*890232f2SAndroid Build Coastguard Worker                     --cpp_data=${test_dir}/monsterdata_test.mon \
59*890232f2SAndroid Build Coastguard Worker                     --out_data=${test_dir}/monsterdata_go_wire.mon \
60*890232f2SAndroid Build Coastguard Worker                     --bench=. \
61*890232f2SAndroid Build Coastguard Worker                     --benchtime=3s \
62*890232f2SAndroid Build Coastguard Worker                     --fuzz=true \
63*890232f2SAndroid Build Coastguard Worker                     --fuzz_fields=4 \
64*890232f2SAndroid Build Coastguard Worker                     --fuzz_objects=10000
65*890232f2SAndroid Build Coastguard Worker
66*890232f2SAndroid Build Coastguard WorkerGO_TEST_RESULT=$?
67*890232f2SAndroid Build Coastguard Workerrm -rf ${go_path}/{pkg,src}
68*890232f2SAndroid Build Coastguard Workerif [[ $GO_TEST_RESULT  == 0 ]]; then
69*890232f2SAndroid Build Coastguard Worker    echo "OK: Go tests passed."
70*890232f2SAndroid Build Coastguard Workerelse
71*890232f2SAndroid Build Coastguard Worker    echo "KO: Go tests failed."
72*890232f2SAndroid Build Coastguard Worker    exit 1
73*890232f2SAndroid Build Coastguard Workerfi
74*890232f2SAndroid Build Coastguard Worker
75*890232f2SAndroid Build Coastguard WorkerNOT_FMT_FILES=$(gofmt -l MyGame)
76*890232f2SAndroid Build Coastguard Workerif [[ ${NOT_FMT_FILES} != "" ]]; then
77*890232f2SAndroid Build Coastguard Worker    echo "These files are not well gofmt'ed:"
78*890232f2SAndroid Build Coastguard Worker    echo
79*890232f2SAndroid Build Coastguard Worker    echo "${NOT_FMT_FILES}"
80*890232f2SAndroid Build Coastguard Worker    # enable this when enums are properly formated
81*890232f2SAndroid Build Coastguard Worker    # exit 1
82*890232f2SAndroid Build Coastguard Workerfi
83