Merge "Move CapturedStderr to test_util library"
This commit is contained in:
commit
0ca2fe4c43
3 changed files with 46 additions and 36 deletions
|
|
@ -48,4 +48,21 @@ class TemporaryDir {
|
||||||
DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
|
DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class CapturedStderr {
|
||||||
|
public:
|
||||||
|
CapturedStderr();
|
||||||
|
~CapturedStderr();
|
||||||
|
|
||||||
|
int fd() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void init();
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
TemporaryFile temp_file_;
|
||||||
|
int old_stderr_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(CapturedStderr);
|
||||||
|
};
|
||||||
|
|
||||||
#endif // ANDROID_BASE_TEST_UTILS_H
|
#endif // ANDROID_BASE_TEST_UTILS_H
|
||||||
|
|
|
||||||
|
|
@ -37,42 +37,6 @@
|
||||||
#define HOST_TEST(suite, name) TEST(suite, name)
|
#define HOST_TEST(suite, name) TEST(suite, name)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class CapturedStderr {
|
|
||||||
public:
|
|
||||||
CapturedStderr() : old_stderr_(-1) {
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
~CapturedStderr() {
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
int fd() const {
|
|
||||||
return temp_file_.fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void init() {
|
|
||||||
#if defined(_WIN32)
|
|
||||||
// On Windows, stderr is often buffered, so make sure it is unbuffered so
|
|
||||||
// that we can immediately read back what was written to stderr.
|
|
||||||
ASSERT_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
|
|
||||||
#endif
|
|
||||||
old_stderr_ = dup(STDERR_FILENO);
|
|
||||||
ASSERT_NE(-1, old_stderr_);
|
|
||||||
ASSERT_NE(-1, dup2(fd(), STDERR_FILENO));
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO));
|
|
||||||
ASSERT_EQ(0, close(old_stderr_));
|
|
||||||
// Note: cannot restore prior setvbuf() setting.
|
|
||||||
}
|
|
||||||
|
|
||||||
TemporaryFile temp_file_;
|
|
||||||
int old_stderr_;
|
|
||||||
};
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
static void ExitSignalAbortHandler(int) {
|
static void ExitSignalAbortHandler(int) {
|
||||||
_exit(3);
|
_exit(3);
|
||||||
|
|
|
||||||
|
|
@ -102,3 +102,32 @@ bool TemporaryDir::init(const std::string& tmp_dir) {
|
||||||
OS_PATH_SEPARATOR);
|
OS_PATH_SEPARATOR);
|
||||||
return (mkdtemp(path) != nullptr);
|
return (mkdtemp(path) != nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CapturedStderr::CapturedStderr() : old_stderr_(-1) {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
CapturedStderr::~CapturedStderr() {
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
int CapturedStderr::fd() const {
|
||||||
|
return temp_file_.fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CapturedStderr::init() {
|
||||||
|
#if defined(_WIN32)
|
||||||
|
// On Windows, stderr is often buffered, so make sure it is unbuffered so
|
||||||
|
// that we can immediately read back what was written to stderr.
|
||||||
|
CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
|
||||||
|
#endif
|
||||||
|
old_stderr_ = dup(STDERR_FILENO);
|
||||||
|
CHECK_NE(-1, old_stderr_);
|
||||||
|
CHECK_NE(-1, dup2(fd(), STDERR_FILENO));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CapturedStderr::reset() {
|
||||||
|
CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO));
|
||||||
|
CHECK_EQ(0, close(old_stderr_));
|
||||||
|
// Note: cannot restore prior setvbuf() setting.
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue