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: https://github.com/nodejs/node/pull/36548
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Daniel Bevenius authored and Danielle Adams committed 2021-01-12 07:07:54 -05:00
1 parent d20235b6cb
commit 863bfc44d2
1 file changed
+29
+29
View File
@@ -5,6 +5,8 @@
#include <string>
#include "gtest/gtest.h"
#include "node_test_fixture.h"
#include <stdio.h>
#include <cstdio>
using node::AtExit;
using node::RunAtExit;
@@ -66,7 +68,34 @@ TEST_F(EnvironmentTest, EnvironmentWithESMLoader) {
"})()");
}
class RedirectStdErr {
public:
explicit RedirectStdErr(const char* filename) : filename_(filename) {
fflush(stderr);
fgetpos(stderr, &pos_);
fd_ = dup(fileno(stderr));
freopen(filename_, "w", stderr);
}
~RedirectStdErr() {
fflush(stderr);
dup2(fd_, fileno(stderr));
close(fd_);
remove(filename_);
clearerr(stderr);
fsetpos(stderr, &pos_);
}
private:
int fd_;
fpos_t pos_;
const char* filename_;
};
TEST_F(EnvironmentTest, EnvironmentWithNoESMLoader) {
// The following line will cause stderr to get redirected to avoid the
// error that would otherwise be printed to the console by this test.
RedirectStdErr redirect_scope("environment_test.log");
const v8::HandleScope handle_scope(isolate_);
Argv argv;
Env env {handle_scope, argv, node::EnvironmentFlags::kNoRegisterESMLoader};