Skip to content

Commit 863bfc4

Browse files
danbevdanielleadams
authored andcommittedJan 12, 2021
test: redirect stderr EnvironmentWithNoESMLoader
This commit adds a suggestion to redirect stderr for EnvironmentTest.EnvironmentWithNoESMLoader. The motivation for this is that currently this tests prints the following error (which is expected): vm:module(0):1 globalThis.importResult = import("") ^ Error: Not supported at vm:module(0):1:1 at SourceTextModule.evaluate (node:internal/vm/module:229:23) at node:embedder_main_12:1:328 at processTicksAndRejections (node:internal/process/task_queues:93:5) It might not be obvious which test caused this error just by looking at the output above and it would be nice if it was not displayed. PR-URL: #36548 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent d20235b commit 863bfc4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎test/cctest/test_environment.cc

+29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <string>
66
#include "gtest/gtest.h"
77
#include "node_test_fixture.h"
8+
#include <stdio.h>
9+
#include <cstdio>
810

911
using node::AtExit;
1012
using node::RunAtExit;
@@ -66,7 +68,34 @@ TEST_F(EnvironmentTest, EnvironmentWithESMLoader) {
6668
"})()");
6769
}
6870

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+
6995
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");
7099
const v8::HandleScope handle_scope(isolate_);
71100
Argv argv;
72101
Env env {handle_scope, argv, node::EnvironmentFlags::kNoRegisterESMLoader};

0 commit comments

Comments
 (0)
Please sign in to comment.