Skip to content

Commit

Permalink
src: use for loop in Dotenv::GetPathFromArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
avivkeller committed Aug 15, 2024
1 parent 38ad892 commit ba7e36e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@
'test/cctest/test_traced_value.cc',
'test/cctest/test_util.cc',
'test/cctest/test_dataqueue.cc',
'test/cctest/test_dotenv.cc',
],
'node_cctest_openssl_sources': [
'test/cctest/test_crypto_clienthello.cc',
Expand Down
31 changes: 10 additions & 21 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,22 @@ using v8::String;

std::vector<std::string> Dotenv::GetPathFromArgs(
const std::vector<std::string>& args) {
const auto find_match = [](const std::string& arg) {
return arg == "--" || arg == "--env-file" || arg.starts_with("--env-file=");
};
std::vector<std::string> paths;
auto path = std::find_if(args.begin(), args.end(), find_match);
for (size_t i = 1; i < args.size(); ++i) {
const auto& arg = args[i];

while (path != args.end()) {
if (*path == "--") {
return paths;
if (arg == "--" || arg[0] != '-') {
break;
}
auto equal_char = path->find('=');

if (equal_char != std::string::npos) {
paths.push_back(path->substr(equal_char + 1));
} else {
auto next_path = std::next(path);

if (next_path == args.end()) {
return paths;
}

paths.push_back(*next_path);
if (arg.starts_with("--env-file=")) {
paths.push_back(arg.substr(11)); // Directly extract the path
} else if (arg == "--env-file" && i + 1 < args.size()) {
paths.push_back(args[++i]); // Advance to the next argument
} else if (arg[1] != '-') {
++i; // Skip short argument values (like `-e <...>`)
}

path = std::find_if(++path, args.end(), find_match);
}

return paths;
}

Expand Down
34 changes: 34 additions & 0 deletions test/cctest/test_dotenv.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include "gtest/gtest.h"
#include "node_dotenv.h"

using node::Dotenv;

#define CHECK_DOTENV(args, expected) \
{ \
auto check_result = \
Dotenv::GetPathFromArgs(std::vector<std::string> args); \
ASSERT_EQ(check_result.size(), expected.size()); \
for (size_t i = 0; i < check_result.size(); ++i) { \
EXPECT_EQ(check_result[i], expected[i]); \
}

TEST(Dotenv, GetPathFromArgs) {
CHECK_DOTENV(({"node", "--env-file=.env"}),
(std::vector<std::string>{".env"}));
CHECK_DOTENV(({"node", "--env-file", ".env"}),
(std::vector<std::string>{".env"}));
CHECK_DOTENV(({"node", "--env-file=.env", "--env-file", "other.env"}),
(std::vector<std::string>{".env", "other.env"}));

CHECK_DOTENV(
({"node", "--env-file=before.env", "script.js", "--env-file=after.env"}),
(std::vector<std::string>{"before.env"}));
CHECK_DOTENV(
({"node", "--env-file=before.env", "--", "--env-file=after.env"}),
(std::vector<std::string>{"before.env"}));

CHECK_DOTENV(({"node", "-e", "...", "--env-file=after.env"}),
(std::vector<std::string>{"after.env"}));
}
14 changes: 0 additions & 14 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,4 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

it('should handle when --env-file is passed along with --', async () => {
const child = await common.spawnPromisified(
process.execPath,
[
'--eval', `require('assert').strictEqual(process.env.BASIC, undefined);`,
'--', '--env-file', validEnvFilePath,
],
{ cwd: fixtures.path('dotenv') },
);
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});

0 comments on commit ba7e36e

Please sign in to comment.