Skip to content

Commit

Permalink
src: return error --env-file if file is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
0xArdi-N committed Nov 7, 2023
1 parent 33704c4 commit b42914a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,10 @@ static ExitCode InitializeNodeWithArgsInternal(

for (const auto& file_path : file_paths) {
std::string path = cwd + kPathSeparator + file_path;
per_process::dotenv_file.ParsePath(path);
auto file_is_exist = per_process::dotenv_file.ParsePath(path);

if (!file_is_exist)
errors->push_back(std::string(file_path) + ": not found");
}

per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options);
Expand Down
9 changes: 3 additions & 6 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ void Dotenv::SetEnvironment(node::Environment* env) {
}
}

void Dotenv::ParsePath(const std::string_view path) {
bool Dotenv::ParsePath(const std::string_view path) {
uv_fs_t req;
auto defer_req_cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });

uv_file file = uv_fs_open(nullptr, &req, path.data(), 0, 438, nullptr);
if (req.result < 0) {
// req will be cleaned up by scope leave.
return;
return false;
}
uv_fs_req_cleanup(&req);

Expand All @@ -87,10 +87,6 @@ void Dotenv::ParsePath(const std::string_view path) {

while (true) {
auto r = uv_fs_read(nullptr, &req, file, &buf, 1, -1, nullptr);
if (req.result < 0) {
// req will be cleaned up by scope leave.
return;
}
uv_fs_req_cleanup(&req);
if (r <= 0) {
break;
Expand All @@ -104,6 +100,7 @@ void Dotenv::ParsePath(const std::string_view path) {
for (const auto& line : lines) {
ParseLine(line);
}
return true;
}

void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
Expand Down
2 changes: 1 addition & 1 deletion src/node_dotenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Dotenv {
Dotenv& operator=(const Dotenv& d) = default;
~Dotenv() = default;

void ParsePath(const std::string_view path);
bool ParsePath(const std::string_view path);
void AssignNodeOptionsIfAvailable(std::string* node_options);
void SetEnvironment(Environment* env);

Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('.env supports edge cases', () => {
[ '--env-file=.env', '--eval', code ],
{ cwd: __dirname },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.stderr.toString().includes('node: .env: not found\n'), true);
assert.strictEqual(child.code, 9);
assert.strictEqual(child.code, 0);

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

View workflow job for this annotation

GitHub Actions / test-macOS

--- stdout --- ::debug::starting to run .env supports edge cases β–Ά .env supports edge cases βœ” supports multiple declarations (64.893774ms) ::debug::starting to run supports multiple declarations ::debug::completed running supports multiple declarations ::debug::starting to run should handle non-existent .env file βœ– should handle non-existent .env file (36.261133ms) AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 9 !== 0 at TestContext.<anonymous> (/Users/runner/work/node/node/test/parallel/test-dotenv-edge-cases.js:39:12) at async Test.run (node:internal/test_runner/test:632:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 9, expected: 0, operator: 'strictEqual' } ::error title=should handle non-existent .env file,file=test/parallel/test-dotenv-edge-cases.js,line=39,col=12::[Error [ERR_TEST_FAILURE]: Expected values to be strictly equal: 9 !== 0 ] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 9 !== 0 at TestContext.<anonymous> (/Users/runner/work/node/node/test/parallel/test-dotenv-edge-cases.js:39:12) at async Test.run (node:internal/test_runner/test:632:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 9, expected: 0, operator: 'strictEqual' } } ::debug::starting to run should not override existing environment variables but introduce new vars βœ” should not override existing environment variables but introduce new vars (68.563792ms) β–Ά .env supports edge cases (177.74106ms) ::debug::completed running should not override existing environment variables but introduce new vars β„Ή tests 3 β„Ή suites 1 β„Ή pass 2 β„Ή fail 1 β„Ή cancelled 0 β„Ή skipped 0 β„Ή todo 0 β„Ή duration_ms 203.742576 βœ– failing tests: test at test/parallel/test-dotenv-edge-cases.js:28:3 βœ– should handle non-existent .env file (36.261133ms) AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 9 !== 0 at TestContext.<anonymous> (/Users/runner/work/node/node/test/parallel/test-dotenv-edge-cases.js:39:12) at async Test.run (node:internal/test_runner/test:632:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 9, expected: 0, operator: 'strictEqual' } ::group::Test results (2 passed, 1 failed) ::notice::Total Tests: 3 Suites πŸ“‚: 1 Passed βœ…: 2 Failed ❌: 1 Canceled 🚫: 0 Skipped ⏭️: 0 Todo πŸ“: 0 Duration πŸ•: 203.743ms ::endgroup:: Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/parallel/test-dotenv-edge-cases.js

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

View workflow job for this annotation

GitHub Actions / test-asan

--- stdout --- ::debug::starting to run .env supports edge cases β–Ά .env supports edge cases βœ” supports multiple declarations (213.608643ms) ::debug::starting to run supports multiple declarations ::debug::completed running supports multiple declarations ::debug::starting to run should handle non-existent .env file βœ– should handle non-existent .env file (68.978811ms) AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 9 !== 0 at TestContext.<anonymous> (/home/runner/work/node/node/test/parallel/test-dotenv-edge-cases.js:39:12) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Test.run (node:internal/test_runner/test:632:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 9, expected: 0, operator: 'strictEqual' } ::error title=should handle non-existent .env file,file=test/parallel/test-dotenv-edge-cases.js,line=39,col=12::[Error [ERR_TEST_FAILURE]: Expected values to be strictly equal: 9 !== 0 ] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 9 !== 0 at TestContext.<anonymous> (/home/runner/work/node/node/test/parallel/test-dotenv-edge-cases.js:39:12) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Test.run (node:internal/test_runner/test:632:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 9, expected: 0, operator: 'strictEqual' } } ::debug::starting to run should not override existing environment variables but introduce new vars βœ” should not override existing environment variables but introduce new vars (245.456204ms) β–Ά .env supports edge cases (550.366407ms) ::debug::completed running should not override existing environment variables but introduce new vars β„Ή tests 3 β„Ή suites 1 β„Ή pass 2 β„Ή fail 1 β„Ή cancelled 0 β„Ή skipped 0 β„Ή todo 0 β„Ή duration_ms 670.148347 βœ– failing tests: test at test/parallel/test-dotenv-edge-cases.js:28:3 βœ– should handle non-existent .env file (68.978811ms) AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 9 !== 0 at TestContext.<anonymous> (/home/runner/work/node/node/test/parallel/test-dotenv-edge-cases.js:39:12) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Test.run (node:internal/test_runner/test:632:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 9, expected: 0, operator: 'strictEqual' } ::group::Test results (2 passed, 1 failed) ::notice::Total Tests: 3 Suites πŸ“‚: 1 Passed βœ…: 2 Failed ❌: 1 Canceled 🚫: 0 Skipped ⏭️: 0 Todo πŸ“: 0 Duration πŸ•: 670.148ms ::endgroup:: Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-dotenv-edge-cases.js
});

Expand Down

0 comments on commit b42914a

Please sign in to comment.