Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Src/ConfigurationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ private static void Parse(StringReader reader, Configuration config)
currentSection.Add(setting);
}
}

// If the source contained settings but never opened a named section, the
// default section was never added above (that only happens when the first
// '[' is encountered). Commit it here so its settings aren't silently lost.
if (currentSection.Name == Section.DefaultSectionName && currentSection.SettingCount > 0)
{
config.Add(currentSection);
}
}

private static string? ParseComment(string line, out int commentCharIndex)
Expand Down
28 changes: 28 additions & 0 deletions Tests/SectionlessConfigTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2013-2026 Cem Dervis, MIT License.
// https://sharpconfig.org

using NUnit.Framework;
using Assert = NUnit.Framework.Legacy.ClassicAssert;
using SharpConfig;

namespace Tests
{
[TestFixture]
public sealed class SectionlessConfigTest
{
[Test]
public void SectionlessFileKeepsSettings()
{
var cfg = Configuration.LoadFromString("Setting1 = Value1\nSetting2 = Value2");

Assert.AreEqual(2, cfg.DefaultSection.SettingCount);

Check warning on line 18 in Tests/SectionlessConfigTest.cs

View workflow job for this annotation

GitHub Actions / build

Consider using the constraint model, Assert.That(actual, Is.EqualTo(expected)), instead of the classic model, ClassicAssert.AreEqual(expected, actual) (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2005.md)
Assert.AreEqual("Value1", cfg.DefaultSection["Setting1"].StringValue);

Check warning on line 19 in Tests/SectionlessConfigTest.cs

View workflow job for this annotation

GitHub Actions / build

Consider using the constraint model, Assert.That(actual, Is.EqualTo(expected)), instead of the classic model, ClassicAssert.AreEqual(expected, actual) (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2005.md)
Assert.AreEqual("Value2", cfg.DefaultSection["Setting2"].StringValue);

Check warning on line 20 in Tests/SectionlessConfigTest.cs

View workflow job for this annotation

GitHub Actions / build

Consider using the constraint model, Assert.That(actual, Is.EqualTo(expected)), instead of the classic model, ClassicAssert.AreEqual(expected, actual) (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2005.md)

// Round-trips without gaining a section header.
var saved = cfg.SaveToString();
Assert.IsFalse(saved.Contains('['));
Assert.AreEqual(2, Configuration.LoadFromString(saved).DefaultSection.SettingCount);

Check warning on line 25 in Tests/SectionlessConfigTest.cs

View workflow job for this annotation

GitHub Actions / build

Consider using the constraint model, Assert.That(actual, Is.EqualTo(expected)), instead of the classic model, ClassicAssert.AreEqual(expected, actual) (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2005.md)
}
}
}
Loading