Skip to content

Commit cb8ecdd

Browse files
authored
Merge pull request #110 from MrDave1999/fix/issue_109
Fixed problem with inline comment being removed when used in multi-line values
2 parents 2380c82 + e8a8fa3 commit cb8ecdd

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/Parser/EnvParser.HelperMethods.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,27 @@ private bool IsComment(string line)
3737
/// Removes the inline comment.
3838
/// </summary>
3939
/// <param name="line">The line with the inline comment to remove.</param>
40+
/// <param name="comment">Contains the removed comment or null if there is no comment.</param>
4041
/// <exception cref="ArgumentNullException"><c>line</c> is <c>null</c>.</exception>
4142
/// <returns>A string without the inline comment.</returns>
42-
private string RemoveInlineComment(string line)
43+
private string RemoveInlineComment(string line, out string comment)
4344
{
4445
_ = line ?? throw new ArgumentNullException(nameof(line));
4546
var separator = $" {_configuration.CommentChar}";
46-
return line.Split(new[] { separator }, MaxCount, StringSplitOptions.None)[0];
47+
var substrings = line.Split(new[] { separator }, MaxCount, StringSplitOptions.None);
48+
comment = substrings.Length == 1 ? null : substrings[1];
49+
return substrings[0];
4750
}
4851

52+
/// <summary>
53+
/// Concatenates the comment with the value.
54+
/// </summary>
55+
/// <param name="value">The value of a key.</param>
56+
/// <param name="comment">The comment to concatenate with the value.</param>
57+
/// <returns>A string with the concatenated comment.</returns>
58+
private string ConcatCommentWithValue(string value, string comment)
59+
=> comment is null ? value : $"{value} {_configuration.CommentChar}{comment}";
60+
4961
/// <summary>
5062
/// Removes all leading and trailing white-space characters from the current key.
5163
/// </summary>
@@ -283,5 +295,13 @@ private string GetValuesMultilines(string[] lines, ref int index, string value)
283295
));
284296
return null;
285297
}
298+
299+
/// <summary>
300+
/// Converts an empty string to a whitespace.
301+
/// </summary>
302+
/// <param name="value">The value to convert.</param>
303+
/// <returns>A string with the converted value.</returns>
304+
private string ConvertStringEmptyToWhitespace(string value)
305+
=> string.IsNullOrEmpty(value) ? " " : value;
286306
}
287307
}

src/Parser/EnvParser.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public IEnvironmentVariablesProvider Parse(string dataSource, out EnvValidationR
7575
if (IsComment(line))
7676
continue;
7777

78-
line = RemoveInlineComment(line);
78+
line = RemoveInlineComment(line, out var removedComment);
7979
if (HasNoKeyValuePair(line))
8080
{
8181
ValidationResult.Add(errorMsg: FormatParserExceptionMessage(
@@ -95,16 +95,17 @@ public IEnvironmentVariablesProvider Parse(string dataSource, out EnvValidationR
9595

9696
key = RemovePrefixBeforeKey(key, ExportPrefix);
9797
key = TrimKey(key);
98-
value = TrimValue(value);
99-
if(IsQuoted(value))
98+
if (IsQuoted(value))
10099
value = RemoveQuotes(value);
101100
else if (IsMultiline(value))
102101
{
103-
value = GetValuesMultilines(lines, ref i, value);
102+
value = GetValuesMultilines(lines, ref i, ConcatCommentWithValue(value, removedComment));
104103
if (value is null)
105104
continue;
106105
}
107-
value = string.IsNullOrEmpty(value) ? " " : value;
106+
else
107+
value = TrimValue(value);
108+
value = ConvertStringEmptyToWhitespace(value);
108109

109110
var retrievedValue = EnvVarsProvider[key];
110111
if (retrievedValue is null)

tests/Parser/.env.multi-lines

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ MULTI_LINE=line
33
#
44
# Double-Quoted
55
#
6-
MULTI_DOUBLE_QUOTED_1="first ${MULTI_LINE} #a
6+
MULTI_DOUBLE_QUOTED_1="first ${MULTI_LINE} #a
77
second ${MULTI_LINE} #b
88
third ${MULTI_LINE} #c
99
four ${MULTI_LINE} #d"
@@ -24,7 +24,7 @@ MULTI_DOUBLE_QUOTED_5="
2424
#
2525
# Single-Quoted
2626
#
27-
MULTI_SINGLE_QUOTED_1='first ${MULTI_LINE} #a
27+
MULTI_SINGLE_QUOTED_1='first ${MULTI_LINE} #a
2828
second ${MULTI_LINE} #b
2929
third ${MULTI_LINE} #c
3030
four ${MULTI_LINE} #d'

tests/Parser/EnvParserTests.Multilines.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void Parse_WhenValuesAreOnMultiLines_ShouldGetValuesSeparatedByNewLine()
1010
parser.Parse(File.ReadAllText(".env.multi-lines"));
1111

1212
Assert.AreEqual(
13-
expected: "first line\nsecond line #b\nthird line #c\nfour line #d",
13+
expected: "first line #a \nsecond line #b\nthird line #c\nfour line #d",
1414
actual: GetEnvironmentVariable("MULTI_DOUBLE_QUOTED_1")
1515
);
1616
Assert.AreEqual(
@@ -21,7 +21,7 @@ public void Parse_WhenValuesAreOnMultiLines_ShouldGetValuesSeparatedByNewLine()
2121
Assert.AreEqual(expected: "\n", actual: GetEnvironmentVariable("MULTI_DOUBLE_QUOTED_4"));
2222
Assert.AreEqual(expected: " \n", actual: GetEnvironmentVariable("MULTI_DOUBLE_QUOTED_5"));
2323
Assert.AreEqual(
24-
expected: "first line\nsecond line #b\nthird line #c\nfour line #d",
24+
expected: "first line #a \nsecond line #b\nthird line #c\nfour line #d",
2525
actual: GetEnvironmentVariable("MULTI_SINGLE_QUOTED_1")
2626
);
2727
Assert.AreEqual(

0 commit comments

Comments
 (0)