From 863bfc44d2456cfc163d6ff3db4100435258c874 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 17 Dec 2020 05:06:20 +0100 Subject: [PATCH] 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 Reviewed-By: Joyee Cheung --- test/cctest/test_environment.cc | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index a2066121cd964f..862dfd5780868a 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -5,6 +5,8 @@ #include #include "gtest/gtest.h" #include "node_test_fixture.h" +#include +#include 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};