create_test.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import re
  5. import sys
  6. from subprocess import call
  7. def main():
  8. # Change to the directory where the script is located,
  9. # so that the script can be run from any location.
  10. os.chdir(os.path.dirname(os.path.realpath(__file__)))
  11. parser = argparse.ArgumentParser(description="Creates a new unit test file.")
  12. parser.add_argument("name", type=str, help="The unit test name in PascalCase notation")
  13. parser.add_argument(
  14. "path",
  15. type=str,
  16. nargs="?",
  17. help="The path to the unit test file relative to the tests folder (default: .)",
  18. default=".",
  19. )
  20. parser.add_argument(
  21. "-i",
  22. "--invasive",
  23. action="store_true",
  24. help="if set, the script will automatically insert the include directive in test_main.cpp. Use with caution!",
  25. )
  26. args = parser.parse_args()
  27. snake_case_regex = re.compile(r"(?<!^)(?=[A-Z])")
  28. name_snake_case = snake_case_regex.sub("_", args.name).lower()
  29. file_path = os.path.normpath(os.path.join(args.path, f"test_{name_snake_case}.h"))
  30. print(file_path)
  31. if os.path.isfile(file_path):
  32. print(f'ERROR: The file "{file_path}" already exists.')
  33. sys.exit(1)
  34. with open(file_path, "w", encoding="utf-8", newline="\n") as file:
  35. file.write(
  36. """/**************************************************************************/
  37. /* test_{name_snake_case}.h {padding} */
  38. /**************************************************************************/
  39. /* This file is part of: */
  40. /* GODOT ENGINE */
  41. /* https://godotengine.org */
  42. /**************************************************************************/
  43. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  44. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  45. /* */
  46. /* Permission is hereby granted, free of charge, to any person obtaining */
  47. /* a copy of this software and associated documentation files (the */
  48. /* "Software"), to deal in the Software without restriction, including */
  49. /* without limitation the rights to use, copy, modify, merge, publish, */
  50. /* distribute, sublicense, and/or sell copies of the Software, and to */
  51. /* permit persons to whom the Software is furnished to do so, subject to */
  52. /* the following conditions: */
  53. /* */
  54. /* The above copyright notice and this permission notice shall be */
  55. /* included in all copies or substantial portions of the Software. */
  56. /* */
  57. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  58. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  59. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  60. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  61. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  62. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  63. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  64. /**************************************************************************/
  65. #ifndef TEST_{name_upper_snake_case}_H
  66. #define TEST_{name_upper_snake_case}_H
  67. #include "tests/test_macros.h"
  68. namespace Test{name_pascal_case} {{
  69. TEST_CASE("[{name_pascal_case}] Example test case") {{
  70. // TODO: Remove this comment and write your test code here.
  71. }}
  72. }} // namespace Test{name_pascal_case}
  73. #endif // TEST_{name_upper_snake_case}_H
  74. """.format(
  75. name_snake_case=name_snake_case,
  76. # Capitalize the first letter but keep capitalization for the rest of the string.
  77. # This is done in case the user passes a camelCase string instead of PascalCase.
  78. name_pascal_case=args.name[0].upper() + args.name[1:],
  79. name_upper_snake_case=name_snake_case.upper(),
  80. # The padding length depends on the test name length.
  81. padding=" " * (61 - len(name_snake_case)),
  82. )
  83. )
  84. # Print an absolute path so it can be Ctrl + clicked in some IDEs and terminal emulators.
  85. print("Test header file created:")
  86. print(os.path.abspath(file_path))
  87. if args.invasive:
  88. print("Trying to insert include directive in test_main.cpp...")
  89. with open("test_main.cpp", "r", encoding="utf-8") as file:
  90. contents = file.read()
  91. match = re.search(r'#include "tests.*\n', contents)
  92. if match:
  93. new_string = contents[: match.start()] + f'#include "tests/{file_path}"\n' + contents[match.start() :]
  94. with open("test_main.cpp", "w", encoding="utf-8", newline="\n") as file:
  95. file.write(new_string)
  96. print("Done.")
  97. # Use clang format to sort include directives afster insertion.
  98. clang_format_args = ["clang-format", "test_main.cpp", "-i"]
  99. retcode = call(clang_format_args)
  100. if retcode != 0:
  101. print(
  102. "Include directives in test_main.cpp could not be sorted automatically using clang-format. Please sort them manually."
  103. )
  104. else:
  105. print("Could not find a valid position in test_main.cpp to insert the include directive.")
  106. else:
  107. print("\nRemember to #include the new test header in this file (following alphabetical order):")
  108. print(os.path.abspath("test_main.cpp"))
  109. print("Insert the following line in the appropriate place:")
  110. print(f'#include "tests/{file_path}"')
  111. if __name__ == "__main__":
  112. main()