xref: /aosp_15_r20/external/flatbuffers/tests/TypeScriptTest.py (revision 890232f25432b36107d06881e0a25aaa6b473652)
1#!/usr/bin/env python3
2#
3# Copyright 2022 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
17import platform
18import shutil
19import subprocess
20import sys
21from pathlib import Path
22
23# Get the path where this script is located so we can invoke the script from
24# any directory and have the paths work correctly.
25tests_path = Path(__file__).parent.resolve()
26
27# Get the root path as an absolute path, so all derived paths are absolute.
28root_path = tests_path.parent.absolute()
29
30# Windows works with subprocess.run a bit differently.
31is_windows = platform.system() == "Windows"
32
33# Get the location of the flatc executable
34flatc_exe = Path("flatc.exe" if is_windows else "flatc")
35
36# Find and assert flatc compiler is present.
37if root_path in flatc_exe.parents:
38    flatc_exe = flatc_exe.relative_to(root_path)
39flatc_path = Path(root_path, flatc_exe)
40assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path)
41
42def check_call(args, cwd=tests_path):
43    subprocess.check_call(args, cwd=str(cwd), shell=is_windows)
44
45# Execute the flatc compiler with the specified parameters
46def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path):
47    cmd = [str(flatc_path)] + options
48    if prefix:
49        cmd += ["-o"] + [prefix]
50    if include:
51        cmd += ["-I"] + [include]
52    cmd += [schema] if isinstance(schema, str) else schema
53    if data:
54        cmd += [data] if isinstance(data, str) else data
55    check_call(cmd)
56
57print("Removing node_modules/ directory...")
58shutil.rmtree(Path(tests_path, "node_modules"), ignore_errors=True)
59
60check_call(["npm", "install", "--silent"])
61
62print("Invoking flatc...")
63flatc(
64    options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
65    schema="monster_test.fbs",
66    include="include_test",
67)
68
69flatc(
70    options=["--gen-object-api", "-b"],
71    schema="monster_test.fbs",
72    include="include_test",
73    data="unicode_test.json",
74)
75
76flatc(
77    options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
78    schema="union_vector/union_vector.fbs",
79    prefix="union_vector",
80)
81
82flatc(
83    options=["--ts", "--gen-name-strings"],
84    schema="optional_scalars.fbs",
85)
86
87flatc(
88    options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
89    schema=[
90        "typescript_keywords.fbs",
91        "test_dir/typescript_include.fbs",
92        "test_dir/typescript_transitive_include.fbs",
93        "../reflection/reflection.fbs",
94    ],
95    include="../",
96)
97
98flatc(
99    options=[
100        "--ts",
101        "--gen-name-strings",
102        "--gen-mutable",
103        "--gen-object-api",
104        "--ts-flat-files",
105    ],
106    schema=[
107        "typescript_keywords.fbs",
108        "test_dir/typescript_include.fbs",
109        "test_dir/typescript_transitive_include.fbs",
110        "../reflection/reflection.fbs",
111    ],
112    include="../",
113)
114
115print("Running TypeScript Compiler...")
116check_call(["tsc"])
117
118NODE_CMD = ["node", "-r", "esm"]
119
120print("Running TypeScript Tests...")
121check_call(NODE_CMD + ["JavaScriptTest"])
122check_call(NODE_CMD + ["JavaScriptUnionVectorTest"])
123check_call(NODE_CMD + ["JavaScriptFlexBuffersTest"])