Skip to content

Commit

Permalink
Remove leading and trailing white-spaces from section name (#63549)
Browse files Browse the repository at this point in the history
* Reproduce the issue with spaces around the section name

* Remove leading and trailing whitespaces from section name
  • Loading branch information
mapogolions authored Jan 11, 2022
1 parent 1ba2734 commit fe8422c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public static IDictionary<string, string> Read(Stream stream)
{
// remove the brackets
#if NET
sectionPrefix = string.Concat(line.AsSpan(1, line.Length - 2), ConfigurationPath.KeyDelimiter);
sectionPrefix = string.Concat(line.AsSpan(1, line.Length - 2).Trim(), ConfigurationPath.KeyDelimiter);
#else
sectionPrefix = line.Substring(1, line.Length - 2) + ConfigurationPath.KeyDelimiter;
sectionPrefix = line.Substring(1, line.Length - 2).Trim() + ConfigurationPath.KeyDelimiter;
#endif
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ public void SupportAndIgnoreComments()
Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider"));
}

[Fact]
public void ShouldRemoveLeadingAndTrailingWhiteSpacesFromKeyAndValue()
{
var ini = "[section]\n" +
" \t key \t = \t value\t ";
var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource());
iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

Assert.Equal("value", iniConfigSrc.Get("section:key"));
}

[Fact]
public void ShouldRemoveLeadingAndTrailingWhiteSpacesFromSectionName()
{
var ini = "[ \t section \t ]\n" +
"key=value";
var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource());
iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

Assert.Equal("value", iniConfigSrc.Get("section:key"));
}

[Fact]
public void ThrowExceptionWhenFoundInvalidLine()
{
Expand Down

0 comments on commit fe8422c

Please sign in to comment.