GeezerVison is showing github.com — test: redirect stderr EnvironmentWithNoESMLoader · nodejs/node@064144d
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: test/cctest/test_environment.cc +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
5 | 5 | #include <string> |
6 | 6 | #include "gtest/gtest.h" |
7 | 7 | #include "node_test_fixture.h" |
| 8 | +#include <stdio.h> |
| 9 | +#include <cstdio> |
8 | 10 |
|
9 | 11 | using node::AtExit; |
10 | 12 | using node::RunAtExit; |
@@ -66,7 +68,34 @@ TEST_F(EnvironmentTest, EnvironmentWithESMLoader) { |
66 | 68 | "})()"); |
67 | 69 | } |
68 | 70 |
|
| 71 | +class RedirectStdErr { |
| 72 | + public: |
| 73 | + explicit RedirectStdErr(const char* filename) : filename_(filename) { |
| 74 | + fflush(stderr); |
| 75 | + fgetpos(stderr, &pos_); |
| 76 | + fd_ = dup(fileno(stderr)); |
| 77 | + freopen(filename_, "w", stderr); |
| 78 | + } |
| 79 | + |
| 80 | + ~RedirectStdErr() { |
| 81 | + fflush(stderr); |
| 82 | + dup2(fd_, fileno(stderr)); |
| 83 | + close(fd_); |
| 84 | + remove(filename_); |
| 85 | + clearerr(stderr); |
| 86 | + fsetpos(stderr, &pos_); |
| 87 | + } |
| 88 | + |
| 89 | + private: |
| 90 | + int fd_; |
| 91 | + fpos_t pos_; |
| 92 | + const char* filename_; |
| 93 | +}; |
| 94 | + |
69 | 95 | TEST_F(EnvironmentTest, EnvironmentWithNoESMLoader) { |
| 96 | + // The following line will cause stderr to get redirected to avoid the |
| 97 | + // error that would otherwise be printed to the console by this test. |
| 98 | + RedirectStdErr redirect_scope("environment_test.log"); |
70 | 99 | const v8::HandleScope handle_scope(isolate_); |
71 | 100 | Argv argv; |
72 | 101 | Env env {handle_scope, argv, node::EnvironmentFlags::kNoRegisterESMLoader}; |
|