Update TypeScriptTest.py to work better cross platform (#7436)

This commit is contained in:
Derek Bailey
2022-08-13 17:01:32 -07:00
committed by GitHub
parent 8b8c7dbdfc
commit 67c4149588

View File

@@ -14,12 +14,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import argparse
import filecmp
import glob
import platform import platform
import shutil import shutil
import subprocess import subprocess
import sys
from pathlib import Path from pathlib import Path
# Get the path where this script is located so we can invoke the script from # Get the path where this script is located so we can invoke the script from
@@ -29,8 +27,11 @@ tests_path = Path(__file__).parent.resolve()
# Get the root path as an absolute path, so all derived paths are absolute. # Get the root path as an absolute path, so all derived paths are absolute.
root_path = tests_path.parent.absolute() root_path = tests_path.parent.absolute()
# Windows works with subprocess.run a bit differently.
is_windows = platform.system() == "Windows"
# Get the location of the flatc executable # Get the location of the flatc executable
flatc_exe = Path("flatc" if not platform.system() == "Windows" else "flatc.exe") flatc_exe = Path("flatc.exe" if is_windows else "flatc")
# Find and assert flatc compiler is present. # Find and assert flatc compiler is present.
if root_path in flatc_exe.parents: if root_path in flatc_exe.parents:
@@ -38,6 +39,9 @@ if root_path in flatc_exe.parents:
flatc_path = Path(root_path, flatc_exe) flatc_path = Path(root_path, flatc_exe)
assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path) assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path)
def check_call(args, cwd=tests_path):
subprocess.check_call(args, cwd=str(cwd), shell=is_windows)
# Execute the flatc compiler with the specified parameters # Execute the flatc compiler with the specified parameters
def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path): def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path):
cmd = [str(flatc_path)] + options cmd = [str(flatc_path)] + options
@@ -48,13 +52,12 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
cmd += [schema] if isinstance(schema, str) else schema cmd += [schema] if isinstance(schema, str) else schema
if data: if data:
cmd += [data] if isinstance(data, str) else data cmd += [data] if isinstance(data, str) else data
result = subprocess.run(cmd, cwd=str(cwd), check=True) check_call(cmd)
print("Removing node_modules/ directory...") print("Removing node_modules/ directory...")
shutil.rmtree(Path(tests_path, "node_modules"), ignore_errors=True) shutil.rmtree(Path(tests_path, "node_modules"), ignore_errors=True)
assert subprocess.run(["npm", "install", "--silent"], cwd=str(tests_path), shell=True) check_call(["npm", "install", "--silent"])
print("Invoking flatc...") print("Invoking flatc...")
flatc( flatc(
@@ -110,15 +113,11 @@ flatc(
) )
print("Running TypeScript Compiler...") print("Running TypeScript Compiler...")
assert subprocess.run(["tsc"], cwd=str(tests_path), shell=True) check_call(["tsc"])
NODE_CMD = ["node", "-r", "esm"] NODE_CMD = ["node", "-r", "esm"]
print("Running TypeScript Tests...") print("Running TypeScript Tests...")
assert subprocess.run(NODE_CMD + ["JavaScriptTest"], cwd=str(tests_path), shell=True) check_call(NODE_CMD + ["JavaScriptTest"])
assert subprocess.run( check_call(NODE_CMD + ["JavaScriptUnionVectorTest"])
NODE_CMD + ["JavaScriptUnionVectorTest"], cwd=str(tests_path), shell=True check_call(NODE_CMD + ["JavaScriptFlexBuffersTest"])
)
assert subprocess.run(
NODE_CMD + ["JavaScriptFlexBuffersTest"], cwd=str(tests_path), shell=True
)