summaryrefslogtreecommitdiff
path: root/base/test_utils.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2018-05-23 09:16:46 -0700
committerElliott Hughes <enh@google.com>2018-05-23 10:06:20 -0700
commit1be0d1481b26281bf699238d5699b38a52b31382 (patch)
treeabe0ba22d28d621564481ef56040e27534605a11 /base/test_utils.cpp
parent9f6f8bf0dcb7f08c8c0afc1685f5d1a54b41b4cb (diff)
Add StdioLogger for command-line tools.
Bug: N/A Test: ran tests Change-Id: If366a4ea25aea1becdd3e443eba225e9bd52ebba
Diffstat (limited to 'base/test_utils.cpp')
-rw-r--r--base/test_utils.cpp37
1 files changed, 23 insertions, 14 deletions
diff --git a/base/test_utils.cpp b/base/test_utils.cpp
index 1619c21347..5096369e7d 100644
--- a/base/test_utils.cpp
+++ b/base/test_utils.cpp
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-#include "android-base/logging.h"
#include "android-base/test_utils.h"
#include <fcntl.h>
@@ -33,6 +32,9 @@
#include <string>
+#include <android-base/file.h>
+#include <android-base/logging.h>
+
#ifdef _WIN32
int mkstemp(char* template_name) {
if (_mktemp(template_name) == nullptr) {
@@ -123,31 +125,38 @@ bool TemporaryDir::init(const std::string& tmp_dir) {
return (mkdtemp(path) != nullptr);
}
-CapturedStderr::CapturedStderr() : old_stderr_(-1) {
- init();
+CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) {
+ Init();
}
-CapturedStderr::~CapturedStderr() {
- reset();
+CapturedStdFd::~CapturedStdFd() {
+ Reset();
}
-int CapturedStderr::fd() const {
+int CapturedStdFd::fd() const {
return temp_file_.fd;
}
-void CapturedStderr::init() {
+std::string CapturedStdFd::str() {
+ std::string result;
+ CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET)));
+ android::base::ReadFdToString(fd(), &result);
+ return result;
+}
+
+void CapturedStdFd::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));
+ if (std_fd_ == STDERR_FILENO) 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));
+ old_fd_ = dup(std_fd_);
+ CHECK_NE(-1, old_fd_);
+ CHECK_NE(-1, dup2(fd(), std_fd_));
}
-void CapturedStderr::reset() {
- CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO));
- CHECK_EQ(0, close(old_stderr_));
+void CapturedStdFd::Reset() {
+ CHECK_NE(-1, dup2(old_fd_, std_fd_));
+ CHECK_EQ(0, close(old_fd_));
// Note: cannot restore prior setvbuf() setting.
}