Skip to content

Commit

Permalink
test: redirect stderr EnvironmentWithNoESMLoader
Browse files Browse the repository at this point in the history
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 <rtrott@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
danbev authored and danielleadams committed Jan 12, 2021
1 parent d20235b commit 863bfc4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/cctest/test_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
Expand Down

0 comments on commit 863bfc4

Please sign in to comment.