diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b0f66d41c..9a9918b18 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -315,11 +315,14 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - name: flatc + # FIXME: make test script not rely on flatc + run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j - name: compile run: npm run compile - name: test working-directory: tests - run: sh TypeScriptTest.sh + run: python3 TypeScriptTest.py build-dart: name: Build Dart diff --git a/docs/source/TypeScriptUsage.md b/docs/source/TypeScriptUsage.md index a1acaeb8a..0dd1da2eb 100644 --- a/docs/source/TypeScriptUsage.md +++ b/docs/source/TypeScriptUsage.md @@ -21,8 +21,8 @@ https://www.npmjs.com/package/flatbuffers. ## Testing the FlatBuffers TypeScript library -To run the tests, use the [TypeScriptTest.sh](https://github.com/google/ -flatbuffers/blob/master/tests/TypeScriptTest.sh) shell script. +To run the tests, use the [TypeScriptTest.py](https://github.com/google/ +flatbuffers/blob/master/tests/TypeScriptTest.py) Python3 script. *Note: The TypeScript test file requires [Node.js](https://nodejs.org/en/).* diff --git a/package.json b/package.json index 41bc51b10..4f43139d9 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "test": "tests" }, "scripts": { - "test": "npm run compile && cd tests && ./TypeScriptTest.sh", + "test": "npm run compile && cd tests && python3 ./TypeScriptTest.py", "compile": "tsc && tsc -p tsconfig.mjs.json", "prepublishOnly": "npm install --only=dev && npm run compile" }, diff --git a/tests/TestAll.sh b/tests/TestAll.sh index 06548a213..3a3fcd7bf 100755 --- a/tests/TestAll.sh +++ b/tests/TestAll.sh @@ -16,7 +16,7 @@ sh PythonTest.sh echo "************************ TypeScript:" -sh TypeScriptTest.sh +python3 TypeScriptTest.py echo "************************ C++:" diff --git a/tests/TypeScriptTest.bat b/tests/TypeScriptTest.bat deleted file mode 100755 index daa545106..000000000 --- a/tests/TypeScriptTest.bat +++ /dev/null @@ -1,9 +0,0 @@ -npm install -../flatc.exe --ts --gen-name-strings --gen-mutable --gen-object-api -I include_test monster_test.fbs -../flatc.exe --gen-object-api -b -I include_test monster_test.fbs unicode_test.json -../flatc.exe --ts --gen-name-strings --gen-mutable --gen-object-api -o union_vector union_vector/union_vector.fbs -../flatc.exe --ts --gen-name-strings optional_scalars.fbs -tsc -node -r esm JavaScriptTest -node -r esm JavaScriptUnionVectorTest -node -r esm JavaScriptFlexBuffersTest diff --git a/tests/TypeScriptTest.py b/tests/TypeScriptTest.py new file mode 100644 index 000000000..a3b1e43e1 --- /dev/null +++ b/tests/TypeScriptTest.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 +# +# Copyright 2022 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import filecmp +import glob +import platform +import shutil +import subprocess +from pathlib import Path + +# Get the path where this script is located so we can invoke the script from +# any directory and have the paths work correctly. +tests_path = Path(__file__).parent.resolve() + +# Get the root path as an absolute path, so all derived paths are absolute. +root_path = tests_path.parent.absolute() + +# Get the location of the flatc executable +flatc_exe = Path("flatc" if not platform.system() == "Windows" else "flatc.exe") + +# Find and assert flatc compiler is present. +if root_path in flatc_exe.parents: + flatc_exe = flatc_exe.relative_to(root_path) +flatc_path = Path(root_path, flatc_exe) +assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path) + +# Execute the flatc compiler with the specified parameters +def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path): + cmd = [str(flatc_path)] + options + if prefix: + cmd += ["-o"] + [prefix] + if include: + cmd += ["-I"] + [include] + cmd += [schema] if isinstance(schema, str) else schema + if data: + cmd += [data] if isinstance(data, str) else data + result = subprocess.run(cmd, cwd=str(cwd), check=True) + + +print("Removing node_modules/ directory...") +shutil.rmtree(Path(tests_path, "node_modules"), ignore_errors=True) + +assert subprocess.run(["npm", "install", "--silent"], cwd=str(tests_path), shell=True) + +print("Invoking flatc...") +flatc( + options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"], + schema="monster_test.fbs", + include="include_test", +) + +flatc( + options=["--gen-object-api", "-b"], + schema="monster_test.fbs", + include="include_test", + data="unicode_test.json", +) + +flatc( + options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"], + schema="union_vector/union_vector.fbs", + prefix="union_vector", +) + +flatc( + options=["--ts", "--gen-name-strings"], + schema="optional_scalars.fbs", +) + +flatc( + options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"], + schema=[ + "typescript_keywords.fbs", + "test_dir/typescript_include.fbs", + "test_dir/typescript_transitive_include.fbs", + "../reflection/reflection.fbs", + ], + include="../", +) + +flatc( + options=[ + "--ts", + "--gen-name-strings", + "--gen-mutable", + "--gen-object-api", + "--ts-flat-files", + ], + schema=[ + "typescript_keywords.fbs", + "test_dir/typescript_include.fbs", + "test_dir/typescript_transitive_include.fbs", + "../reflection/reflection.fbs", + ], + include="../", +) + +print("Running TypeScript Compiler...") +assert subprocess.run(["tsc"], cwd=str(tests_path), shell=True) + +NODE_CMD = ["node", "-r", "esm"] + +print("Running TypeScript Tests...") +assert subprocess.run(NODE_CMD + ["JavaScriptTest"], cwd=str(tests_path), shell=True) +assert subprocess.run( + NODE_CMD + ["JavaScriptUnionVectorTest"], cwd=str(tests_path), shell=True +) +assert subprocess.run( + NODE_CMD + ["JavaScriptFlexBuffersTest"], cwd=str(tests_path), shell=True +) diff --git a/tests/TypeScriptTest.sh b/tests/TypeScriptTest.sh deleted file mode 100755 index fd7b82d50..000000000 --- a/tests/TypeScriptTest.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh -# -# Copyright 2016 Google Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -e - -# clean node_modules to make sure we depend on latest local flatbuffers at ../ -rm -rf node_modules -npm install - -if [ -x ../flatc ]; then - ../flatc --ts --gen-name-strings --gen-mutable --gen-object-api -I include_test monster_test.fbs - ../flatc --gen-object-api -b -I include_test monster_test.fbs unicode_test.json - ../flatc --ts --gen-name-strings --gen-mutable --gen-object-api -o union_vector union_vector/union_vector.fbs - ../flatc --ts --gen-name-strings optional_scalars.fbs - ../flatc --ts --gen-name-strings --gen-object-api --gen-mutable -I ../ ./typescript_keywords.fbs test_dir/typescript_include.fbs test_dir/typescript_transitive_include.fbs ../reflection/reflection.fbs - ../flatc --ts --gen-name-strings --gen-object-api --gen-mutable --ts-flat-files -I ../ ./typescript_keywords.fbs test_dir/typescript_include.fbs test_dir/typescript_transitive_include.fbs ../reflection/reflection.fbs -fi -tsc -node -r esm JavaScriptTest -node -r esm JavaScriptUnionVectorTest -node -r esm JavaScriptFlexBuffersTest diff --git a/tests/monster_test.js b/tests/monster_test.js index b4e13d14b..afc333eeb 100644 --- a/tests/monster_test.js +++ b/tests/monster_test.js @@ -9,6 +9,7 @@ export { Race } from './my-game/example/race'; export { Referrable, ReferrableT } from './my-game/example/referrable'; export { Stat, StatT } from './my-game/example/stat'; export { StructOfStructs, StructOfStructsT } from './my-game/example/struct-of-structs'; +export { StructOfStructsOfStructs, StructOfStructsOfStructsT } from './my-game/example/struct-of-structs-of-structs'; export { Test, TestT } from './my-game/example/test'; export { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from './my-game/example/test-simple-table-with-enum'; export { TypeAliases, TypeAliasesT } from './my-game/example/type-aliases'; diff --git a/tests/my-game/example/struct-of-structs.js b/tests/my-game/example/struct-of-structs.js index 2debc2943..f265ba508 100644 --- a/tests/my-game/example/struct-of-structs.js +++ b/tests/my-game/example/struct-of-structs.js @@ -56,6 +56,7 @@ export class StructOfStructsT { this.c = c; } pack(builder) { - return StructOfStructs.createStructOfStructs(builder, (this.a === null ? 0 : this.a.id), (this.a === null ? 0 : this.a.distance), (this.b === null ? 0 : this.b.a), (this.b === null ? 0 : this.b.b), (this.c === null ? 0 : this.c.id), (this.c === null ? 0 : this.c.distance)); + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; + return StructOfStructs.createStructOfStructs(builder, ((_b = (_a = this.a) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0), ((_d = (_c = this.a) === null || _c === void 0 ? void 0 : _c.distance) !== null && _d !== void 0 ? _d : 0), ((_f = (_e = this.b) === null || _e === void 0 ? void 0 : _e.a) !== null && _f !== void 0 ? _f : 0), ((_h = (_g = this.b) === null || _g === void 0 ? void 0 : _g.b) !== null && _h !== void 0 ? _h : 0), ((_k = (_j = this.c) === null || _j === void 0 ? void 0 : _j.id) !== null && _k !== void 0 ? _k : 0), ((_m = (_l = this.c) === null || _l === void 0 ? void 0 : _l.distance) !== null && _m !== void 0 ? _m : 0)); } } diff --git a/tests/my-game/example/vec3.js b/tests/my-game/example/vec3.js index 5fd244737..102ce4ada 100644 --- a/tests/my-game/example/vec3.js +++ b/tests/my-game/example/vec3.js @@ -92,6 +92,7 @@ export class Vec3T { this.test3 = test3; } pack(builder) { - return Vec3.createVec3(builder, this.x, this.y, this.z, this.test1, this.test2, (this.test3 === null ? 0 : this.test3.a), (this.test3 === null ? 0 : this.test3.b)); + var _a, _b, _c, _d; + return Vec3.createVec3(builder, this.x, this.y, this.z, this.test1, this.test2, ((_b = (_a = this.test3) === null || _a === void 0 ? void 0 : _a.a) !== null && _b !== void 0 ? _b : 0), ((_d = (_c = this.test3) === null || _c === void 0 ? void 0 : _c.b) !== null && _d !== void 0 ? _d : 0)); } }