Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2021 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 | |
| 17 | import platform |
| 18 | import subprocess |
| 19 | from pathlib import Path |
| 20 | |
| 21 | # Get the path where this script is located so we can invoke the script from |
| 22 | # any directory and have the paths work correctly. |
| 23 | script_path = Path(__file__).parent.resolve() |
| 24 | |
| 25 | # Get the root path as an absolute path, so all derived paths are absolute. |
| 26 | root_path = script_path.parent.parent.absolute() |
| 27 | |
| 28 | # Get the location of the flatc executable, reading from the first command line |
| 29 | # argument or defaulting to default names. |
| 30 | flatc_exe = Path( |
| 31 | ("flatc" if not platform.system() == "Windows" else "flatc.exe") |
| 32 | ) |
| 33 | |
| 34 | # Find and assert flatc compiler is present. |
| 35 | if root_path in flatc_exe.parents: |
| 36 | flatc_exe = flatc_exe.relative_to(root_path) |
| 37 | flatc_path = Path(root_path, flatc_exe) |
| 38 | assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path) |
| 39 | |
| 40 | # Specify the other paths that will be referenced |
| 41 | tests_path = Path(script_path, "tests") |
| 42 | |
| 43 | |
| 44 | def flatc_annotate(schema, file, cwd=script_path): |
| 45 | cmd = [str(flatc_path), "--annotate", schema, file] |
| 46 | result = subprocess.run(cmd, cwd=str(cwd), check=True) |
| 47 | |
| 48 | |
| 49 | test_files = [ |
| 50 | "annotated_binary.bin", |
| 51 | "tests/invalid_root_offset.bin", |
| 52 | "tests/invalid_root_table_too_short.bin", |
| 53 | "tests/invalid_root_table_vtable_offset.bin", |
| 54 | "tests/invalid_string_length.bin", |
| 55 | "tests/invalid_string_length_cut_short.bin", |
| 56 | "tests/invalid_struct_array_field_cut_short.bin", |
| 57 | "tests/invalid_struct_field_cut_short.bin", |
| 58 | "tests/invalid_table_field_size.bin", |
| 59 | "tests/invalid_table_field_offset.bin", |
| 60 | "tests/invalid_union_type_value.bin", |
| 61 | "tests/invalid_vector_length_cut_short.bin", |
| 62 | "tests/invalid_vector_scalars_cut_short.bin", |
| 63 | "tests/invalid_vector_strings_cut_short.bin", |
| 64 | "tests/invalid_vector_structs_cut_short.bin", |
| 65 | "tests/invalid_vector_tables_cut_short.bin", |
| 66 | "tests/invalid_vector_unions_cut_short.bin", |
| 67 | "tests/invalid_vector_union_type_value.bin", |
| 68 | "tests/invalid_vtable_ref_table_size_short.bin", |
| 69 | "tests/invalid_vtable_ref_table_size.bin", |
| 70 | "tests/invalid_vtable_size_short.bin", |
| 71 | "tests/invalid_vtable_size.bin", |
| 72 | "tests/invalid_vtable_field_offset.bin", |
| 73 | ] |
| 74 | |
| 75 | for test_file in test_files: |
| 76 | flatc_annotate("annotated_binary.fbs", test_file) |