Skip to content

Commit

Permalink
add tests to verify capturing output from ChildProcess
Browse files Browse the repository at this point in the history
Summary: as above.

Reviewed By: sid0

Differential Revision: D4424911

fbshipit-source-id: 595457173c4b21d512a18fd17c360d529e72299b
  • Loading branch information
wez authored and facebook-github-bot committed Jan 23, 2017
1 parent 3aec455 commit ec480f1
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ChildProcess.cpp
Expand Up @@ -375,4 +375,9 @@ void ChildProcess::kill(
}
#endif
}

Pipe& ChildProcess::pipe(int fd) {
return *pipes_.at(fd);
}

}
2 changes: 2 additions & 0 deletions ChildProcess.h
Expand Up @@ -144,6 +144,8 @@ class ChildProcess {
#endif
);

Pipe& pipe(int fd);

private:
pid_t pid_;
bool waited_{false};
Expand Down
14 changes: 14 additions & 0 deletions Makefile.am
Expand Up @@ -133,6 +133,7 @@ WILDMATCH_LIB = libwildmatch.a
TESTS = \
tests/art.t \
tests/bser.t \
tests/childproc.t \
tests/ignore.t \
tests/pending.t \
tests/string.t \
Expand Down Expand Up @@ -213,6 +214,19 @@ tests_art_t_SOURCES = \
hash.cpp \
log.cpp

tests_childproc_t_CPPFLAGS = $(THIRDPARTY_CPPFLAGS) @IRONMANCFLAGS@
tests_childproc_t_LDADD = $(TAP_LIB) $(JSON_LIB)
tests_childproc_t_SOURCES = \
tests/childproc.cpp \
tests/log_stub.cpp \
ChildProcess.cpp \
Pipe.cpp \
FileDescriptor.cpp \
PubSub.cpp \
string.cpp \
hash.cpp \
log.cpp

tests_ignore_t_CPPFLAGS = $(THIRDPARTY_CPPFLAGS) @IRONMANCFLAGS@
tests_ignore_t_LDADD = $(TAP_LIB) $(JSON_LIB)
tests_ignore_t_SOURCES = \
Expand Down
3 changes: 3 additions & 0 deletions TARGETS
Expand Up @@ -181,6 +181,9 @@ cpp_library(
'opendir.cpp',
'pending.cpp',
'time.cpp',
'ChildProcess.cpp',
'Pipe.cpp',
'FileDescriptor.cpp',
],
deps=[
':headers',
Expand Down
8 changes: 8 additions & 0 deletions tests/TARGETS
Expand Up @@ -77,3 +77,11 @@ t_test(
'@/watchman/thirdparty/wildmatch:wildmatch',
],
)

t_test(
name='childproc',
srcs=['childproc.cpp', 'log_stub.cpp'],
deps=[
'@/watchman:testsupport',
],
)
48 changes: 48 additions & 0 deletions tests/childproc.cpp
@@ -0,0 +1,48 @@
/* Copyright 2017-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0. */
#include "watchman_system.h"
#include "ChildProcess.h"
#include "thirdparty/tap.h"

using watchman::ChildProcess;
using Environment = ChildProcess::Environment;
using Options = ChildProcess::Options;

void test_pipe() {
Options opts;
opts.pipeStdout();
ChildProcess echo(
{
#ifndef _WIN32
"/bin/echo",
#else
"cmd",
"/c",
"echo",
#endif
"hello"},
std::move(opts));

echo.pipe(STDOUT_FILENO).read.clearNonBlock();
echo.wait();

char buf[128];
auto len = read(echo.pipe(STDOUT_FILENO).read.fd(), buf, sizeof(buf));

ok(len >= 0,
"read from pipe was successful: len=%d err=%s",
len,
strerror(errno));
w_string_piece line(buf, len);
ok(line.startsWith("hello"), "starts with hello");
}

int main(int argc, char **argv) {
(void)argc;
(void)argv;

plan_tests(2);
test_pipe();

return exit_status();
}
19 changes: 18 additions & 1 deletion winbuild/Makefile
Expand Up @@ -130,6 +130,7 @@ TEST_DIR_SRCS=
TEST_DIR_SRCS_CPP=\
tests\art_test.cpp \
tests\bser.cpp \
tests\childproc.cpp \
tests\ignore_test.cpp \
tests\log_stub.cpp \
tests\log.cpp
Expand All @@ -139,7 +140,7 @@ TEST_OBJS=$(TEST_SRCS_CPP:.cpp=.obj) $(TEST_SRCS_C:.c=.obj)
TEST_DIR_OBJS=$(TEST_DIR_SRCS:.c=.obj) $(TEST_DIR_SRCS_CPP:.cpp=.obj)
TESTS=tests\log.exe tests\bser.exe tests\wildmatch_test.exe \
tests\art.exe tests\ignore.exe tests\pending.exe \
tests\string.exe
tests\string.exe tests\childproc.exe

{}.cpp{}.obj::
@echo CXX $<
Expand Down Expand Up @@ -288,6 +289,22 @@ tests\bser.exe: tests\bser.obj bser.obj $(TEST_OBJS) \
@echo LINK $@
$(LINKER) $** $(LIBS)

tests\childproc.exe: tests\childproc.obj $(TEST_OBJS) \
tests\log_stub.obj \
ChildProcess.obj \
winbuild\posix_spawn.obj \
winbuild\pathmap.obj \
Pipe.obj \
FileDescriptor.obj \
Win32Handle.obj \
string.obj \
stream_win.obj \
hash.obj \
PubSub.obj \
log.obj
@echo LINK $@
$(LINKER) $** $(LIBS)

tests\log.exe: tests\log.obj log.obj $(TEST_OBJS) \
PubSub.obj \
string.obj \
Expand Down

0 comments on commit ec480f1

Please sign in to comment.