Skip to content

Commit

Permalink
src: handle empty value without newline at EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasShabiCS committed Apr 12, 2024
1 parent a723280 commit c69d5c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ void Dotenv::ParseContent(const std::string_view input) {

// SAFETY: Content is guaranteed to have at least one character
if (content.empty()) {
// In case the last line is a single key without value
// Example: KEY= (without a newline at the EOF)
store_.insert_or_assign(std::string(key), "");
break;
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/dotenv/eof-without-value.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BASIC=value
EMPTY=
17 changes: 17 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,21 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

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

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Trailing spaces not allowed
it('should handle empty value without a newline at the EOF', async () => {
// Ref: https://github.com/nodejs/node/issues/52466
const code = `
process.loadEnvFile('./eof-without-value.env');
require('assert').strictEqual(process.env.BASIC, 'value');
require('assert').strictEqual(process.env.EMPTY, '');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ '--eval', code ],
{ cwd: fixtures.path('dotenv') },
);
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});

0 comments on commit c69d5c3

Please sign in to comment.