Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ca2fe4c authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Move CapturedStderr to test_util library"

parents 620469a4 8c176302
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -48,4 +48,21 @@ class 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
+0 −36
Original line number Diff line number Diff line
@@ -37,42 +37,6 @@
#define HOST_TEST(suite, name) TEST(suite, name)
#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)
static void ExitSignalAbortHandler(int) {
  _exit(3);
+29 −0
Original line number Diff line number Diff line
@@ -102,3 +102,32 @@ bool TemporaryDir::init(const std::string& tmp_dir) {
           OS_PATH_SEPARATOR);
  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.
}