Skip to content

Commit

Permalink
test: add variable arguments support for Argv
Browse files Browse the repository at this point in the history
At the moment Argv only supports three arguments which fulfilled my
requirements when working on #9163.

This commit adds support for a variable number of arguments. There is
also a no-args constructor that is the equivalent to running "node -p
process.version" which is hopefully alright as a default.

PR-URL: #12166
Ref: #9163
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
danbev authored and jasnell committed Apr 4, 2017
1 parent 503342d commit 5ccaba4
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions test/cctest/node_test_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,27 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {

struct Argv {
public:
Argv(const char* prog, const char* arg1, const char* arg2) {
int prog_len = strlen(prog) + 1;
int arg1_len = strlen(arg1) + 1;
int arg2_len = strlen(arg2) + 1;
argv_ = static_cast<char**>(malloc(3 * sizeof(char*)));
argv_[0] = static_cast<char*>(malloc(prog_len + arg1_len + arg2_len));
snprintf(argv_[0], prog_len, "%s", prog);
snprintf(argv_[0] + prog_len, arg1_len, "%s", arg1);
snprintf(argv_[0] + prog_len + arg1_len, arg2_len, "%s", arg2);
argv_[1] = argv_[0] + prog_len;
argv_[2] = argv_[0] + prog_len + arg1_len;
Argv() : Argv({"node", "-p", "process.version"}) {}

Argv(const std::initializer_list<const char*> &args) {
int nrArgs = args.size();
int totalLen = 0;
for (auto it = args.begin(); it != args.end(); ++it) {
totalLen += strlen(*it) + 1;
}
argv_ = static_cast<char**>(malloc(nrArgs * sizeof(char*)));
argv_[0] = static_cast<char*>(malloc(totalLen));
int i = 0;
int offset = 0;
for (auto it = args.begin(); it != args.end(); ++it, ++i) {
int len = strlen(*it) + 1;
snprintf(argv_[0] + offset, len, "%s", *it);
// Skip argv_[0] as it points the correct location already
if (i > 0) {
argv_[i] = argv_[0] + offset;
}
offset += len;
}
}

~Argv() {
Expand Down

0 comments on commit 5ccaba4

Please sign in to comment.