Skip to content

Commit ba7e36e

Browse files
committed
src: use for loop in Dotenv::GetPathFromArgs
1 parent 38ad892 commit ba7e36e

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@
421421
'test/cctest/test_traced_value.cc',
422422
'test/cctest/test_util.cc',
423423
'test/cctest/test_dataqueue.cc',
424+
'test/cctest/test_dotenv.cc',
424425
],
425426
'node_cctest_openssl_sources': [
426427
'test/cctest/test_crypto_clienthello.cc',

src/node_dotenv.cc

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,22 @@ using v8::String;
1313

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

22-
while (path != args.end()) {
23-
if (*path == "--") {
24-
return paths;
20+
if (arg == "--" || arg[0] != '-') {
21+
break;
2522
}
26-
auto equal_char = path->find('=');
27-
28-
if (equal_char != std::string::npos) {
29-
paths.push_back(path->substr(equal_char + 1));
30-
} else {
31-
auto next_path = std::next(path);
3223

33-
if (next_path == args.end()) {
34-
return paths;
35-
}
36-
37-
paths.push_back(*next_path);
24+
if (arg.starts_with("--env-file=")) {
25+
paths.push_back(arg.substr(11)); // Directly extract the path
26+
} else if (arg == "--env-file" && i + 1 < args.size()) {
27+
paths.push_back(args[++i]); // Advance to the next argument
28+
} else if (arg[1] != '-') {
29+
++i; // Skip short argument values (like `-e <...>`)
3830
}
39-
40-
path = std::find_if(++path, args.end(), find_match);
4131
}
42-
4332
return paths;
4433
}
4534

test/cctest/test_dotenv.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include "gtest/gtest.h"
4+
#include "node_dotenv.h"
5+
6+
using node::Dotenv;
7+
8+
#define CHECK_DOTENV(args, expected) \
9+
{ \
10+
auto check_result = \
11+
Dotenv::GetPathFromArgs(std::vector<std::string> args); \
12+
ASSERT_EQ(check_result.size(), expected.size()); \
13+
for (size_t i = 0; i < check_result.size(); ++i) { \
14+
EXPECT_EQ(check_result[i], expected[i]); \
15+
}
16+
17+
TEST(Dotenv, GetPathFromArgs) {
18+
CHECK_DOTENV(({"node", "--env-file=.env"}),
19+
(std::vector<std::string>{".env"}));
20+
CHECK_DOTENV(({"node", "--env-file", ".env"}),
21+
(std::vector<std::string>{".env"}));
22+
CHECK_DOTENV(({"node", "--env-file=.env", "--env-file", "other.env"}),
23+
(std::vector<std::string>{".env", "other.env"}));
24+
25+
CHECK_DOTENV(
26+
({"node", "--env-file=before.env", "script.js", "--env-file=after.env"}),
27+
(std::vector<std::string>{"before.env"}));
28+
CHECK_DOTENV(
29+
({"node", "--env-file=before.env", "--", "--env-file=after.env"}),
30+
(std::vector<std::string>{"before.env"}));
31+
32+
CHECK_DOTENV(({"node", "-e", "...", "--env-file=after.env"}),
33+
(std::vector<std::string>{"after.env"}));
34+
}

test/parallel/test-dotenv-edge-cases.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,4 @@ describe('.env supports edge cases', () => {
9898
assert.strictEqual(child.stderr, '');
9999
assert.strictEqual(child.code, 0);
100100
});
101-
102-
it('should handle when --env-file is passed along with --', async () => {
103-
const child = await common.spawnPromisified(
104-
process.execPath,
105-
[
106-
'--eval', `require('assert').strictEqual(process.env.BASIC, undefined);`,
107-
'--', '--env-file', validEnvFilePath,
108-
],
109-
{ cwd: fixtures.path('dotenv') },
110-
);
111-
assert.strictEqual(child.stdout, '');
112-
assert.strictEqual(child.stderr, '');
113-
assert.strictEqual(child.code, 0);
114-
});
115101
});

0 commit comments

Comments
 (0)