Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: redirect stderr EnvironmentWithNoESMLoader #36548

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
danbev marked this conversation as resolved.
Show resolved Hide resolved
const v8::HandleScope handle_scope(isolate_);
Argv argv;
Env env {handle_scope, argv, node::EnvironmentFlags::kNoRegisterESMLoader};
Expand Down