...

Text file src/github.com/google/flatbuffers/tests/ts/TypeScriptTest.py

Documentation: github.com/google/flatbuffers/tests/ts

     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.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    print("Invoking flatc on schema " + str(schema))
    48    cmd = [str(flatc_path)] + options
    49    if prefix:
    50        cmd += ["-o"] + [prefix]
    51    if include:
    52        cmd += ["-I"] + [include]
    53    cmd += [schema] if isinstance(schema, str) else schema
    54    if data:
    55        cmd += [data] if isinstance(data, str) else data
    56    check_call(cmd)
    57
    58# Execute esbuild with the specified parameters
    59def esbuild(input, output):
    60    cmd = ["esbuild", input, "--outfile=" + output]
    61    cmd += ["--format=cjs", "--bundle", "--external:flatbuffers"]
    62    check_call(cmd)
    63
    64print("Removing node_modules/ directory...")
    65shutil.rmtree(Path(tests_path, "node_modules"), ignore_errors=True)
    66
    67check_call(["npm", "install", "--silent"])
    68
    69flatc(
    70    options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", "--ts-entry-points", "--ts-flat-files"],
    71    schema="../monster_test.fbs",
    72    include="../include_test",
    73)
    74esbuild("monster_test.ts", "monster_test_generated.cjs")
    75
    76flatc(
    77    options=["--gen-object-api", "-b"],
    78    schema="../monster_test.fbs",
    79    include="../include_test",
    80    data="../unicode_test.json",
    81)
    82
    83flatc(
    84    options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", "--ts-entry-points", "--ts-flat-files"],
    85    schema="../union_vector/union_vector.fbs",
    86    prefix="union_vector",
    87)
    88esbuild("union_vector/union_vector.ts", "union_vector/union_vector_generated.cjs")
    89
    90flatc(
    91    options=["--ts", "--reflect-names", "--gen-name-strings"],
    92    schema="../optional_scalars.fbs",
    93)
    94
    95flatc(
    96    options=["--ts", "--reflect-names", "--gen-name-strings", "--ts-no-import-ext"],
    97    schema="../optional_scalars.fbs",
    98    prefix="no_import_ext",
    99)
   100
   101flatc(
   102    options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-object-api", "--ts-entry-points", "--ts-flat-files"],
   103    schema="arrays_test_complex/arrays_test_complex.fbs",
   104    prefix="arrays_test_complex"
   105)
   106esbuild("arrays_test_complex/my-game/example.ts", "arrays_test_complex/arrays_test_complex_generated.cjs")
   107
   108flatc(
   109    options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", "--ts-entry-points", "--ts-flat-files"],
   110    schema=[
   111        "typescript_keywords.fbs",
   112        "test_dir/typescript_include.fbs",
   113        "test_dir/typescript_transitive_include.fbs",
   114        "../../reflection/reflection.fbs",
   115    ],
   116    include="../../",
   117)
   118esbuild("typescript_keywords.ts", "typescript_keywords_generated.cjs")
   119
   120flatc(
   121    options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", "--ts-entry-points", "--ts-flat-files"],
   122    schema="../union_underlying_type_test.fbs"
   123)
   124
   125print("Running TypeScript Compiler...")
   126check_call(["tsc"])
   127print("Running TypeScript Compiler in old node resolution mode for no_import_ext...")
   128check_call(["tsc", "-p", "./tsconfig.node.json"])
   129
   130NODE_CMD = ["node"]
   131
   132print("Running TypeScript Tests...")
   133check_call(NODE_CMD + ["JavaScriptTest"])
   134check_call(NODE_CMD + ["JavaScriptUnionVectorTest"])
   135check_call(NODE_CMD + ["JavaScriptFlexBuffersTest"])
   136check_call(NODE_CMD + ["JavaScriptComplexArraysTest"])
   137check_call(NODE_CMD + ["JavaScriptUnionUnderlyingTypeTest"])
   138
   139print("Running old v1 TypeScript Tests...")
   140check_call(NODE_CMD + ["JavaScriptTestv1.cjs", "./monster_test_generated.cjs"])

View as plain text