Skip to content

Commit f7692aa

Browse files
committed
Merge pull request #118220 from StarryWorm/modernize-test-creator
Modernize `create_test.py`
2 parents 7c720b8 + 3778da1 commit f7692aa

1 file changed

Lines changed: 10 additions & 42 deletions

File tree

tests/create_test.py

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import re
66
import sys
7-
from subprocess import call
87

98

109
def main():
@@ -25,19 +24,13 @@ def main():
2524
help="The path to the unit test file relative to the tests folder (e.g. core). This should correspond to the relative path of the class or component being tested. (default: .)",
2625
default=".",
2726
)
28-
parser.add_argument(
29-
"-i",
30-
"--invasive",
31-
action="store_true",
32-
help="if set, the script will automatically insert the include directive in test_main.cpp. Use with caution!",
33-
)
3427
args = parser.parse_args()
3528

3629
snake_case_regex = re.compile(r"(?<!^)(?=[A-Z, 0-9])")
3730
# Replace 2D, 3D, and 4D with 2d, 3d, and 4d, respectively. This avoids undesired splits like node_3_d.
3831
prefiltered_name = re.sub(r"([234])D", lambda match: match.group(1).lower() + "d", args.name)
3932
name_snake_case = snake_case_regex.sub("_", prefiltered_name).lower()
40-
file_path = os.path.normpath(os.path.join(args.path, f"test_{name_snake_case}.h"))
33+
file_path = os.path.normpath(os.path.join(args.path, f"test_{name_snake_case}.cpp"))
4134

4235
# Ensure the directory exists.
4336
os.makedirs(os.path.dirname(file_path), exist_ok=True)
@@ -49,7 +42,7 @@ def main():
4942
with open(file_path, "w", encoding="utf-8", newline="\n") as file:
5043
file.write(
5144
"""/**************************************************************************/
52-
/* test_{name_snake_case}.h {padding} */
45+
/* test_{name_snake_case}.cpp {padding} */
5346
/**************************************************************************/
5447
/* This file is part of: */
5548
/* GODOT ENGINE */
@@ -78,14 +71,14 @@ def main():
7871
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
7972
/**************************************************************************/
8073
81-
#pragma once
82-
8374
#include "tests/test_macros.h"
8475
76+
TEST_FORCE_LINK(test_{name_snake_case})
77+
8578
namespace Test{name_pascal_case} {{
8679
8780
TEST_CASE("[{name_pascal_case}] Example test case") {{
88-
// TODO: Remove this comment and write your test code here.
81+
// TODO: Remove this comment and write your test code here.
8982
}}
9083
9184
}} // namespace Test{name_pascal_case}
@@ -95,41 +88,16 @@ def main():
9588
# This is done in case the user passes a camelCase string instead of PascalCase.
9689
name_pascal_case=args.name[0].upper() + args.name[1:],
9790
# The padding length depends on the test name length.
98-
padding=" " * (61 - len(name_snake_case)),
91+
padding=" " * (59 - len(name_snake_case)),
9992
)
10093
)
10194

10295
# Print an absolute path so it can be Ctrl + clicked in some IDEs and terminal emulators.
103-
print("Test header file created:")
96+
print("Test file created:")
10497
print(os.path.abspath(file_path))
105-
106-
if args.invasive:
107-
print("Trying to insert include directive in test_main.cpp...")
108-
with open("test_main.cpp", "r", encoding="utf-8") as file:
109-
contents = file.read()
110-
match = re.search(r'#include "tests.*\n', contents)
111-
112-
if match:
113-
new_string = contents[: match.start()] + f'#include "tests/{file_path}"\n' + contents[match.start() :]
114-
115-
with open("test_main.cpp", "w", encoding="utf-8", newline="\n") as file:
116-
file.write(new_string)
117-
print("Done.")
118-
# Use clang format to sort include directives afster insertion.
119-
clang_format_args = ["clang-format", "test_main.cpp", "-i"]
120-
retcode = call(clang_format_args)
121-
if retcode != 0:
122-
print(
123-
"Include directives in test_main.cpp could not be sorted automatically using clang-format. Please sort them manually."
124-
)
125-
else:
126-
print("Could not find a valid position in test_main.cpp to insert the include directive.")
127-
128-
else:
129-
print("\nRemember to #include the new test header in this file (following alphabetical order):")
130-
print(os.path.abspath("test_main.cpp"))
131-
print("Insert the following line in the appropriate place:")
132-
print(f'#include "tests/{file_path}"')
98+
print(
99+
"Don't forget to add special tags to the test case if needed, see https://docs.godotengine.org/en/latest/contributing/development/core_and_modules/unit_testing.html#special-tags-in-test-case-names for more information."
100+
)
133101

134102

135103
if __name__ == "__main__":

0 commit comments

Comments
 (0)