Skip to content

Commit bae0194

Browse files
util(parseEnv): add more test cases
1 parent 83f47a6 commit bae0194

2 files changed

Lines changed: 46 additions & 12 deletions

File tree

src/node_dotenv.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,15 @@ void Dotenv::ParseContent(const std::string_view input) {
137137

138138
while (!content.empty()) {
139139
// Skip empty lines and comments
140-
// Example:
141-
// # This is a comment
142140
if (content.front() == '\n' || content.front() == '#') {
143141
// Check if the first character of the content is a newline or a hash
144-
if (content.front() == '\n') {
145-
// If the first character is a newline, remove it
146-
content.remove_prefix(1);
142+
auto newline = content.find('\n');
143+
if (newline != std::string_view::npos) {
144+
// Remove everything up to and including the newline character
145+
content.remove_prefix(newline + 1);
147146
} else {
148-
// If the first character is a hash, find the next newline character
149-
auto newline = content.find('\n');
150-
if (newline != std::string_view::npos) {
151-
// If a newline is found, remove the comment line including the
152-
// newline character.
153-
content.remove_prefix(newline + 1);
154-
}
147+
// If no newline is found, clear the content
148+
content = {};
155149
}
156150

157151
// Skip the remaining code in the loop and continue with the next
@@ -231,6 +225,8 @@ void Dotenv::ParseContent(const std::string_view input) {
231225
if (newline != std::string_view::npos) {
232226
content.remove_prefix(newline + 1);
233227
} else {
228+
// In case the last line is a single key/value pair
229+
// Example: KEY=VALUE (without a newline at the EOF
234230
content = {};
235231
}
236232
continue;

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,42 @@ describe('.env supports edge cases', () => {
220220
ANOTHER_VALID: 'value',
221221
});
222222
});
223+
224+
it('should handle trimming of keys and values correctly', () => {
225+
const result = parseEnv([
226+
' KEY_WITH_SPACES_BEFORE= value_with_spaces_before_and_after ',
227+
'KEY_WITH_TABS_BEFORE\t=\tvalue_with_tabs_before_and_after\t',
228+
'KEY_WITH_SPACES_AND_TABS\t = \t value_with_spaces_and_tabs \t',
229+
' KEY_WITH_SPACES_ONLY =value',
230+
'KEY_WITH_NO_VALUE=',
231+
'KEY_WITH_SPACES_AFTER= value ',
232+
'KEY_WITH_SPACES_AND_COMMENT=value # this is a comment',
233+
'KEY_WITH_ONLY_COMMENT=# this is a comment',
234+
'KEY_WITH_EXPORT=export value',
235+
' export KEY_WITH_EXPORT_AND_SPACES = value ',
236+
].join('\n'));
237+
238+
assert.deepStrictEqual(result, {
239+
KEY_WITH_SPACES_BEFORE: 'value_with_spaces_before_and_after',
240+
KEY_WITH_TABS_BEFORE: 'value_with_tabs_before_and_after',
241+
KEY_WITH_SPACES_AND_TABS: 'value_with_spaces_and_tabs',
242+
KEY_WITH_SPACES_ONLY: 'value',
243+
KEY_WITH_NO_VALUE: '',
244+
KEY_WITH_ONLY_COMMENT: '',
245+
KEY_WITH_SPACES_AFTER: 'value',
246+
KEY_WITH_SPACES_AND_COMMENT: 'value',
247+
KEY_WITH_EXPORT: 'export value',
248+
KEY_WITH_EXPORT_AND_SPACES: 'value',
249+
});
250+
});
251+
252+
it('should handle a comment in a valid value', () => {
253+
const result = parseEnv([
254+
'KEY_WITH_COMMENT_IN_VALUE="value # this is a comment"',
255+
].join('\n'));
256+
257+
assert.deepStrictEqual(result, {
258+
KEY_WITH_COMMENT_IN_VALUE: 'value # this is a comment',
259+
});
260+
});
223261
});

0 commit comments

Comments
 (0)