Skip to content

Commit dc3a52b

Browse files
authored
fix: re-enable unit tests (#209)
* fix: unit tests were disabled * chore: fix clang formatting * fix: issue for codespell * fix: issue with add_source_files for tests * fix: black formatter * chore: move formatting * fix: disable some static checks * fix: issue with cicd * chore: align generation of files
1 parent 439f653 commit dc3a52b

23 files changed

+317
-214
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
Make sure to follow the PR preparation steps in [CONTRIBUTING.md](https://github.com/godotjs/javascript/blob/master/CONTRIBUTING.md#preparing-your-pr) before submitting your PR:
66

7-
- [ ] format the codebase: from the root, run `bash ./clang_format.sh`.
7+
- [ ] format C++: from the root, run `bash ./misc/formatting/clang_format.sh`.

.github/workflows/static_checks.yml

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- name: Checkout workaround
3232
run: |
3333
mv tmp/misc/scripts misc/scripts
34-
cp clang_format.sh misc/scripts/clang_format.sh
34+
cp misc/formatting/clang_format.sh misc/scripts/clang_format.sh
3535
3636
- name: Install APT dependencies
3737
uses: awalsh128/cache-apt-pkgs-action@latest
@@ -66,14 +66,6 @@ jobs:
6666
run: |
6767
bash ./misc/scripts/header_guards.sh changed.txt
6868
69-
- name: Python style checks via black (black_format.sh)
70-
run: |
71-
if grep -qE '\.py$|SConstruct|SCsub' changed.txt || [ -z "$(cat changed.txt)" ]; then
72-
bash ./misc/scripts/black_format.sh
73-
else
74-
echo "Skipping Python formatting as no Python files were changed."
75-
fi
76-
7769
- name: Python scripts static analysis (mypy_check.sh)
7870
run: |
7971
if grep -qE '\.py$|SConstruct|SCsub' changed.txt || [ -z "$(cat changed.txt)" ]; then
@@ -86,19 +78,3 @@ jobs:
8678
run: |
8779
clang-format --version
8880
bash ./misc/scripts/clang_format.sh changed.txt
89-
90-
- name: Style checks via dotnet format (dotnet_format.sh)
91-
run: |
92-
if grep -q "modules/mono" changed.txt || [ -z "$(cat changed.txt)" ]; then
93-
bash ./misc/scripts/dotnet_format.sh
94-
else
95-
echo "Skipping dotnet format as no C# files were changed."
96-
fi
97-
98-
- name: Spell checks via codespell
99-
if: github.event_name == 'pull_request' && env.CHANGED_FILES != ''
100-
uses: codespell-project/actions-codespell@v2
101-
with:
102-
skip: "./bin,./thirdparty,*.desktop,*.svg,*.gen.*,*.po,*.pot,*.rc,./AUTHORS.md,./COPYRIGHT.txt,./DONORS.md,./core/input/gamecontrollerdb.txt,./core/string/locales.h,./editor/project_converter_3_to_4.cpp,./misc/scripts/codespell.sh,./platform/android/java/lib/src/com,./platform/web/node_modules,./platform/web/package-lock.json"
103-
ignore_words_list: "breaked,checkin,curvelinear,doubleclick,expct,findn,gird,hel,inout,labelin,lod,mis,nd,numer,ot,pointin,requestor,te,textin,thirdparty,vai"
104-
path: ${{ env.CHANGED_FILES }}

.github/workflows/windows_builds.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
run: |
7979
${{ matrix.bin }} --version
8080
${{ matrix.bin }} --help
81-
${{ matrix.bin }} --test
81+
${{ matrix.bin }} --test --force-colors
8282
8383
- name: Prepare artifact
8484
run: |

CONTRIBUTING.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@
22

33
## Project Structure
44

5-
TODO
5+
- [Clang](https://clang.llvm.org/): Code formatting
6+
- ``.clang-format``
7+
- ``.clang-tidy``
8+
- ``clang-format.sh``
9+
- [Custom Modules in C++](https://docs.godotengine.org/en/stable/contributing/development/core_and_modules/custom_modules_in_cpp.html#custom-modules-in-c)
10+
- ``config.py``: Configs for this module
11+
- ``doc``: For showing documentation in editor
12+
- ``doc_classes``: For showing documentation in editor
13+
- ``icons``: For showing icons in editor
14+
- ``SCsub``: Will be called from Godots `SConstruct` during build
15+
- ``editor``: Custom `.cpp` only bundled for `target=editor`
16+
- ``misc``: Scripts and other files, which aren't related to `c++`
17+
- ``tests``: Some testcases to run in CICD
18+
- ``thirdparty``: Dependencies or libraries which shouldn't be analysed by static checks
19+
- GodotJS custom data
20+
- ``.github``: Runs custom CI/CD in GitHub
21+
- ``docs``: Add some additional stuff for README.md
22+
- ``src``: Contains custom C++ code
23+
624

725
## Preparing your PR
826

9-
The project is using [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html) to format most files. You need to run `bash ./clang_format.sh` before your PR for a successful pipeline.
27+
The project is using [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html) to format C++ files.
28+
You need to run `bash ./misc/formatting/clang_format.sh` before your PR for a successful pipeline.
1029

1130
Furthermore, there is an `utf-8` and `LF` checker to fix file formats. Additionally, some spellchecks run inside the [pipeline](.github/workflows/static_checks.yml).
1231

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Download the binaries from the [release page](https://github.com/GodotExplorer/E
4646
- Use `scons` with those additional options `warnings=extra werror=yes module_text_server_fb_enabled=yes` to show all potential errors:
4747
- Windows: `scons platform=windows warnings=extra werror=yes module_text_server_fb_enabled=yes`
4848
- MacOS: `scons platform=macos arch=arm64 warnings=extra werror=yes module_text_server_fb_enabled=yes`
49+
- **Hint**: To enable unit tests you need to add ``tests=true`` to `scons` arguments
4950

5051
## Documentation, Tutorials & Demos
5152

SCsub

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,32 @@
11
#!/usr/bin/env python
2-
import platform, os, sys
2+
import os
3+
4+
from misc.generate.generate_files import generate
35

46
Import("env")
57
Import("env_modules")
6-
env_module = env_modules.Clone()
8+
9+
env_javascript = env_modules.Clone()
10+
11+
# Default engine to use we might add some more in the future like node or v8
712
JS_ENGINE = "quickjs"
8-
TOOLS = "editor" == env_module["target"]
13+
javascript_dir = os.path.join(GetLaunchDir(), "modules", os.path.basename(os.getcwd()))
914

15+
# Windows workaround
1016
if env["platform"] == "windows":
1117
if env["use_mingw"]:
1218
env.Append(LIBS=["pthread"])
1319

14-
15-
def open_file(path, mode):
16-
if platform.python_version() > "3":
17-
return open(path, mode, encoding="utf8")
18-
else:
19-
return open(path, mode)
20-
21-
22-
def dump_text_file_to_cpp(file):
23-
source = open_file(file, "r").read()
24-
lines = source.split("\n")
25-
source = ""
26-
length = len(lines)
27-
for i in range(length):
28-
line = lines[i].replace('"', '\\"')
29-
line = '\t"' + line + '\\n"'
30-
if i < length - 1:
31-
line += "\n"
32-
source += line
33-
return source
34-
35-
20+
# Add required files for quickjs into godot
3621
if JS_ENGINE == "quickjs":
3722
import generate_builtin_api
3823

39-
generate_builtin_api.generate_api_json(os.path.join(GetLaunchDir(), "modules", os.path.basename(os.getcwd())))
24+
generate_builtin_api.generate_api_json(javascript_dir)
4025
import thirdparty.quickjs.builtin_binding_generator
4126

4227
thirdparty.quickjs.builtin_binding_generator.generate_builtin_bindings()
4328
version = open("thirdparty/quickjs/quickjs/VERSION.txt", "r").read().split("\n")[0]
44-
quickjs_env = env_modules.Clone()
29+
quickjs_env = env_javascript.Clone()
4530
quickjs_env.Append(CPPDEFINES={"QUICKJS_CONFIG_VERSION": '"' + version + '"'})
4631
quickjs_env.Append(CPPDEFINES=["CONFIG_BIGNUM"])
4732
if "release" not in (quickjs_env["target"] or ""):
@@ -55,50 +40,50 @@ if JS_ENGINE == "quickjs":
5540
quickjs_env.add_source_files(env.modules_sources, "thirdparty/quickjs/*.cpp")
5641
quickjs_env.add_source_files(env.modules_sources, "thirdparty/quickjs/quickjs/*.c")
5742

58-
# Binding script to run at engine initializing
59-
with open("misc/godot.binding_script.gen.cpp", "w") as f:
60-
text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "../javascript_binder.h"\nString JavaScriptBinder::BINDING_SCRIPT_CONTENT = \n${source};'
61-
f.write(text.replace("${source}", dump_text_file_to_cpp("misc/binding_script.js")))
43+
# If target=editor is provided via scons
44+
if env.editor_build:
45+
env_javascript.add_source_files(env.modules_sources, "editor/workarounds/*.cpp")
46+
env_javascript.add_source_files(env.modules_sources, "editor/*.cpp")
47+
48+
# --- Generate editor tool files ---
49+
from misc.generate.editor_tools import get_editor_tools_files
50+
from misc.generate.editor_tools import get_editor_tools_header
51+
52+
files = get_editor_tools_files()
53+
generate(javascript_dir, get_editor_tools_header(), files)
54+
env_javascript.add_source_files(env.modules_sources, files.keys())
55+
56+
# If tests=yes flag is provided via scons
57+
if env["tests"]:
58+
env_javascript.Append(CPPDEFINES=["TESTS_ENABLED"])
59+
60+
# --- Generate test files ---
61+
from misc.generate.test_manager import get_test_files
62+
from misc.generate.test_manager import get_test_manager_header
63+
64+
files = get_test_files()
65+
generate(javascript_dir, get_test_manager_header(), files)
66+
env_javascript.add_source_files(env.modules_sources, files.keys())
67+
68+
# --- Files inside editor & targets ---
6269

6370
sources = [
6471
"register_types.cpp",
6572
"javascript_language.cpp",
6673
"javascript_instance.cpp",
6774
"javascript.cpp",
68-
"misc/godot.binding_script.gen.cpp",
6975
]
7076

71-
if TOOLS:
72-
base_text = (
73-
'/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString JavaScriptPlugin::{} = \n{};'
74-
)
75-
tool_fns = {
76-
"editor/godot.d.ts.gen.cpp": (
77-
"BUILTIN_DECLARATION_TEXT",
78-
dump_text_file_to_cpp("misc/godot.d.ts"),
79-
),
80-
"editor/tsconfig.json.gen.cpp": (
81-
"TSCONFIG_CONTENT",
82-
dump_text_file_to_cpp("misc/tsconfig.json"),
83-
),
84-
"editor/decorators.ts.gen.cpp": (
85-
"TS_DECORATORS_CONTENT",
86-
dump_text_file_to_cpp("misc/decorators.ts"),
87-
),
88-
"editor/package.json.gen.cpp": (
89-
"PACKAGE_JSON_CONTENT",
90-
dump_text_file_to_cpp("misc/package.json"),
91-
),
92-
}
93-
for fn, subs in tool_fns.items():
94-
with open_file(fn, "w") as fh:
95-
fh.write(base_text.format(*subs))
96-
env_module.add_source_files(env.modules_sources, fn)
97-
98-
99-
env_module.Append(CPPPATH=["#modules/javascript"])
100-
env_module.add_source_files(env.modules_sources, sources)
77+
# Add all required files for "Javascript" language into godot
78+
env_javascript.Append(CPPPATH=["#modules/javascript"])
79+
env_javascript.add_source_files(env.modules_sources, sources)
10180

102-
if env.editor_build:
103-
env_module.add_source_files(env.modules_sources, "editor/workarounds/*.cpp")
104-
env_module.add_source_files(env.modules_sources, "editor/*.cpp")
81+
# --- Generate binding files ---
82+
# Binding script to run at engine initializing
83+
# The bindings add some functionality for `this.connect(...)`
84+
from misc.generate.bindings import get_bindings_header
85+
from misc.generate.bindings import get_binding_files
86+
87+
files = get_binding_files()
88+
generate(javascript_dir, get_bindings_header(), files)
89+
env_javascript.add_source_files(env.modules_sources, files.keys())

generate_builtin_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,11 @@ def parse_class(cls):
372372
)
373373

374374

375-
def generate_api_json(MODULE_DIR):
376-
DOCS_DIR = os.path.abspath(os.path.join(MODULE_DIR, "../../doc/classes"))
375+
def generate_api_json(MODULES_DIR):
376+
DOCS_DIR = os.path.abspath(os.path.join(MODULES_DIR, "../../doc/classes"))
377377
if not os.path.isdir(DOCS_DIR) and len(sys.argv) > 1:
378378
DOCS_DIR = sys.argv[-1]
379-
OUTPUT_FILE = os.path.join(MODULE_DIR, "builtin_api.gen.json")
379+
OUTPUT_FILE = os.path.join(MODULES_DIR, "builtin_api.gen.json")
380380

381381
classes = []
382382
for cls in BUILTIN_CLASSES:

javascript_language.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ JavaScriptLanguage::~JavaScriptLanguage() {
5656
void JavaScriptLanguage::init() {
5757
ERR_FAIL_NULL(main_binder);
5858
main_binder->initialize();
59-
execute_file("modules/javascript/tests/UnitTest.js");
6059
}
6160

6261
void JavaScriptLanguage::finish() {
@@ -65,14 +64,10 @@ void JavaScriptLanguage::finish() {
6564
main_binder->language_finalize();
6665
}
6766

68-
Error JavaScriptLanguage::execute_file(const String &p_path) {
67+
Error JavaScriptLanguage::execute_file(const String &code) {
6968
ERR_FAIL_NULL_V(main_binder, ERR_BUG);
70-
Error err;
71-
String code = FileAccess::get_file_as_string(p_path, &err);
72-
if (err == OK) {
73-
JavaScriptGCHandler eval_ret;
74-
err = main_binder->eval_string(code, JavaScriptBinder::EVAL_TYPE_GLOBAL, p_path, eval_ret);
75-
}
69+
JavaScriptGCHandler eval_ret;
70+
const Error err = main_binder->eval_string(code, JavaScriptBinder::EVAL_TYPE_GLOBAL, "test.js", eval_ret);
7671
return err;
7772
}
7873

javascript_language.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ class JavaScriptLanguage : public ScriptLanguage {
5656

5757
virtual void init() override;
5858
virtual void finish() override;
59-
virtual Error execute_file(const String &p_path);
59+
60+
/**
61+
* Executes a js file with JavaScriptBinder - currently used via init() for testing the editor in cicd
62+
* @param code Code as string which should be executed
63+
* @return
64+
*/
65+
virtual Error execute_file(const String &code);
6066

6167
virtual void get_reserved_words(List<String> *p_words) const override;
6268
virtual bool is_control_flow_keyword(String p_keywords) const override;
File renamed without changes.

0 commit comments

Comments
 (0)