Skip to content
Permalink
Browse files

test: add variable arguments support for Argv

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 Mar 31, 2017
1 parent 503342d commit 5ccaba49f06a1c38f844430d9d4192b7a3aa67a3
Showing with 21 additions and 11 deletions.
  1. +21 −11 test/cctest/node_test_fixture.h
@@ -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() {

0 comments on commit 5ccaba4

Please sign in to comment.
You can’t perform that action at this time.