From 4e6c2c2d956ca7207f6c4c4460da35e56b8fc4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Sverre=20Lien=20Sell=C3=A6g?= Date: Tue, 8 Sep 2020 21:48:37 +0200 Subject: [PATCH] bind up tiny process lib and add test should also fix terminal warnings --- src/CMakeLists.txt | 1 + src/python_module.cc | 2 ++ src/terminal.cpp | 10 +++---- src/tiny_process_module.cpp | 27 +++++++++++++++++++ src/tiny_process_module.hpp | 6 +++++ .../Terminal_tests/terminal_test.cc | 11 ++++++++ .../Terminal_tests/terminal_test.py | 13 ++++++++- 7 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/tiny_process_module.cpp create mode 100644 src/tiny_process_module.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f0f0007..7387a0d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,6 +22,7 @@ set(JUCI_SHARED_FILES source_language_protocol.cpp source_spellcheck.cpp terminal.cpp + tiny_process_module.cpp tooltips.cpp usages_clang.cpp utility.cpp diff --git a/src/python_module.cc b/src/python_module.cc index ad34880..3b4cef4 100644 --- a/src/python_module.cc +++ b/src/python_module.cc @@ -9,6 +9,7 @@ #include "dialogs.hpp" #include "git.hpp" #include "terminal.hpp" +#include "tiny_process_module.hpp" PyObject *Module::init_jucipp_module() { auto api = py::module("Jucipp", "API"); @@ -23,5 +24,6 @@ PyObject *Module::init_jucipp_module() { Dispatcher::init_module(api); Git::init_module(api); Terminal::init_module(api); + TinyProcessModule::init_module(api); return api.ptr(); } diff --git a/src/terminal.cpp b/src/terminal.cpp index 77885a4..ce7e8a7 100644 --- a/src/terminal.cpp +++ b/src/terminal.cpp @@ -7,6 +7,8 @@ #include "utility.hpp" #include #include +#include +#include #include #include @@ -611,7 +613,7 @@ void Terminal::init_module(pybind11::module &api) { py::arg("command"), py::arg("path") = "", py::arg("use_pipes") = false) - .def("async_process", (void (Terminal::*)(const std::string &, const boost::filesystem::path &, const std::function &, bool)) & Terminal::async_process, + .def("async_process", &Terminal::async_process, py::arg("command"), py::arg("path") = "", py::arg("callback") = nullptr, @@ -623,14 +625,12 @@ void Terminal::init_module(pybind11::module &api) { .def("print", &Terminal::print, py::arg("message"), py::arg("bold") = false) - .def("async_print", (void (Terminal::*)(const std::string &, bool)) & Terminal::async_print, + .def("async_print", (void (Terminal::*)(std::string, bool)) & Terminal::async_print, py::arg("message"), py::arg("bold") = false) - .def("async_print", (void (Terminal::*)(size_t, const std::string &)) & Terminal::async_print, - py::arg("line_nr"), - py::arg("message")) .def("configure", &Terminal::configure) .def("clear", &Terminal::clear) + .def_readwrite("", &Terminal::scroll_to_bottom) ; } diff --git a/src/tiny_process_module.cpp b/src/tiny_process_module.cpp new file mode 100644 index 0000000..b5dd88e --- /dev/null +++ b/src/tiny_process_module.cpp @@ -0,0 +1,27 @@ +#include "tiny_process_module.hpp" +#include + +void TinyProcessModule::init_module(py::module &api) { + py::class_> process(api, "Process"); + process + // .def("kill", (void (TinyProcessLib::Process::*)(TinyProcessLib::Process::id_type, bool)) & TinyProcessLib::Process::kill, + // py::arg("id"), + // py::arg("force") = false) + // .def(py::init(), + // py::arg("command"), + // py::arg("path") = TinyProcessLib::Process::string_type()) + .def("get_id", &TinyProcessLib::Process::get_id) + .def("get_exit_status", &TinyProcessLib::Process::get_exit_status) + .def("try_get_exit_status", &TinyProcessLib::Process::try_get_exit_status, + py::arg("exit_status")) + .def("write", (bool (TinyProcessLib::Process::*)(const char *, size_t)) & TinyProcessLib::Process::write, + py::arg("bytes"), + py::arg("n")) + .def("write", (bool (TinyProcessLib::Process::*)(const std::string &)) & TinyProcessLib::Process::write, + py::arg("string")) + .def("close_stdin", &TinyProcessLib::Process::close_stdin) + .def("kill", (void (TinyProcessLib::Process::*)(bool)) & TinyProcessLib::Process::kill, + py::arg("force")) + + ; +} diff --git a/src/tiny_process_module.hpp b/src/tiny_process_module.hpp new file mode 100644 index 0000000..01bae74 --- /dev/null +++ b/src/tiny_process_module.hpp @@ -0,0 +1,6 @@ +#include "python_bind.h" + +class TinyProcessModule { +public: + static void init_module(py::module &api); +}; \ No newline at end of file diff --git a/tests/python_bindings/Terminal_tests/terminal_test.cc b/tests/python_bindings/Terminal_tests/terminal_test.cc index 48821a6..329dd27 100644 --- a/tests/python_bindings/Terminal_tests/terminal_test.cc +++ b/tests/python_bindings/Terminal_tests/terminal_test.cc @@ -37,4 +37,15 @@ int main() { connection.disconnect(); } } + { + suite test_suite(test_directory); + try { + const auto ls_dir = test_suite.test_file_path / test_directory / "ls"; + py::module::import("terminal_test").attr("async_process")(ls_dir); + test_suite.has_assertion = true; + } + catch(const py::error_already_set &error) { + std::cout << error.what(); + } + } } \ No newline at end of file diff --git a/tests/python_bindings/Terminal_tests/terminal_test.py b/tests/python_bindings/Terminal_tests/terminal_test.py index d8151d6..e364365 100644 --- a/tests/python_bindings/Terminal_tests/terminal_test.py +++ b/tests/python_bindings/Terminal_tests/terminal_test.py @@ -1,4 +1,5 @@ from Jucipp import Terminal +from jucipp_test import assert_equal t = Terminal() @@ -9,7 +10,17 @@ def clear(): t.clear() def process(path): - return t.process("ls", path, True) + p = t.process("ls", path, True) + assert_equal(p, 0) + return p def async_print(): return t.async_print("Hello, World!") + +def callback(exit_code): + assert_equal(0, exit_code) + +def async_process(path): + p = t.async_process("ls", path, callback, True) + assert_equal(0, p.get_exit_status()) + return p.get_exit_status()