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

src: use for loop in Dotenv::GetPathFromArgs #54385

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
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.length() == 2 && arg[1] != '-') {
++i; // Skip short argument values (like `-e <...>`)
RedYetiDev marked this conversation as resolved.
Show resolved Hide resolved
}

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

return paths;
}

Expand Down
41 changes: 41 additions & 0 deletions test/cctest/test_dotenv.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#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"}));

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

CHECK_DOTENV(({"node", "-", "--env-file=.env"}),
(std::vector<std::string>{".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);
});
});
Loading