WIP: Add windows compatibility (#30)

* add package files

* add windows compat to subprocess.hpp

* add test modifications

* repair test_read_all
This commit is contained in:
xoviat 2019-05-02 01:32:17 -05:00 committed by Arun Muralidharan
parent de5f791d04
commit 5d92f48492
6 changed files with 640 additions and 216 deletions

1
.clang-format Normal file
View File

@ -0,0 +1 @@
BreakBeforeBraces: Stroustrup

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build*
.vscode

6
CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.9)
project(subprocess CXX)
enable_testing()
add_subdirectory(test)

File diff suppressed because it is too large Load Diff

8
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,8 @@
add_executable(test_subprocess test_subprocess.cc)
add_test(
NAME test_subprocess
COMMAND $<TARGET_FILE:test_subprocess>
)

View File

@ -1,19 +1,27 @@
#include <iostream>
#include "../subprocess.hpp" #include "../subprocess.hpp"
#include <iostream>
using namespace subprocess; using namespace subprocess;
void test_exename() void test_exename()
{ {
#ifdef _MSC_VER
auto ret = call({"--version"}, executable{"cmake"}, shell{false});
#else
auto ret = call({"-l"}, executable{"ls"}, shell{false}); auto ret = call({"-l"}, executable{"ls"}, shell{false});
#endif
std::cout << ret << std::endl; std::cout << ret << std::endl;
} }
void test_input() void test_input()
{ {
#if 0
auto p = Popen({"cmake", "--version"}, output{PIPE}, input{PIPE});
#else
auto p = Popen({"grep", "f"}, output{PIPE}, input{PIPE}); auto p = Popen({"grep", "f"}, output{PIPE}, input{PIPE});
const char* msg = "one\ntwo\nthree\nfour\nfive\n"; const char *msg = "one\ntwo\nthree\nfour\nfive\n";
p.send(msg, strlen(msg)); p.send(msg, strlen(msg));
#endif
auto res = p.communicate(nullptr, 0); auto res = p.communicate(nullptr, 0);
std::cout << res.first.buf.data() << std::endl; std::cout << res.first.buf.data() << std::endl;
} }
@ -22,7 +30,8 @@ void test_piping()
{ {
auto cat = Popen({"cat", "../subprocess.hpp"}, output{PIPE}); auto cat = Popen({"cat", "../subprocess.hpp"}, output{PIPE});
auto grep = Popen({"grep", "template"}, input{cat.output()}, output{PIPE}); auto grep = Popen({"grep", "template"}, input{cat.output()}, output{PIPE});
auto cut = Popen({"cut", "-d,", "-f", "1"}, input{grep.output()}, output{PIPE}); auto cut =
Popen({"cut", "-d,", "-f", "1"}, input{grep.output()}, output{PIPE});
auto res = cut.communicate().first; auto res = cut.communicate().first;
std::cout << res.buf.data() << std::endl; std::cout << res.buf.data() << std::endl;
} }
@ -45,7 +54,10 @@ void test_sleep()
while (p.poll() == -1) { while (p.poll() == -1) {
std::cout << "Waiting..." << std::endl; std::cout << "Waiting..." << std::endl;
#ifdef _MSC_VER
#else
sleep(1); sleep(1);
#endif
} }
std::cout << "Sleep ended: ret code = " << p.retcode() << std::endl; std::cout << "Sleep ended: ret code = " << p.retcode() << std::endl;
@ -56,7 +68,7 @@ void test_read_all()
Popen p = Popen({"echo","12345678"}, output{PIPE}); Popen p = Popen({"echo","12345678"}, output{PIPE});
std::vector<char> buf(6); std::vector<char> buf(6);
int rbytes = util::read_all(fileno(p.output()), buf); int rbytes = util::read_all(p.output(), buf);
std::string out(buf.begin(), buf.end()); std::string out(buf.begin(), buf.end());