diff --git a/crates/erg_common/python_util.rs b/crates/erg_common/python_util.rs index c218d3969..074173eca 100644 --- a/crates/erg_common/python_util.rs +++ b/crates/erg_common/python_util.rs @@ -738,15 +738,14 @@ pub fn _eval_pyc>(file: S, py_command: Option<&str>) -> String { String::from_utf8_lossy(&out.stdout).to_string() } -pub fn _exec_py(code: &str) -> Option { +pub fn exec_py(file: &str) -> Option { let mut child = if cfg!(windows) { Command::new(which_python()) - .arg("-c") - .arg(code) + .arg(file) .spawn() .expect("cannot execute python") } else { - let exec_command = format!("{} -c \"{}\"", which_python(), code); + let exec_command = format!("{} {file}", which_python()); Command::new("sh") .arg("-c") .arg(exec_command) diff --git a/src/scripts/repl_server_test.py b/src/scripts/repl_server_test.py new file mode 100644 index 000000000..da4bf467d --- /dev/null +++ b/src/scripts/repl_server_test.py @@ -0,0 +1,40 @@ +import itertools +import random +import string + +with open("./src/scripts/repl_server.py") as f: + code = f.readlines() + +code.insert(0, "__PORT__ = 9000\n") +code = itertools.takewhile(lambda l: not l.startswith("# DummyVM"), code) + +exec("".join(code)) + + +class MockSocket: + def __init__(self): + self.data = bytearray() + self.cursor = 0 + + def send(self, data): + self.data.extend(data) + + def recv(self, bufsize): + if self.cursor > len(self.data): + raise Exception(f"MockSocket: recv({bufsize}) out of range") + data = bytes(self.data[self.cursor : self.cursor + bufsize]) + self.cursor += bufsize + return data + +corr_data = "".join(random.choices(string.ascii_uppercase + string.digits, k=2048)) +s = MessageStream(MockSocket()) + +s.send_msg(INST.PRINT, corr_data) +inst, recv_data = s.recv_msg() +assert inst == INST.PRINT +assert recv_data == corr_data + +s.send_msg(INST.EXIT, "") +inst, recv_data = s.recv_msg() +assert inst == INST.EXIT +assert recv_data == "" diff --git a/tests/repl.rs b/tests/repl.rs index 4df5c898f..7378b16a9 100644 --- a/tests/repl.rs +++ b/tests/repl.rs @@ -1,6 +1,8 @@ mod common; + use common::expect_repl_failure; use common::expect_repl_success; +use erg_common::python_util::exec_py; #[test] #[ignore] @@ -193,3 +195,10 @@ fn exec_repl_invalid_def_after_the_at_sign() -> Result<(), ()> { 1, ) } + +#[test] +#[ignore] +fn exec_repl_server_mock_test() -> Result<(), ()> { + assert_eq!(exec_py("src/scripts/repl_server_test.py"), Some(0)); + Ok(()) +}