From f6459893c72c516fd1bf4c23638914db0a613616 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 15 Aug 2021 01:38:57 +0300 Subject: [PATCH 1/6] Annotate FunctionalTests --- .../FunctionalTests/ConfigurationTests.cs | 52 +++++++++---------- .../FunctionalTests/DisposableFileSystem.cs | 9 ++-- ...ions.Configuration.Functional.Tests.csproj | 1 + 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs index 50ffa927af1177..6a0c3b8d8109ed 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs @@ -68,7 +68,7 @@ public class ConfigurationTests : IDisposable } } }"; - private static readonly Dictionary _memConfigContent = new Dictionary + private static readonly Dictionary _memConfigContent = new Dictionary { { "MemKey1", "MemValue1" }, { "MemKey2:MemKey3", "MemValue2" }, @@ -100,7 +100,7 @@ public void ThrowsOnFileNotFoundWhenNotIgnored() Assert.Throws(() => configurationBuilder.Build()); } - + [Fact] public void CanHandleExceptionIfFileNotFound() { @@ -137,9 +137,9 @@ public void MissingFileIncludesAbsolutePathIfPhysicalFileProvider() private class NotVeryGoodFileProvider : IFileProvider { - public IDirectoryContents GetDirectoryContents(string subpath) => null; - public IFileInfo GetFileInfo(string subpath) => null; - public IChangeToken Watch(string filter) => null; + public IDirectoryContents GetDirectoryContents(string? subpath) => null!; + public IFileInfo GetFileInfo(string subpath) => null!; + public IChangeToken Watch(string? filter) => null!; } private class MissingFile : IFileInfo @@ -155,9 +155,9 @@ private class MissingFile : IFileInfo private class AlwaysMissingFileProvider : IFileProvider { - public IDirectoryContents GetDirectoryContents(string subpath) => null; - public IFileInfo GetFileInfo(string subpath) => null; - public IChangeToken Watch(string filter) => null; + public IDirectoryContents GetDirectoryContents(string? subpath) => null!; + public IFileInfo GetFileInfo(string subpath) => null!; + public IChangeToken Watch(string? filter) => null!; } private void WriteTestFiles() @@ -366,8 +366,8 @@ public void OnLoadErrorWillBeCalledOnJsonParseError() { _fileSystem.WriteFile(Path.Combine(_basePath, "error.json"), @"{""JsonKey1"": ", absolute: true); - FileConfigurationProvider provider = null; - Exception jsonError = null; + FileConfigurationProvider? provider = null; + Exception? jsonError = null; Action jsonLoadError = c => { jsonError = c.Exception; @@ -395,8 +395,8 @@ public void OnLoadErrorWillBeCalledOnXmlParseError() { _fileSystem.WriteFile("error.xml", @"gobblygook"); - FileConfigurationProvider provider = null; - Exception error = null; + FileConfigurationProvider? provider = null; + Exception? error = null; Action loadError = c => { error = c.Exception; @@ -425,7 +425,7 @@ public void OnLoadErrorWillBeCalledOnIniLoadError() IniKey1=IniValue2"); FileConfigurationProvider provider = null; - Exception error = null; + Exception? error = null; Action loadError = c => { error = c.Exception; @@ -695,7 +695,7 @@ await WaitForChange( Assert.Equal("IniValue1", config["Key"]); Assert.True(token.HasChanged); } - + [Theory] [ActiveIssue("File watching is flaky (particularly on non windows. https://github.com/dotnet/runtime/issues/33992")] [InlineData(false)] @@ -925,7 +925,7 @@ public async Task TouchingFileWillReloadForUserSecrets() { string userSecretsId = "Test"; var userSecretsPath = PathHelper.GetSecretsPathFromSecretsId(userSecretsId); - var userSecretsFolder = Path.GetDirectoryName(userSecretsPath); + var userSecretsFolder = Path.GetDirectoryName(userSecretsPath)!; _fileSystem.CreateFolder(userSecretsFolder); _fileSystem.WriteFile(userSecretsPath, @"{""UserSecretKey1"": ""UserSecretValue1""}"); @@ -977,18 +977,18 @@ void ReloadLoop() _ = Task.Run(ReloadLoop); - MyOptions options = null; + MyOptions? options = null; while (!cts.IsCancellationRequested) { options = config.Get(); } - Assert.Equal("CmdValue1", options.CmdKey1); - Assert.Equal("IniValue1", options.IniKey1); - Assert.Equal("JsonValue1", options.JsonKey1); - Assert.Equal("MemValue1", options.MemKey1); - Assert.Equal("XmlValue1", options.XmlKey1); + Assert.Equal("CmdValue1", options?.CmdKey1); + Assert.Equal("IniValue1", options?.IniKey1); + Assert.Equal("JsonValue1", options?.JsonKey1); + Assert.Equal("MemValue1", options?.MemKey1); + Assert.Equal("XmlValue1", options?.XmlKey1); } } @@ -1017,15 +1017,15 @@ private async Task WaitForChange( private sealed class MyOptions { - public string CmdKey1 { get; set; } + public string? CmdKey1 { get; set; } - public string IniKey1 { get; set; } + public string? IniKey1 { get; set; } - public string JsonKey1 { get; set; } + public string? JsonKey1 { get; set; } - public string MemKey1 { get; set; } + public string? MemKey1 { get; set; } - public string XmlKey1 { get; set; } + public string? XmlKey1 { get; set; } } } } diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/DisposableFileSystem.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/DisposableFileSystem.cs index 73f131afb3fa32..77abcbff036f56 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/DisposableFileSystem.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/DisposableFileSystem.cs @@ -73,7 +73,10 @@ public DisposableFileSystem CreateFiles(params string[] fileRelativePaths) { var fullPath = Path.Combine(RootPath, path); var dirName = Path.GetDirectoryName(fullPath); - Directory.CreateDirectory(dirName); + if (dirName != null) + { + Directory.CreateDirectory(dirName); + } WaitForFileSystem( () => Directory.Exists(dirName), @@ -102,9 +105,9 @@ public void Dispose() private void WaitForFileSystem( Func test, string failureMessage, - Action retry = null) + Action? retry = null) { - Exception failure = null; + Exception? failure = null; Func nonThrowingTest = () => { diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/Microsoft.Extensions.Configuration.Functional.Tests.csproj b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/Microsoft.Extensions.Configuration.Functional.Tests.csproj index e28690dc03d718..7831c96dd7013a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/Microsoft.Extensions.Configuration.Functional.Tests.csproj +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/Microsoft.Extensions.Configuration.Functional.Tests.csproj @@ -3,6 +3,7 @@ $(NetCoreAppCurrent);net461 true + enable From 8c97f908f6569c3e8499bddb5e45ae134c4bfa76 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 15 Aug 2021 01:42:06 +0300 Subject: [PATCH 2/6] Annotate tests --- .../tests/Common/TestStreamHelpers.cs | 64 +++--------- .../tests/ConfigurationManagerTest.cs | 99 ++++++++++--------- .../tests/ConfigurationPathComparerTest.cs | 2 +- .../tests/ConfigurationProviderTestBase.cs | 67 ++++++------- .../tests/ConfigurationTest.cs | 87 ++++++++-------- ...soft.Extensions.Configuration.Tests.csproj | 1 + 6 files changed, 144 insertions(+), 176 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/Common/TestStreamHelpers.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/Common/TestStreamHelpers.cs index facb079982a843..d71e9bc7e89e10 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/Common/TestStreamHelpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/Common/TestStreamHelpers.cs @@ -12,7 +12,7 @@ public static class TestStreamHelpers { public static readonly string ArbitraryFilePath = "Unit tests do not touch file system"; - public static IFileProvider StringToFileProvider(string str) + public static IFileProvider StringToFileProvider(string? str) { return new TestFileProvider(str); @@ -20,60 +20,24 @@ public static IFileProvider StringToFileProvider(string str) private class TestFile : IFileInfo { - private readonly string _data; + private readonly string? _data; - public TestFile(string str) + public TestFile(string? str) { _data = str; } - public bool Exists - { - get - { - return true; - } - } + public bool Exists => true; - public bool IsDirectory - { - get - { - return false; - } - } + public bool IsDirectory => false; - public DateTimeOffset LastModified - { - get - { - throw new NotImplementedException(); - } - } + public DateTimeOffset LastModified => throw new NotImplementedException(); - public long Length - { - get - { - return 0; - } - } + public long Length => 0; - public string Name - { - get - { - return null; - } - } + public string Name => string.Empty; - public string PhysicalPath - { - get - { - return null; - } - } + public string? PhysicalPath => null; public Stream CreateReadStream() { @@ -83,13 +47,13 @@ public Stream CreateReadStream() private class TestFileProvider : IFileProvider { - private string _data; - public TestFileProvider(string str) + private string? _data; + public TestFileProvider(string? str) { _data = str; } - public IDirectoryContents GetDirectoryContents(string subpath) + public IDirectoryContents GetDirectoryContents(string? subpath) { throw new NotImplementedException(); } @@ -99,13 +63,13 @@ public IFileInfo GetFileInfo(string subpath) return new TestFile(_data); } - public IChangeToken Watch(string filter) + public IChangeToken Watch(string? filter) { throw new NotImplementedException(); } } - public static Stream StringToStream(string str) + public static Stream StringToStream(string? str) { var memStream = new MemoryStream(); var textWriter = new StreamWriter(memStream); diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationManagerTest.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationManagerTest.cs index 89da7883517544..ae642885c6ea1c 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationManagerTest.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationManagerTest.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.Extensions.Configuration.Memory; using Microsoft.Extensions.Primitives; @@ -18,7 +19,7 @@ public void AutoUpdates() { var config = new ConfigurationManager(); - config.AddInMemoryCollection(new Dictionary + config.AddInMemoryCollection(new Dictionary { { "TestKey", "TestValue" }, }); @@ -35,7 +36,7 @@ public void TriggersReloadTokenOnSourceAddition() Assert.False(reloadToken.HasChanged); - config.AddInMemoryCollection(new Dictionary + config.AddInMemoryCollection(new Dictionary { { "TestKey", "TestValue" }, }); @@ -235,15 +236,15 @@ public void ChainedConfigurationIsDisposedOnDispose(bool shouldDispose) public void LoadAndCombineKeyValuePairsFromDifferentConfigurationProviders() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1:KeyInMem1", "ValueInMem1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2:KeyInMem2", "ValueInMem2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3:KeyInMem3", "ValueInMem3"} }; @@ -282,15 +283,15 @@ public void LoadAndCombineKeyValuePairsFromDifferentConfigurationProviders() public void CanChainConfiguration() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1:KeyInMem1", "ValueInMem1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2:KeyInMem2", "ValueInMem2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3:KeyInMem3", "ValueInMem3"} }; @@ -327,21 +328,21 @@ public void CanChainConfiguration() public void ChainedAsEnumerateFlattensIntoDictionaryTest(bool removePath) { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:", "NoKeyValue1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, {"Mem1:KeyInMem1:Deep1", "ValueDeep1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2", "Value2"}, {"Mem2:", "NoKeyValue2"}, {"Mem2:KeyInMem2", "ValueInMem2"}, {"Mem2:KeyInMem2:Deep2", "ValueDeep2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3", "Value3"}, {"Mem3:", "NoKeyValue3"}, @@ -387,21 +388,21 @@ public void ChainedAsEnumerateFlattensIntoDictionaryTest(bool removePath) public void AsEnumerateFlattensIntoDictionaryTest(bool removePath) { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:", "NoKeyValue1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, {"Mem1:KeyInMem1:Deep1", "ValueDeep1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2", "Value2"}, {"Mem2:", "NoKeyValue2"}, {"Mem2:KeyInMem2", "ValueInMem2"}, {"Mem2:KeyInMem2:Deep2", "ValueDeep2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3", "Value3"}, {"Mem3:", "NoKeyValue3"}, @@ -439,21 +440,21 @@ public void AsEnumerateFlattensIntoDictionaryTest(bool removePath) public void AsEnumerateStripsKeyFromChildren() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:", "NoKeyValue1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, {"Mem1:KeyInMem1:Deep1", "ValueDeep1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2", "Value2"}, {"Mem2:", "NoKeyValue2"}, {"Mem2:KeyInMem2", "ValueInMem2"}, {"Mem2:KeyInMem2:Deep2", "ValueDeep2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3", "Value3"}, {"Mem3:", "NoKeyValue3"}, @@ -499,11 +500,11 @@ public void AsEnumerateStripsKeyFromChildren() public void NewConfigurationProviderOverridesOldOneWhenKeyIsDuplicated() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Key1:Key2", "ValueInMem1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Key1:Key2", "ValueInMem2"} }; @@ -526,11 +527,11 @@ public void NewConfigurationRootMayBeBuiltFromExistingWithDuplicateKeys() { var configurationRoot = new ConfigurationManager(); - configurationRoot.AddInMemoryCollection(new Dictionary + configurationRoot.AddInMemoryCollection(new Dictionary { {"keya:keyb", "valueA"}, }); - configurationRoot.AddInMemoryCollection(new Dictionary + configurationRoot.AddInMemoryCollection(new Dictionary { {"KEYA:KEYB", "valueB"}, }); @@ -546,7 +547,7 @@ public void NewConfigurationRootMayBeBuiltFromExistingWithDuplicateKeys() public void SettingValueUpdatesAllConfigurationProviders() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Key1", "Value1"}, {"Key2", "Value2"} @@ -586,16 +587,16 @@ public void SettingValueUpdatesAllConfigurationProviders() public void CanGetConfigurationSection() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Data:DB1:Connection1", "MemVal1"}, {"Data:DB1:Connection2", "MemVal2"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"DataSource:DB2:Connection", "MemVal3"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Data", "MemVal4"} }; @@ -635,12 +636,12 @@ public void CanGetConfigurationSection() public void CanGetConnectionStrings() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"ConnectionStrings:DB1:Connection1", "MemVal1"}, {"ConnectionStrings:DB1:Connection2", "MemVal2"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"ConnectionStrings:DB2:Connection", "MemVal3"} }; @@ -668,16 +669,16 @@ public void CanGetConnectionStrings() public void CanGetConfigurationChildren() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Data:DB1:Connection1", "MemVal1"}, {"Data:DB1:Connection2", "MemVal2"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Data:DB2Connection", "MemVal3"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"DataSource:DB3:Connection", "MemVal4"} }; @@ -708,7 +709,7 @@ public void CanGetConfigurationChildren() public void SourcesReturnsAddedConfigurationProviders() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem:KeyInMem", "MemVal"} }; @@ -857,7 +858,7 @@ public void NewTokenAfterReloadIsNotChanged() public void KeyStartingWithColonMeansFirstSectionHasEmptyName() { // Arrange - var dict = new Dictionary + var dict = new Dictionary { [":Key2"] = "value" }; @@ -878,7 +879,7 @@ public void KeyStartingWithColonMeansFirstSectionHasEmptyName() public void KeyWithDoubleColonHasSectionWithEmptyName() { // Arrange - var dict = new Dictionary + var dict = new Dictionary { ["Key1::Key3"] = "value" }; @@ -902,7 +903,7 @@ public void KeyWithDoubleColonHasSectionWithEmptyName() public void KeyEndingWithColonMeansLastSectionHasEmptyName() { // Arrange - var dict = new Dictionary + var dict = new Dictionary { ["Key1:"] = "value" }; @@ -924,7 +925,7 @@ public void KeyEndingWithColonMeansLastSectionHasEmptyName() public void SectionWithValueExists() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, @@ -949,7 +950,7 @@ public void SectionWithValueExists() public void SectionGetRequiredSectionSuccess() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, @@ -972,7 +973,7 @@ public void SectionGetRequiredSectionSuccess() public void SectionGetRequiredSectionMissingThrowException() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:Deep1", "Value1"}, @@ -989,7 +990,7 @@ public void SectionGetRequiredSectionMissingThrowException() public void SectionWithChildrenExists() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1:KeyInMem1", "ValueInMem1"}, {"Mem1:KeyInMem1:Deep1", "ValueDeep1"}, @@ -1016,7 +1017,7 @@ public void SectionWithChildrenExists() public void KeyWithValueAndWithoutChildrenExistsAsSection(string value) { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", value} }; @@ -1035,7 +1036,7 @@ public void KeyWithValueAndWithoutChildrenExistsAsSection(string value) public void KeyWithNullValueAndWithoutChildrenIsASectionButNotExists() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", null} }; @@ -1058,7 +1059,7 @@ public void KeyWithNullValueAndWithoutChildrenIsASectionButNotExists() public void SectionWithChildrenHasNullValue() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1:KeyInMem1", "ValueInMem1"}, }; @@ -1141,13 +1142,13 @@ public void Dispose() private class TestChangeToken : IChangeToken { - public List<(Action, object)> Callbacks { get; } = new List<(Action, object)>(); + public List<(Action, object?)> Callbacks { get; } = new List<(Action, object?)>(); public bool HasChanged => false; public bool ActiveChangeCallbacks => true; - public IDisposable RegisterChangeCallback(Action callback, object state) + public IDisposable RegisterChangeCallback(Action callback, object? state) { var item = (callback, state); Callbacks.Add(item); @@ -1156,9 +1157,9 @@ public IDisposable RegisterChangeCallback(Action callback, object state) private class DisposableAction : IDisposable { - private Action _action; + private Action? _action; - public DisposableAction(Action action) + public DisposableAction(Action? action) { _action = action; } @@ -1177,7 +1178,7 @@ public void Dispose() private class TestMemorySourceProvider : MemoryConfigurationProvider, IConfigurationSource { - public TestMemorySourceProvider(Dictionary initialData) + public TestMemorySourceProvider(Dictionary initialData) : base(new MemoryConfigurationSource { InitialData = initialData }) { } @@ -1189,11 +1190,11 @@ public IConfigurationProvider Build(IConfigurationBuilder builder) private class NullReloadTokenConfigSource : IConfigurationSource, IConfigurationProvider { - public IEnumerable GetChildKeys(IEnumerable earlierKeys, string parentPath) => throw new NotImplementedException(); + public IEnumerable GetChildKeys(IEnumerable earlierKeys, string? parentPath) => throw new NotImplementedException(); public IChangeToken GetReloadToken() => null; public void Load() { } - public void Set(string key, string value) => throw new NotImplementedException(); - public bool TryGet(string key, out string value) => throw new NotImplementedException(); + public void Set(string key, string? value) => throw new NotImplementedException(); + public bool TryGet(string key, [MaybeNullWhen(false)] out string value) => throw new NotImplementedException(); public IConfigurationProvider Build(IConfigurationBuilder builder) => this; } diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationPathComparerTest.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationPathComparerTest.cs index 159ba1d4a75cdf..81e2ca856bee08 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationPathComparerTest.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationPathComparerTest.cs @@ -127,7 +127,7 @@ public void ComparePathsWithIndicesInside() ComparerTest("abc:def:22:jkl", "abc:def:10:jkl", 1); } - private static void ComparerTest(string a, string b, int expectedSign) + private static void ComparerTest(string? a, string? b, int expectedSign) { var result = ConfigurationKeyComparer.Instance.Compare(a, b); Assert.Equal(expectedSign, Math.Sign(result)); diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationProviderTestBase.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationProviderTestBase.cs index c684a89f645bc1..ece09b1e392d83 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationProviderTestBase.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationProviderTestBase.cs @@ -111,7 +111,7 @@ public virtual void Load_from_single_provider_with_differing_case_duplicates_thr private void AssertFormatOrArgumentException(Action test) { - Exception caught = null; + Exception? caught = null; try { test(); @@ -133,41 +133,41 @@ public virtual void Bind_to_object() var options = configuration.Get(); Assert.Equal("Value1", options.Key1); - Assert.Equal("Value12", options.Section1.Key2); - Assert.Equal("Value123", options.Section1.Section2.Key3); - Assert.Equal("Value344", options.Section3.Section4.Key4); - Assert.Equal(new[] { "ArrayValue0", "ArrayValue1", "ArrayValue2" }, options.Section1.Section2.Key3a); + Assert.Equal("Value12", options.Section1?.Key2); + Assert.Equal("Value123", options.Section1?.Section2?.Key3); + Assert.Equal("Value344", options.Section3?.Section4?.Key4); + Assert.Equal(new[] { "ArrayValue0", "ArrayValue1", "ArrayValue2" }, options.Section1?.Section2?.Key3a); } public class AsOptions { - public string Key1 { get; set; } + public string? Key1 { get; set; } - public Section1AsOptions Section1 { get; set; } - public Section3AsOptions Section3 { get; set; } + public Section1AsOptions? Section1 { get; set; } + public Section3AsOptions? Section3 { get; set; } } public class Section1AsOptions { - public string Key2 { get; set; } + public string? Key2 { get; set; } - public Section2AsOptions Section2 { get; set; } + public Section2AsOptions? Section2 { get; set; } } public class Section2AsOptions { - public string Key3 { get; set; } - public string[] Key3a { get; set; } + public string? Key3 { get; set; } + public string[]? Key3a { get; set; } } public class Section3AsOptions { - public Section4AsOptions Section4 { get; set; } + public Section4AsOptions? Section4 { get; set; } } public class Section4AsOptions { - public string Key4 { get; set; } + public string? Key4 { get; set; } } protected virtual void AssertDebugView( @@ -186,7 +186,7 @@ protected virtual void AssertDebugView( protected virtual void AssertConfig( IConfigurationRoot config, bool expectNulls = false, - string nullValue = null) + string? nullValue = null) { var value1 = expectNulls ? nullValue : "Value1"; var value12 = expectNulls ? nullValue : "Value12"; @@ -239,7 +239,7 @@ protected virtual void AssertConfig( var section3 = config.GetSection("Section3"); Assert.Equal("Section3", section3.Path, StringComparer.InvariantCultureIgnoreCase); Assert.Null(section3.Value); - + var section4 = config.GetSection("Section3:Section4"); Assert.Equal(value344, section4["Key4"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section3:Section4", section4.Path, StringComparer.InvariantCultureIgnoreCase); @@ -340,7 +340,7 @@ protected virtual IConfigurationRoot BuildConfigRoot( protected static (IConfigurationProvider Provider, Action Initializer) LoadUsingMemoryProvider(TestSection testConfig) { - var values = new List>(); + var values = new List>(); SectionToValues(testConfig, "", values); return (new MemoryConfigurationProvider( @@ -348,17 +348,18 @@ protected static (IConfigurationProvider Provider, Action Initializer) LoadUsing { InitialData = values }), - () => { }); + () => { } + ); } protected static void SectionToValues( TestSection section, string sectionName, - IList> values) + IList> values) { foreach (var tuple in section.Values.SelectMany(e => e.Value.Expand(e.Key))) { - values.Add(new KeyValuePair(sectionName + tuple.Key, tuple.Value)); + values.Add(new KeyValuePair(sectionName + tuple.Key, tuple.Value)); } foreach (var tuple in section.Sections) @@ -372,26 +373,26 @@ protected static void SectionToValues( protected class TestKeyValue { - public object Value { get; } + public object? Value { get; } - public TestKeyValue(string value) + public TestKeyValue(string? value) { Value = value; } - public TestKeyValue(string[] values) + public TestKeyValue(string?[]? values) { Value = values; } - public static implicit operator TestKeyValue(string value) => new TestKeyValue(value); - public static implicit operator TestKeyValue(string[] values) => new TestKeyValue(values); + public static implicit operator TestKeyValue(string? value) => new TestKeyValue(value); + public static implicit operator TestKeyValue(string?[]? values) => new TestKeyValue(values); - public string[] AsArray => Value as string[]; + public string[]? AsArray => Value as string[]; - public string AsString => Value as string; + public string? AsString => Value as string; - public IEnumerable<(string Key, string Value)> Expand(string key) + public IEnumerable<(string Key, string? Value)> Expand(string key) { if (AsArray == null) { @@ -673,20 +674,20 @@ protected class TestSection public static TestSection NullsTestConfig { get; } = new TestSection { - Values = new[] { ("Key1", new TestKeyValue((string)null)) }, + Values = new[] { ("Key1", new TestKeyValue((string?)null)) }, Sections = new[] { ("Section1", new TestSection { - Values = new[] {("Key2", new TestKeyValue((string)null))}, + Values = new[] {("Key2", new TestKeyValue((string?)null))}, Sections = new[] { ("Section2", new TestSection { Values = new[] { - ("Key3", new TestKeyValue((string)null)), - ("Key3a", (TestKeyValue)new string[] {null, null, null}) + ("Key3", new TestKeyValue((string?)null)), + ("Key3a", (TestKeyValue)new string?[] {null, null, null}) }, }) } @@ -697,7 +698,7 @@ protected class TestSection { ("Section4", new TestSection { - Values = new[] {("Key4", new TestKeyValue((string)null))} + Values = new[] {("Key4", new TestKeyValue((string?)null))} }) } }) diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationTest.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationTest.cs index 70a1abf218a10d..f740aa13c52740 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationTest.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationTest.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.Extensions.Configuration.Memory; using Xunit; @@ -15,15 +16,15 @@ public class ConfigurationTest public void LoadAndCombineKeyValuePairsFromDifferentConfigurationProviders() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1:KeyInMem1", "ValueInMem1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2:KeyInMem2", "ValueInMem2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3:KeyInMem3", "ValueInMem3"} }; @@ -63,15 +64,15 @@ public void LoadAndCombineKeyValuePairsFromDifferentConfigurationProviders() public void CanChainConfiguration() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1:KeyInMem1", "ValueInMem1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2:KeyInMem2", "ValueInMem2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3:KeyInMem3", "ValueInMem3"} }; @@ -108,21 +109,21 @@ public void CanChainConfiguration() public void ChainedAsEnumerateFlattensIntoDictionaryTest(bool removePath) { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:", "NoKeyValue1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, {"Mem1:KeyInMem1:Deep1", "ValueDeep1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2", "Value2"}, {"Mem2:", "NoKeyValue2"}, {"Mem2:KeyInMem2", "ValueInMem2"}, {"Mem2:KeyInMem2:Deep2", "ValueDeep2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3", "Value3"}, {"Mem3:", "NoKeyValue3"}, @@ -164,21 +165,21 @@ public void ChainedAsEnumerateFlattensIntoDictionaryTest(bool removePath) public void AsEnumerateFlattensIntoDictionaryTest(bool removePath) { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:", "NoKeyValue1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, {"Mem1:KeyInMem1:Deep1", "ValueDeep1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2", "Value2"}, {"Mem2:", "NoKeyValue2"}, {"Mem2:KeyInMem2", "ValueInMem2"}, {"Mem2:KeyInMem2:Deep2", "ValueDeep2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3", "Value3"}, {"Mem3:", "NoKeyValue3"}, @@ -216,21 +217,21 @@ public void AsEnumerateFlattensIntoDictionaryTest(bool removePath) public void AsEnumerateStripsKeyFromChildren() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:", "NoKeyValue1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, {"Mem1:KeyInMem1:Deep1", "ValueDeep1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Mem2", "Value2"}, {"Mem2:", "NoKeyValue2"}, {"Mem2:KeyInMem2", "ValueInMem2"}, {"Mem2:KeyInMem2:Deep2", "ValueDeep2"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Mem3", "Value3"}, {"Mem3:", "NoKeyValue3"}, @@ -278,11 +279,11 @@ public void AsEnumerateStripsKeyFromChildren() public void NewConfigurationProviderOverridesOldOneWhenKeyIsDuplicated() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Key1:Key2", "ValueInMem1"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Key1:Key2", "ValueInMem2"} }; @@ -305,11 +306,11 @@ public void NewConfigurationProviderOverridesOldOneWhenKeyIsDuplicated() public void NewConfigurationRootMayBeBuiltFromExistingWithDuplicateKeys() { var configurationRoot = new ConfigurationBuilder() - .AddInMemoryCollection(new Dictionary + .AddInMemoryCollection(new Dictionary { {"keya:keyb", "valueA"}, }) - .AddInMemoryCollection(new Dictionary + .AddInMemoryCollection(new Dictionary { {"KEYA:KEYB", "valueB"} }) @@ -322,7 +323,7 @@ public void NewConfigurationRootMayBeBuiltFromExistingWithDuplicateKeys() public class TestMemorySourceProvider : MemoryConfigurationProvider, IConfigurationSource { - public TestMemorySourceProvider(Dictionary initialData) + public TestMemorySourceProvider(Dictionary initialData) : base(new MemoryConfigurationSource { InitialData = initialData }) { } @@ -336,7 +337,7 @@ public IConfigurationProvider Build(IConfigurationBuilder builder) public void SettingValueUpdatesAllConfigurationProviders() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Key1", "Value1"}, {"Key2", "Value2"} @@ -377,16 +378,16 @@ public void SettingValueUpdatesAllConfigurationProviders() public void CanGetConfigurationSection() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Data:DB1:Connection1", "MemVal1"}, {"Data:DB1:Connection2", "MemVal2"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"DataSource:DB2:Connection", "MemVal3"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"Data", "MemVal4"} }; @@ -426,12 +427,12 @@ public void CanGetConfigurationSection() public void CanGetConnectionStrings() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"ConnectionStrings:DB1:Connection1", "MemVal1"}, {"ConnectionStrings:DB1:Connection2", "MemVal2"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"ConnectionStrings:DB2:Connection", "MemVal3"} }; @@ -459,16 +460,16 @@ public void CanGetConnectionStrings() public void CanGetConfigurationChildren() { // Arrange - var dic1 = new Dictionary() + var dic1 = new Dictionary() { {"Data:DB1:Connection1", "MemVal1"}, {"Data:DB1:Connection2", "MemVal2"} }; - var dic2 = new Dictionary() + var dic2 = new Dictionary() { {"Data:DB2Connection", "MemVal3"} }; - var dic3 = new Dictionary() + var dic3 = new Dictionary() { {"DataSource:DB3:Connection", "MemVal4"} }; @@ -499,7 +500,7 @@ public void CanGetConfigurationChildren() public void SourcesReturnsAddedConfigurationProviders() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem:KeyInMem", "MemVal"} }; @@ -655,7 +656,7 @@ public void NewTokenAfterReloadIsNotChanged() public void KeyStartingWithColonMeansFirstSectionHasEmptyName() { // Arrange - var dict = new Dictionary + var dict = new Dictionary { [":Key2"] = "value" }; @@ -677,7 +678,7 @@ public void KeyStartingWithColonMeansFirstSectionHasEmptyName() public void KeyWithDoubleColonHasSectionWithEmptyName() { // Arrange - var dict = new Dictionary + var dict = new Dictionary { ["Key1::Key3"] = "value" }; @@ -701,7 +702,7 @@ public void KeyWithDoubleColonHasSectionWithEmptyName() public void KeyEndingWithColonMeansLastSectionHasEmptyName() { // Arrange - var dict = new Dictionary + var dict = new Dictionary { ["Key1:"] = "value" }; @@ -723,7 +724,7 @@ public void KeyEndingWithColonMeansLastSectionHasEmptyName() public void SectionWithValueExists() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, @@ -748,7 +749,7 @@ public void SectionWithValueExists() public void SectionGetRequiredSectionSuccess() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:KeyInMem1", "ValueInMem1"}, @@ -771,7 +772,7 @@ public void SectionGetRequiredSectionSuccess() public void SectionGetRequiredSectionMissingThrowException() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", "Value1"}, {"Mem1:Deep1", "Value1"}, @@ -795,7 +796,7 @@ public void SectionGetRequiredSectionNullThrowException() public void SectionWithChildrenExists() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1:KeyInMem1", "ValueInMem1"}, {"Mem1:KeyInMem1:Deep1", "ValueDeep1"}, @@ -822,7 +823,7 @@ public void SectionWithChildrenExists() public void KeyWithValueAndWithoutChildrenExistsAsSection(string value) { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", value} }; @@ -841,7 +842,7 @@ public void KeyWithValueAndWithoutChildrenExistsAsSection(string value) public void KeyWithNullValueAndWithoutChildrenIsASectionButNotExists() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1", null} }; @@ -864,7 +865,7 @@ public void KeyWithNullValueAndWithoutChildrenIsASectionButNotExists() public void SectionWithChildrenHasNullValue() { // Arrange - var dict = new Dictionary() + var dict = new Dictionary() { {"Mem1:KeyInMem1", "ValueInMem1"}, }; @@ -892,11 +893,11 @@ public void NullSectionDoesNotExist() internal class NullReloadTokenConfigSource : IConfigurationSource, IConfigurationProvider { - public IEnumerable GetChildKeys(IEnumerable earlierKeys, string parentPath) => throw new NotImplementedException(); + public IEnumerable GetChildKeys(IEnumerable earlierKeys, string? parentPath) => throw new NotImplementedException(); public Primitives.IChangeToken GetReloadToken() => null; public void Load() { } - public void Set(string key, string value) => throw new NotImplementedException(); - public bool TryGet(string key, out string value) => throw new NotImplementedException(); + public void Set(string key, string? value) => throw new NotImplementedException(); + public bool TryGet(string key, [MaybeNullWhen(false)] out string value) => throw new NotImplementedException(); public IConfigurationProvider Build(IConfigurationBuilder builder) => this; } diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/Microsoft.Extensions.Configuration.Tests.csproj b/src/libraries/Microsoft.Extensions.Configuration/tests/Microsoft.Extensions.Configuration.Tests.csproj index 2354ddcd9ffcc5..19b8a2d203f182 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/Microsoft.Extensions.Configuration.Tests.csproj +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/Microsoft.Extensions.Configuration.Tests.csproj @@ -2,6 +2,7 @@ $(NetCoreAppCurrent);net461 + enable From 31576e7ed67b1e6c3d8738d9c630c1748aa2009f Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 15 Aug 2021 02:22:42 +0300 Subject: [PATCH 3/6] Update ConfigurationRootTest.cs --- .../Common/tests/Extensions/ConfigurationRootTest.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/Common/tests/Extensions/ConfigurationRootTest.cs b/src/libraries/Common/tests/Extensions/ConfigurationRootTest.cs index d61144d0e43138..3ff32779719895 100644 --- a/src/libraries/Common/tests/Extensions/ConfigurationRootTest.cs +++ b/src/libraries/Common/tests/Extensions/ConfigurationRootTest.cs @@ -95,13 +95,13 @@ public void Dispose() public class ChangeToken : IChangeToken { - public List<(Action, object)> Callbacks { get; } = new List<(Action, object)>(); + public List<(Action, object?)> Callbacks { get; } = new List<(Action, object?)>(); public bool HasChanged => false; public bool ActiveChangeCallbacks => true; - public IDisposable RegisterChangeCallback(Action callback, object state) + public IDisposable RegisterChangeCallback(Action callback, object? state) { var item = (callback, state); Callbacks.Add(item); @@ -110,9 +110,9 @@ public IDisposable RegisterChangeCallback(Action callback, object state) private class DisposableAction : IDisposable { - private Action _action; + private Action? _action; - public DisposableAction(Action action) + public DisposableAction(Action? action) { _action = action; } From d8bdc49f0c73941db9af853e9b309a1aca1311e2 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 15 Aug 2021 03:06:56 +0300 Subject: [PATCH 4/6] Update ConfigurationTests.cs --- .../tests/FunctionalTests/ConfigurationTests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs index 6a0c3b8d8109ed..ba7335ee2e4889 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs @@ -424,7 +424,7 @@ public void OnLoadErrorWillBeCalledOnIniLoadError() _fileSystem.WriteFile("error.ini", @"IniKey1=IniValue1 IniKey1=IniValue2"); - FileConfigurationProvider provider = null; + FileConfigurationProvider? provider = null; Exception? error = null; Action loadError = c => { @@ -452,7 +452,7 @@ public void OnLoadErrorCanIgnoreErrors() { _fileSystem.WriteFile("error.json", @"{""JsonKey1"": "); - FileConfigurationProvider provider = null; + FileConfigurationProvider? provider = null; Action jsonLoadError = c => { provider = c.Provider; @@ -490,7 +490,7 @@ public void CanSetValuesAndReloadValues() foreach (var provider in configurationBuilder.Sources) { Assert.Equal("NewValue", - (provider as FileConfigurationProvider).Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4")); + (provider as FileConfigurationProvider)?.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4")); } // Recover values by reloading @@ -505,7 +505,7 @@ public void CanSetValuesAndReloadValues() foreach (var provider in configurationBuilder.Sources) { Assert.Equal("NewValue", - (provider as FileConfigurationProvider).Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4")); + (provider as FileConfigurationProvider)?.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4")); } // Recover values by reloading @@ -834,7 +834,7 @@ public void LoadIncorrectJsonFile_ThrowException() _fileSystem.WriteFile(_jsonFile, json); var exception = Assert.Throws(() => CreateBuilder().AddJsonFile(_jsonFile).Build()); - Assert.Contains("Could not parse the JSON file.", exception.InnerException.Message); + Assert.Contains("Could not parse the JSON file.", exception.InnerException?.Message); } [Fact] From c3212e1b8a5d122ea34966f90b9bb06a5b6858f1 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 15 Aug 2021 03:34:36 +0300 Subject: [PATCH 5/6] Fix tests --- .../Common/tests/Extensions/ConfigurationRootTest.cs | 2 +- .../tests/ConfigurationManagerTest.cs | 8 ++++---- .../tests/ConfigurationTest.cs | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libraries/Common/tests/Extensions/ConfigurationRootTest.cs b/src/libraries/Common/tests/Extensions/ConfigurationRootTest.cs index 3ff32779719895..8b66213715f2f1 100644 --- a/src/libraries/Common/tests/Extensions/ConfigurationRootTest.cs +++ b/src/libraries/Common/tests/Extensions/ConfigurationRootTest.cs @@ -71,7 +71,7 @@ public void ChainedConfigurationIsDisposed(bool shouldDispose) Assert.False(provider.IsDisposed); - (config as IDisposable).Dispose(); + (config as IDisposable)?.Dispose(); Assert.Equal(shouldDispose, provider.IsDisposed); } diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationManagerTest.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationManagerTest.cs index ae642885c6ea1c..119ae597e0b319 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationManagerTest.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationManagerTest.cs @@ -698,9 +698,9 @@ public void CanGetConfigurationChildren() // Assert Assert.Equal(2, configSections.Count()); - Assert.Equal("MemVal1", configSections.FirstOrDefault(c => c.Key == "DB1")["Connection1"]); - Assert.Equal("MemVal2", configSections.FirstOrDefault(c => c.Key == "DB1")["Connection2"]); - Assert.Equal("MemVal3", configSections.FirstOrDefault(c => c.Key == "DB2Connection").Value); + Assert.Equal("MemVal1", configSections.First(c => c.Key == "DB1")["Connection1"]); + Assert.Equal("MemVal2", configSections.First(c => c.Key == "DB1")["Connection2"]); + Assert.Equal("MemVal3", configSections.First(c => c.Key == "DB2Connection").Value); Assert.False(configSections.Exists(c => c.Key == "DB3")); Assert.False(configSections.Exists(c => c.Key == "DB3")); } @@ -1191,7 +1191,7 @@ public IConfigurationProvider Build(IConfigurationBuilder builder) private class NullReloadTokenConfigSource : IConfigurationSource, IConfigurationProvider { public IEnumerable GetChildKeys(IEnumerable earlierKeys, string? parentPath) => throw new NotImplementedException(); - public IChangeToken GetReloadToken() => null; + public IChangeToken GetReloadToken() => null!; public void Load() { } public void Set(string key, string? value) => throw new NotImplementedException(); public bool TryGet(string key, [MaybeNullWhen(false)] out string value) => throw new NotImplementedException(); diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationTest.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationTest.cs index f740aa13c52740..b495761073638a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationTest.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationTest.cs @@ -489,9 +489,9 @@ public void CanGetConfigurationChildren() // Assert Assert.Equal(2, configSections.Count()); - Assert.Equal("MemVal1", configSections.FirstOrDefault(c => c.Key == "DB1")["Connection1"]); - Assert.Equal("MemVal2", configSections.FirstOrDefault(c => c.Key == "DB1")["Connection2"]); - Assert.Equal("MemVal3", configSections.FirstOrDefault(c => c.Key == "DB2Connection").Value); + Assert.Equal("MemVal1", configSections.First(c => c.Key == "DB1")["Connection1"]); + Assert.Equal("MemVal2", configSections.First(c => c.Key == "DB1")["Connection2"]); + Assert.Equal("MemVal3", configSections.First(c => c.Key == "DB2Connection").Value); Assert.False(configSections.Exists(c => c.Key == "DB3")); Assert.False(configSections.Exists(c => c.Key == "DB3")); } @@ -788,7 +788,7 @@ public void SectionGetRequiredSectionMissingThrowException() [Fact] public void SectionGetRequiredSectionNullThrowException() { - IConfigurationRoot config = null; + IConfigurationRoot? config = null; Assert.Throws(() => config.GetRequiredSection("Mem1")); } @@ -894,7 +894,7 @@ public void NullSectionDoesNotExist() internal class NullReloadTokenConfigSource : IConfigurationSource, IConfigurationProvider { public IEnumerable GetChildKeys(IEnumerable earlierKeys, string? parentPath) => throw new NotImplementedException(); - public Primitives.IChangeToken GetReloadToken() => null; + public Primitives.IChangeToken GetReloadToken() => null!; public void Load() { } public void Set(string key, string? value) => throw new NotImplementedException(); public bool TryGet(string key, [MaybeNullWhen(false)] out string value) => throw new NotImplementedException(); From a8eb1ff69a86568225e76b28f04c40f80b595607 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 25 Aug 2021 02:09:21 +0300 Subject: [PATCH 6/6] Update ConfigurationTests.cs --- .../tests/FunctionalTests/ConfigurationTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs index ba7335ee2e4889..aa9fa67373b11b 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs @@ -137,9 +137,9 @@ public void MissingFileIncludesAbsolutePathIfPhysicalFileProvider() private class NotVeryGoodFileProvider : IFileProvider { - public IDirectoryContents GetDirectoryContents(string? subpath) => null!; + public IDirectoryContents GetDirectoryContents(string subpath) => null!; public IFileInfo GetFileInfo(string subpath) => null!; - public IChangeToken Watch(string? filter) => null!; + public IChangeToken Watch(string filter) => null!; } private class MissingFile : IFileInfo @@ -155,9 +155,9 @@ private class MissingFile : IFileInfo private class AlwaysMissingFileProvider : IFileProvider { - public IDirectoryContents GetDirectoryContents(string? subpath) => null!; + public IDirectoryContents GetDirectoryContents(string subpath) => null!; public IFileInfo GetFileInfo(string subpath) => null!; - public IChangeToken Watch(string? filter) => null!; + public IChangeToken Watch(string filter) => null!; } private void WriteTestFiles()