30 lines
1.5 KiB
Python
30 lines
1.5 KiB
Python
"""Commands are consumed once, outside the UI callback which caused the reconnect crash."""
|
|
from pathlib import Path
|
|
import tempfile,subprocess,unittest
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
class CommandFileTest(unittest.TestCase):
|
|
def test_consumption_and_failure(self):
|
|
code=r'''
|
|
#include "python_command_file.h"
|
|
#include <cassert>
|
|
int main(){
|
|
std::string err;int calls=0;
|
|
auto run=[&](const std::string& c){++calls;assert(c=="test()");return true;};
|
|
assert(mt_host::consume_python_command_file("",run,err) && calls==0);
|
|
assert(mt_host::consume_python_command_file("command.py",run,err) && calls==0);
|
|
{std::ofstream f("command.py");f<<"test()";}
|
|
assert(mt_host::consume_python_command_file("command.py",run,err) && calls==1);
|
|
assert(mt_host::consume_python_command_file("command.py",run,err) && calls==1);
|
|
{std::ofstream f("command.py");f<<"fail()";}
|
|
assert(!mt_host::consume_python_command_file("command.py",[&](const std::string&){++calls;return false;},err));
|
|
assert(!std::filesystem::exists("command.py") && calls==2);
|
|
{std::ofstream f("command.py");f<<std::string(1024*1024+1,'x');}
|
|
assert(!mt_host::consume_python_command_file("command.py",run,err) && calls==2 && !err.empty());
|
|
assert(!std::filesystem::exists("command.py"));
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p=Path(d);(p/'test.cpp').write_text(code)
|
|
subprocess.run(['c++','-std=c++17','-I'+str(ROOT/'src/host'),str(p/'test.cpp'),'-o',str(p/'test')],check=True)
|
|
subprocess.run([str(p/'test')],cwd=d,check=True)
|