From a14eb70038e8c6d921d390529fb2b3ffffafdf9d Mon Sep 17 00:00:00 2001 From: a2nr Date: Sat, 1 Aug 2026 22:34:51 +0000 Subject: [PATCH] test(compiler-worker): regression test stdin passthrough untuk Python input() dan C scanf() --- compiler_worker/tests/__init__.py | 0 compiler_worker/tests/test_app.py | 80 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 compiler_worker/tests/__init__.py create mode 100644 compiler_worker/tests/test_app.py diff --git a/compiler_worker/tests/__init__.py b/compiler_worker/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/compiler_worker/tests/test_app.py b/compiler_worker/tests/test_app.py new file mode 100644 index 0000000..74ed72f --- /dev/null +++ b/compiler_worker/tests/test_app.py @@ -0,0 +1,80 @@ +"""Regression tests for compiler_worker stdin passthrough. + +compiler_worker/ is NOT a Python package — app.py is a standalone Flask +module. It is loaded via importlib from its absolute path. +""" + +import os +import importlib.util + +_HERE = os.path.dirname(os.path.abspath(__file__)) +_APP_PATH = os.path.join(os.path.dirname(_HERE), "app.py") + +spec = importlib.util.spec_from_file_location("worker_app", _APP_PATH) +worker_app = importlib.util.module_from_spec(spec) +spec.loader.exec_module(worker_app) + + +# --- Python input() --------------------------------------------------------- + + +def test_python_input_single_line_with_stdin(): + code = 'nama = input("Siapa nama kamu? ")\nprint(f"Halo, {nama}!")\n' + result = worker_app.run_python_code(code, stdin="Budi\n") + assert result["success"] is True + assert "Halo, Budi!" in result["output"] + + +def test_python_input_multiple_lines_with_stdin(): + code = ( + 'nama = input("Nama: ")\n' + 'umur = input("Umur: ")\n' + 'print(f"{nama} berumur {umur} tahun")\n' + ) + result = worker_app.run_python_code(code, stdin="Budi\n17\n") + assert result["success"] is True + assert "Budi berumur 17 tahun" in result["output"] + + +def test_python_input_empty_stdin_raises_eoferror(): + code = 'nama = input("Siapa nama kamu? ")\nprint(f"Halo, {nama}!")\n' + result = worker_app.run_python_code(code, stdin="") + # Worker reports success even when the child process exits with an error + # (subprocess returncode is not checked); the traceback lands in stderr. + assert result["success"] is True + assert "EOFError" in result["error"] + + +# --- C scanf() -------------------------------------------------------------- + + +def test_c_scanf_single_line_with_stdin(): + code = ( + "#include \n" + "int main() {\n" + " char nama[32];\n" + " scanf(\"%31s\", nama);\n" + " printf(\"Halo, %s!\\n\", nama);\n" + " return 0;\n" + "}\n" + ) + result = worker_app.run_c_code(code, stdin="Budi\n") + assert result["success"] is True + assert "Halo, Budi!" in result["output"] + + +def test_c_scanf_empty_stdin_returns_no_output(): + code = ( + "#include \n" + "int main() {\n" + " char nama[32] = \"\";\n" + " if (scanf(\"%31s\", nama) != 1) {\n" + " return 1;\n" + " }\n" + " printf(\"Halo, %s!\\n\", nama);\n" + " return 0;\n" + "}\n" + ) + result = worker_app.run_c_code(code, stdin="") + assert result["success"] is True + assert "Halo," not in result["output"]