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
RedYetiDev committed Aug 15, 2024
1 parent 38ad892 commit b7af227
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
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
28 changes: 28 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,32 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

// Ref: https://github.com/nodejs/node/issues/54255
it('should handle when an unrelated argument starts with --env-file', async () => {
const child = await common.spawnPromisified(
process.execPath,
[
'--env-file-XYZ', validEnvFilePath,
],
{ cwd: fixtures.path('dotenv') },
);
assert.strictEqual(child.stdout, '');
assert.match(child.stderr, /bad option: --env-file-XYZ/)

Check failure on line 126 in test/parallel/test-dotenv-edge-cases.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
assert.strictEqual(child.code, 9);
});

// Ref: https://github.com/nodejs/node/pull/54385
it('should handle when --env-file is passed after the script to run', async () => {
const child = await common.spawnPromisified(
process.execPath,
[
fixtures.path('empty.js'), '--env-file=nonexistent.env',
],
{ cwd: fixtures.path('dotenv') },
);
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.stderr, '')

Check failure on line 140 in test/parallel/test-dotenv-edge-cases.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
assert.strictEqual(child.code, 0);
});
});

0 comments on commit b7af227

Please sign in to comment.