Skip to content

Truly independent web browser

Tests: Add a test suite for running `js` as a debugger

Sam Atkins25f36a169c98
diff --git a/Tests/LibJS/CMakeLists.txt b/Tests/LibJS/CMakeLists.txt
index e8614e3e8f7..d69769f3a54 100644
--- a/Tests/LibJS/CMakeLists.txt
+++ b/Tests/LibJS/CMakeLists.txt
@@ -30,3 +30,12 @@ if (ENABLE_JS_JIT)
COMMAND "${Python3_EXECUTABLE}" "${ladybird_SOURCE_DIR}/Tests/LibJS/JIT/irtest.py" --build "${CMAKE_BINARY_DIR}"
)
endif()
+
+add_custom_target(test-js-debugger ALL DEPENDS js "${CMAKE_BINARY_DIR}/bin/test-js-debugger")
+add_custom_command(
+ OUTPUT "${CMAKE_BINARY_DIR}/bin/test-js-debugger"
+ COMMAND "${CMAKE_COMMAND}" -E copy "${ladybird_SOURCE_DIR}/Tests/LibJS/test-js-debugger.py" "${CMAKE_BINARY_DIR}/bin/test-js-debugger"
+ DEPENDS "${ladybird_SOURCE_DIR}/Tests/LibJS/test-js-debugger.py"
+)
+add_test(NAME test-js-debugger COMMAND "${Python3_EXECUTABLE}" "${CMAKE_BINARY_DIR}/bin/test-js-debugger")
+set_tests_properties(test-js-debugger PROPERTIES ENVIRONMENT LADYBIRD_SOURCE_DIR=${LADYBIRD_SOURCE_DIR})
diff --git a/Tests/LibJS/Debugger/expected/basic.txt b/Tests/LibJS/Debugger/expected/basic.txt
new file mode 100644
index 00000000000..3a86586639e
--- /dev/null
+++ b/Tests/LibJS/Debugger/expected/basic.txt
@@ -0,0 +1,14 @@
+Paused at <ladybird>/Tests/LibJS/Debugger/input/basic.js:7:1 (entry)
+Debugger commands:
+ break [file:]<line>[:column]
+ breakpoints
+ continue
+ delete <id>
+ help
+No breakpoints.
+1: <ladybird>/Tests/LibJS/Debugger/input/basic.js:4 (resolved)
+1: <ladybird>/Tests/LibJS/Debugger/input/basic.js:4 (resolved)
+Deleted breakpoint 1.
+2: <ladybird>/Tests/LibJS/Debugger/input/basic.js:4 (resolved)
+Paused at <ladybird>/Tests/LibJS/Debugger/input/basic.js:4:5 (breakpoint)
+Deleted breakpoint 2.
diff --git a/Tests/LibJS/Debugger/input/basic.commands b/Tests/LibJS/Debugger/input/basic.commands
new file mode 100644
index 00000000000..a9d2ce5aa3b
--- /dev/null
+++ b/Tests/LibJS/Debugger/input/basic.commands
@@ -0,0 +1,9 @@
+help
+breakpoints
+break 4
+breakpoints
+delete 1
+break 4
+continue
+delete 2
+continue
diff --git a/Tests/LibJS/Debugger/input/basic.js b/Tests/LibJS/Debugger/input/basic.js
new file mode 100644
index 00000000000..5f5f90411c4
--- /dev/null
+++ b/Tests/LibJS/Debugger/input/basic.js
@@ -0,0 +1,7 @@
+function calculate() {
+ let first = 20;
+ let second = 22;
+ return first + second;
+}
+
+calculate();
diff --git a/Tests/LibJS/Debugger/output/.gitignore b/Tests/LibJS/Debugger/output/.gitignore
new file mode 100644
index 00000000000..d6b7ef32c84
--- /dev/null
+++ b/Tests/LibJS/Debugger/output/.gitignore
@@ -0,0 +1,2 @@
+*
+!.gitignore
diff --git a/Tests/LibJS/test-js-debugger.py b/Tests/LibJS/test-js-debugger.py
new file mode 100755
index 00000000000..4b8e324b25b
--- /dev/null
+++ b/Tests/LibJS/test-js-debugger.py
@@ -0,0 +1,169 @@
+#!/usr/bin/env python3
+
+import difflib
+import os
+import shlex
+import subprocess
+import sys
+
+from argparse import ArgumentParser
+from concurrent.futures import ThreadPoolExecutor
+from concurrent.futures import as_completed
+from pathlib import Path
+
+LADYBIRD_SOURCE_DIR: Path
+DEBUGGER_TEST_DIR: Path
+BUILD_DIR: Path
+TEST_TIMEOUT_SECONDS = 30
+
+
+def setup() -> None:
+ global LADYBIRD_SOURCE_DIR, DEBUGGER_TEST_DIR, BUILD_DIR
+
+ ladybird_source_dir = os.getenv("LADYBIRD_SOURCE_DIR")
+
+ if ladybird_source_dir is None:
+ print("LADYBIRD_SOURCE_DIR must be set!")
+ sys.exit(1)
+
+ LADYBIRD_SOURCE_DIR = Path(ladybird_source_dir).resolve()
+ DEBUGGER_TEST_DIR = LADYBIRD_SOURCE_DIR.joinpath("Tests", "LibJS", "Debugger")
+
+ # The script is copied to bin/test-js-debugger, so the build dir is one level up
+ BUILD_DIR = Path(__file__).parent.parent.resolve()
+
+
+DIFF_PREFIX_ESCAPES = {
+ "@": "\x1b[36m",
+ "+": "\x1b[32m",
+ "-": "\x1b[31m",
+}
+
+
+def diff(a: str, a_file: Path, b: str, b_file: Path) -> None:
+ for line in difflib.unified_diff(a.splitlines(), b.splitlines(), fromfile=str(a_file), tofile=str(b_file)):
+ line = line.rstrip()
+
+ color_prefix = DIFF_PREFIX_ESCAPES.get((line or " ")[0], "")
+
+ print(f"{color_prefix}{line}\x1b[0m")
+
+
+def normalize_output(output: str) -> str:
+ source_directory = str(LADYBIRD_SOURCE_DIR)
+ if os.name == "nt":
+ output = output.replace("\\", "/")
+ source_directory = source_directory.replace("\\", "/")
+ return output.replace(source_directory, "<ladybird>").strip()
+
+
+def test(file: Path, rebaseline: bool) -> bool:
+ input_file = DEBUGGER_TEST_DIR / "input" / file
+ commands_file = input_file.with_suffix(".commands")
+ arguments_file = input_file.with_suffix(".args")
+ expected_file = DEBUGGER_TEST_DIR / "expected" / file.with_suffix(".txt")
+ output_file = DEBUGGER_TEST_DIR / "output" / file.with_suffix(".txt")
+
+ if not commands_file.is_file():
+ print(f"Missing debugger commands for {file}: {commands_file}")
+ return True
+
+ if not rebaseline and not expected_file.is_file():
+ print(f"Missing expected debugger output for {file}: {expected_file}")
+ return True
+
+ args = [
+ str(BUILD_DIR / "bin/js"),
+ "--debug",
+ "--disable-ansi-colors",
+ ]
+ if arguments_file.is_file():
+ try:
+ args.extend(shlex.split(arguments_file.read_text(encoding="utf8")))
+ except ValueError as error:
+ print(f"Unable to parse debugger arguments for {file}: {error}")
+ return True
+ args.append(str(input_file))
+
+ try:
+ process = subprocess.run(
+ args,
+ input=commands_file.read_text(encoding="utf8"),
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ text=True,
+ timeout=TEST_TIMEOUT_SECONDS,
+ )
+ except subprocess.TimeoutExpired as error:
+ stdout = error.stdout or ""
+ if isinstance(stdout, bytes):
+ stdout = stdout.decode("utf8", errors="replace")
+ stdout = normalize_output(stdout)
+ output_file.write_text(stdout + "\n", encoding="utf8")
+ print(f"\nDebugger test timed out after {TEST_TIMEOUT_SECONDS} seconds for {file}:\n")
+ if stdout:
+ print(stdout)
+ return True
+
+ stdout = normalize_output(process.stdout)
+ output_file.write_text(stdout + "\n", encoding="utf8")
+
+ if process.returncode != 0:
+ print(f"\nDebugger exited with code {process.returncode} for {file}:\n")
+ print(stdout)
+ return True
+
+ if rebaseline:
+ expected_file.write_text(stdout + "\n", encoding="utf8")
+ return False
+
+ expected = expected_file.read_text(encoding="utf8").strip()
+
+ if stdout != expected:
+ print(f"\nDebugger output does not match for {file}!\n")
+
+ diff(a=expected, a_file=expected_file, b=stdout, b_file=output_file)
+
+ return True
+
+ return False
+
+
+def main() -> int:
+ setup()
+
+ parser = ArgumentParser()
+ parser.add_argument("-j", "--jobs", type=int)
+ parser.add_argument("--rebaseline", action="store_true")
+
+ args = parser.parse_args()
+
+ input_dir = DEBUGGER_TEST_DIR / "input"
+ failed = 0
+
+ js_files = [js_file for js_file in sorted(input_dir.iterdir()) if js_file.is_file() and js_file.suffix == ".js"]
+
+ with ThreadPoolExecutor(max_workers=args.jobs) as executor:
+ executables = [executor.submit(test, js_file.relative_to(input_dir), args.rebaseline) for js_file in js_files]
+
+ for executable in as_completed(executables):
+ if executable.result():
+ failed += 1
+
+ total = len(js_files)
+ passed = total - failed
+
+ if failed:
+ print(f"\nTests: {passed} passed, {failed} failed, {total} total")
+ return 1
+
+ if args.rebaseline:
+ print(f"Rebaselined {total} tests.")
+ return 0
+
+ print(f"All tests passed! ({total} total)")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())