Method to get source for each configuration element

Fixes dotnet/extensions#609 by allowing the configuration paths to be associated with values to help with debugging.
\n\nCommit migrated from d7f8e253d4
This commit is contained in:
Arthur Vickers 2018-12-07 11:58:10 -08:00
parent b330870678
commit 6d45c06807
3 changed files with 295 additions and 127 deletions

View File

@ -39,10 +39,8 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile
{ {
return; return;
} }
else
{ throw new DirectoryNotFoundException("A non-null file provider for the directory is required when this source is not optional.");
throw new DirectoryNotFoundException("A non-null file provider for the directory is required when this source is not optional.");
}
} }
var directory = Source.FileProvider.GetDirectoryContents("/"); var directory = Source.FileProvider.GetDirectoryContents("/");
@ -68,5 +66,15 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile
} }
} }
} }
private string GetDirectoryName()
=> Source.FileProvider?.GetFileInfo("/")?.PhysicalPath ?? "<Unknown>";
/// <summary>
/// Generates a string representing this provider name and relevant details.
/// </summary>
/// <returns> The configuration name. </returns>
public override string ToString()
=> $"{GetType().Name} for files in '{GetDirectoryName()}' ({(Source.Optional ? "Optional" : "Required")})";
} }
} }

View File

@ -14,7 +14,39 @@ namespace Microsoft.Extensions.Configuration.Test
[Fact] [Fact]
public virtual void Load_from_single_provider() public virtual void Load_from_single_provider()
{ {
AssertConfig(BuildConfigRoot(LoadThroughProvider(TestSection.TestConfig))); var configRoot = BuildConfigRoot(LoadThroughProvider(TestSection.TestConfig));
AssertConfig(configRoot);
}
[Fact]
public virtual void Has_debug_view()
{
var configRoot = BuildConfigRoot(LoadThroughProvider(TestSection.TestConfig));
var providerTag = configRoot.Providers.Single().ToString();
var expected =
$@"Key1=Value1 ({providerTag})
Section1:
Key2=Value12 ({providerTag})
Section2:
Key3=Value123 ({providerTag})
Key3a:
0=ArrayValue0 ({providerTag})
1=ArrayValue1 ({providerTag})
2=ArrayValue2 ({providerTag})
Section3:
Section4:
Key4=Value344 ({providerTag})
";
AssertDebugView(configRoot, expected);
}
[Fact]
public virtual void Null_values_are_included_in_the_config()
{
AssertConfig(BuildConfigRoot(LoadThroughProvider(TestSection.NullsTestConfig)), expectNulls: true, nullValue: "");
} }
[Fact] [Fact]
@ -22,8 +54,13 @@ namespace Microsoft.Extensions.Configuration.Test
{ {
AssertConfig( AssertConfig(
BuildConfigRoot( BuildConfigRoot(
LoadUsingMemoryProvider(TestSection.MissingSection2Config), LoadUsingMemoryProvider(TestSection.MissingSection2ValuesConfig),
LoadThroughProvider(TestSection.MissingSection4Config))); LoadThroughProvider(TestSection.MissingSection4Config)));
AssertConfig(
BuildConfigRoot(
LoadUsingMemoryProvider(TestSection.MissingSection4Config),
LoadThroughProvider(TestSection.MissingSection2ValuesConfig)));
} }
[Fact] [Fact]
@ -31,8 +68,13 @@ namespace Microsoft.Extensions.Configuration.Test
{ {
AssertConfig( AssertConfig(
BuildConfigRoot( BuildConfigRoot(
LoadThroughProvider(TestSection.MissingSection2Config), LoadThroughProvider(TestSection.MissingSection2ValuesConfig),
LoadUsingMemoryProvider(TestSection.MissingSection4Config))); LoadUsingMemoryProvider(TestSection.MissingSection4Config)));
AssertConfig(
BuildConfigRoot(
LoadThroughProvider(TestSection.MissingSection4Config),
LoadUsingMemoryProvider(TestSection.MissingSection2ValuesConfig)));
} }
[Fact] [Fact]
@ -128,48 +170,83 @@ namespace Microsoft.Extensions.Configuration.Test
public string Key4 { get; set; } public string Key4 { get; set; }
} }
protected virtual void AssertConfig(IConfigurationRoot config) protected virtual void AssertDebugView(
IConfigurationRoot config,
string expected)
{ {
Assert.Equal("Value1", config["Key1"], StringComparer.InvariantCultureIgnoreCase); string RemoveLineEnds(string source) => source.Replace("\n", "").Replace("\r", "");
Assert.Equal("Value12", config["Section1:Key2"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Value123", config["Section1:Section2:Key3"], StringComparer.InvariantCultureIgnoreCase); var actual = config.GetDebugView();
Assert.Equal("ArrayValue0", config["Section1:Section2:Key3a:0"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue1", config["Section1:Section2:Key3a:1"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(
Assert.Equal("ArrayValue2", config["Section1:Section2:Key3a:2"], StringComparer.InvariantCultureIgnoreCase); RemoveLineEnds(expected),
Assert.Equal("Value344", config["Section3:Section4:Key4"], StringComparer.InvariantCultureIgnoreCase); RemoveLineEnds(actual));
}
protected virtual void AssertConfig(
IConfigurationRoot config,
bool expectNulls = false,
string nullValue = null)
{
var value1 = expectNulls ? nullValue : "Value1";
var value12 = expectNulls ? nullValue : "Value12";
var value123 = expectNulls ? nullValue : "Value123";
var arrayvalue0 = expectNulls ? nullValue : "ArrayValue0";
var arrayvalue1 = expectNulls ? nullValue : "ArrayValue1";
var arrayvalue2 = expectNulls ? nullValue : "ArrayValue2";
var value344 = expectNulls ? nullValue : "Value344";
Assert.Equal(value1, config["Key1"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(value12, config["Section1:Key2"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(value123, config["Section1:Section2:Key3"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(arrayvalue0, config["Section1:Section2:Key3a:0"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(arrayvalue1, config["Section1:Section2:Key3a:1"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(arrayvalue2, config["Section1:Section2:Key3a:2"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(value344, config["Section3:Section4:Key4"], StringComparer.InvariantCultureIgnoreCase);
var section1 = config.GetSection("Section1"); var section1 = config.GetSection("Section1");
Assert.Equal("Value12", section1["Key2"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(value12, section1["Key2"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Value123", section1["Section2:Key3"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(value123, section1["Section2:Key3"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue0", section1["Section2:Key3a:0"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue0, section1["Section2:Key3a:0"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue1", section1["Section2:Key3a:1"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue1, section1["Section2:Key3a:1"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue2", section1["Section2:Key3a:2"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue2, section1["Section2:Key3a:2"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1", section1.Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section1", section1.Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(section1.Value); Assert.Null(section1.Value);
var section2 = config.GetSection("Section1:Section2"); var section2 = config.GetSection("Section1:Section2");
Assert.Equal("Value123", section2["Key3"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(value123, section2["Key3"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue0", section2["Key3a:0"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue0, section2["Key3a:0"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue1", section2["Key3a:1"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue1, section2["Key3a:1"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue2", section2["Key3a:2"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue2, section2["Key3a:2"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2", section2.Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section1:Section2", section2.Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(section2.Value); Assert.Null(section2.Value);
section2 = section1.GetSection("Section2"); section2 = section1.GetSection("Section2");
Assert.Equal("Value123", section2["Key3"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(value123, section2["Key3"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue0", section2["Key3a:0"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue0, section2["Key3a:0"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue1", section2["Key3a:1"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue1, section2["Key3a:1"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("ArrayValue2", section2["Key3a:2"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(arrayvalue2, section2["Key3a:2"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2", section2.Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section1:Section2", section2.Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(section2.Value); Assert.Null(section2.Value);
var section3a = section2.GetSection("Key3a");
Assert.Equal(arrayvalue0, section3a["0"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(arrayvalue1, section3a["1"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(arrayvalue2, section3a["2"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2:Key3a", section3a.Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(section3a.Value);
var section3 = config.GetSection("Section3");
Assert.Equal("Section3", section3.Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(section3.Value);
var section4 = config.GetSection("Section3:Section4"); var section4 = config.GetSection("Section3:Section4");
Assert.Equal("Value344", section4["Key4"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(value344, section4["Key4"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section3:Section4", section4.Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section3:Section4", section4.Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(section4.Value); Assert.Null(section4.Value);
section4 = config.GetSection("Section3").GetSection("Section4"); section4 = config.GetSection("Section3").GetSection("Section4");
Assert.Equal("Value344", section4["Key4"], StringComparer.InvariantCultureIgnoreCase); Assert.Equal(value344, section4["Key4"], StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section3:Section4", section4.Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section3:Section4", section4.Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(section4.Value); Assert.Null(section4.Value);
@ -179,7 +256,7 @@ namespace Microsoft.Extensions.Configuration.Test
Assert.Equal("Key1", sections[0].Key, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Key1", sections[0].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Key1", sections[0].Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Key1", sections[0].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Value1", sections[0].Value, StringComparer.InvariantCultureIgnoreCase); Assert.Equal(value1, sections[0].Value, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1", sections[1].Key, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section1", sections[1].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1", sections[1].Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section1", sections[1].Path, StringComparer.InvariantCultureIgnoreCase);
@ -195,11 +272,55 @@ namespace Microsoft.Extensions.Configuration.Test
Assert.Equal("Key2", sections[0].Key, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Key2", sections[0].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Key2", sections[0].Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section1:Key2", sections[0].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Value12", sections[0].Value, StringComparer.InvariantCultureIgnoreCase); Assert.Equal(value12, sections[0].Value, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section2", sections[1].Key, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section2", sections[1].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2", sections[1].Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal("Section1:Section2", sections[1].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(sections[1].Value); Assert.Null(sections[1].Value);
sections = section2.GetChildren().ToList();
Assert.Equal(2, sections.Count);
Assert.Equal("Key3", sections[0].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2:Key3", sections[0].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(value123, sections[0].Value, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Key3a", sections[1].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2:Key3a", sections[1].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(sections[1].Value);
sections = section3a.GetChildren().ToList();
Assert.Equal(3, sections.Count);
Assert.Equal("0", sections[0].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2:Key3a:0", sections[0].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(arrayvalue0, sections[0].Value, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("1", sections[1].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2:Key3a:1", sections[1].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(arrayvalue1, sections[1].Value, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("2", sections[2].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section1:Section2:Key3a:2", sections[2].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(arrayvalue2, sections[2].Value, StringComparer.InvariantCultureIgnoreCase);
sections = section3.GetChildren().ToList();
Assert.Single(sections);
Assert.Equal("Section4", sections[0].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section3:Section4", sections[0].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Null(sections[0].Value);
sections = section4.GetChildren().ToList();
Assert.Single(sections);
Assert.Equal("Key4", sections[0].Key, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal("Section3:Section4:Key4", sections[0].Path, StringComparer.InvariantCultureIgnoreCase);
Assert.Equal(value344, sections[0].Value, StringComparer.InvariantCultureIgnoreCase);
} }
protected abstract (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(TestSection testConfig); protected abstract (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(TestSection testConfig);
@ -272,7 +393,7 @@ namespace Microsoft.Extensions.Configuration.Test
public IEnumerable<(string Key, string Value)> Expand(string key) public IEnumerable<(string Key, string Value)> Expand(string key)
{ {
if (AsString != null) if (AsArray == null)
{ {
yield return (key, AsString); yield return (key, AsString);
} }
@ -310,7 +431,7 @@ namespace Microsoft.Extensions.Configuration.Test
Values = new[] Values = new[]
{ {
("Key3", (TestKeyValue)"Value123"), ("Key3", (TestKeyValue)"Value123"),
("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"}), ("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"})
}, },
}) })
} }
@ -321,11 +442,10 @@ namespace Microsoft.Extensions.Configuration.Test
{ {
("Section4", new TestSection ("Section4", new TestSection
{ {
Values = new[] {("Key4", (TestKeyValue)"Value344")}, Values = new[] {("Key4", (TestKeyValue)"Value344")}
}) })
} }
}), })
} }
}; };
@ -345,7 +465,7 @@ namespace Microsoft.Extensions.Configuration.Test
Values = new[] Values = new[]
{ {
("Key3", (TestKeyValue)"-----"), ("Key3", (TestKeyValue)"-----"),
("Key3a", (TestKeyValue)new[] {"-----------", "-----------", "-----------"}), ("Key3a", (TestKeyValue)new[] {"-----------", "-----------", "-----------"})
}, },
}) })
} }
@ -356,15 +476,14 @@ namespace Microsoft.Extensions.Configuration.Test
{ {
("Section4", new TestSection ("Section4", new TestSection
{ {
Values = new[] {("Key4", (TestKeyValue)"--------")}, Values = new[] {("Key4", (TestKeyValue)"--------")}
}) })
} }
}), })
} }
}; };
public static TestSection MissingSection2Config { get; } public static TestSection MissingSection2ValuesConfig { get; }
= new TestSection = new TestSection
{ {
Values = new[] { ("Key1", (TestKeyValue)"Value1") }, Values = new[] { ("Key1", (TestKeyValue)"Value1") },
@ -373,6 +492,16 @@ namespace Microsoft.Extensions.Configuration.Test
("Section1", new TestSection ("Section1", new TestSection
{ {
Values = new[] {("Key2", (TestKeyValue)"Value12")}, Values = new[] {("Key2", (TestKeyValue)"Value12")},
Sections = new[]
{
("Section2", new TestSection
{
Values = new[]
{
("Key3a", (TestKeyValue)new[] {"ArrayValue0"})
},
})
}
}), }),
("Section3", new TestSection ("Section3", new TestSection
{ {
@ -380,11 +509,10 @@ namespace Microsoft.Extensions.Configuration.Test
{ {
("Section4", new TestSection ("Section4", new TestSection
{ {
Values = new[] {("Key4", (TestKeyValue)"Value344")}, Values = new[] {("Key4", (TestKeyValue)"Value344")}
}) })
} }
}), })
} }
}; };
@ -405,15 +533,12 @@ namespace Microsoft.Extensions.Configuration.Test
Values = new[] Values = new[]
{ {
("Key3", (TestKeyValue)"Value123"), ("Key3", (TestKeyValue)"Value123"),
("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"}), ("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"})
}, },
}) })
} }
}), }),
("Section3", new TestSection ("Section3", new TestSection())
{
}),
} }
}; };
@ -433,7 +558,7 @@ namespace Microsoft.Extensions.Configuration.Test
Values = new[] Values = new[]
{ {
("KeY3", (TestKeyValue)"Value123"), ("KeY3", (TestKeyValue)"Value123"),
("KeY3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"}), ("KeY3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"})
}, },
}) })
} }
@ -444,11 +569,10 @@ namespace Microsoft.Extensions.Configuration.Test
{ {
("SectioN4", new TestSection ("SectioN4", new TestSection
{ {
Values = new[] {("KeY4", (TestKeyValue)"Value344")}, Values = new[] {("KeY4", (TestKeyValue)"Value344")}
}) })
} }
}), })
} }
}; };
@ -472,7 +596,7 @@ namespace Microsoft.Extensions.Configuration.Test
Values = new[] Values = new[]
{ {
("Key3", (TestKeyValue)"Value123"), ("Key3", (TestKeyValue)"Value123"),
("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"}), ("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"})
}, },
}), }),
("Section2", new TestSection ("Section2", new TestSection
@ -480,7 +604,7 @@ namespace Microsoft.Extensions.Configuration.Test
Values = new[] Values = new[]
{ {
("Key3", (TestKeyValue)"Value123"), ("Key3", (TestKeyValue)"Value123"),
("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"}), ("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"})
}, },
}) })
@ -492,11 +616,10 @@ namespace Microsoft.Extensions.Configuration.Test
{ {
("Section4", new TestSection ("Section4", new TestSection
{ {
Values = new[] {("Key4", (TestKeyValue)"Value344")}, Values = new[] {("Key4", (TestKeyValue)"Value344")}
}) })
} }
}), })
} }
}; };
@ -520,7 +643,7 @@ namespace Microsoft.Extensions.Configuration.Test
Values = new[] Values = new[]
{ {
("Key3", (TestKeyValue)"Value123"), ("Key3", (TestKeyValue)"Value123"),
("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"}), ("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"})
}, },
}), }),
("SectioN2", new TestSection ("SectioN2", new TestSection
@ -528,7 +651,7 @@ namespace Microsoft.Extensions.Configuration.Test
Values = new[] Values = new[]
{ {
("KeY3", (TestKeyValue)"Value123"), ("KeY3", (TestKeyValue)"Value123"),
("KeY3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"}), ("KeY3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2"})
}, },
}) })
@ -540,11 +663,97 @@ namespace Microsoft.Extensions.Configuration.Test
{ {
("Section4", new TestSection ("Section4", new TestSection
{ {
Values = new[] {("Key4", (TestKeyValue)"Value344")}, Values = new[] {("Key4", (TestKeyValue)"Value344")}
})
}
})
}
};
public static TestSection NullsTestConfig { get; }
= new TestSection
{
Values = new[] { ("Key1", new TestKeyValue((string)null)) },
Sections = new[]
{
("Section1", new TestSection
{
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})
},
}) })
} }
}), }),
("Section3", new TestSection
{
Sections = new[]
{
("Section4", new TestSection
{
Values = new[] {("Key4", new TestKeyValue((string)null))}
})
}
})
}
};
public static TestSection ExtraValuesTestConfig { get; }
= new TestSection
{
Values = new[]
{
("Key1", (TestKeyValue)"Value1"),
("Key1r", (TestKeyValue)"Value1r")
},
Sections = new[]
{
("Section1", new TestSection
{
Values = new[]
{
("Key2", (TestKeyValue)"Value12"),
("Key2r", (TestKeyValue)"Value12r")
},
Sections = new[]
{
("Section2", new TestSection
{
Values = new[]
{
("Key3", (TestKeyValue)"Value123"),
("Key3a", (TestKeyValue)new[] {"ArrayValue0", "ArrayValue1", "ArrayValue2", "ArrayValue2r"}),
("Key3ar", (TestKeyValue)new[] {"ArrayValue0r"})
},
})
}
}),
("Section3", new TestSection
{
Sections = new[]
{
("Section4", new TestSection
{
Values = new[] {("Key4", (TestKeyValue)"Value344")}
})
}
}),
("Section5r", new TestSection
{
Sections = new[]
{
("Section6r", new TestSection
{
Values = new[] {("Key5r", (TestKeyValue)"Value565r")}
})
}
})
} }
}; };
} }

View File

@ -190,20 +190,11 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile.Test
_contents = new TestDirectoryContents(files); _contents = new TestDirectoryContents(files);
} }
public IDirectoryContents GetDirectoryContents(string subpath) public IDirectoryContents GetDirectoryContents(string subpath) => _contents;
{
return _contents;
}
public IFileInfo GetFileInfo(string subpath) public IFileInfo GetFileInfo(string subpath) => new TestFile("TestDirectory");
{
throw new NotImplementedException();
}
public IChangeToken Watch(string filter) public IChangeToken Watch(string filter) => throw new NotImplementedException();
{
throw new NotImplementedException();
}
} }
class TestDirectoryContents : IDirectoryContents class TestDirectoryContents : IDirectoryContents
@ -215,75 +206,33 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile.Test
_list = new List<IFileInfo>(files); _list = new List<IFileInfo>(files);
} }
public bool Exists public bool Exists => true;
{
get
{
return true;
}
}
public IEnumerator<IFileInfo> GetEnumerator() public IEnumerator<IFileInfo> GetEnumerator() => _list.GetEnumerator();
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
{
return GetEnumerator();
}
} }
//TODO: Probably need a directory and file type. //TODO: Probably need a directory and file type.
class TestFile : IFileInfo class TestFile : IFileInfo
{ {
private string _name; private readonly string _name;
private string _contents; private readonly string _contents;
public bool Exists public bool Exists => true;
{
get
{
return true;
}
}
public bool IsDirectory public bool IsDirectory
{ {
get; get;
} }
public DateTimeOffset LastModified public DateTimeOffset LastModified => throw new NotImplementedException();
{
get
{
throw new NotImplementedException();
}
}
public long Length public long Length => throw new NotImplementedException();
{
get
{
throw new NotImplementedException();
}
}
public string Name public string Name => _name;
{
get
{
return _name;
}
}
public string PhysicalPath public string PhysicalPath => "Root/" + Name;
{
get
{
throw new NotImplementedException();
}
}
public TestFile(string name) public TestFile(string name)
{ {
@ -304,7 +253,9 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile.Test
throw new InvalidOperationException("Cannot create stream from directory"); throw new InvalidOperationException("Cannot create stream from directory");
} }
return new MemoryStream(Encoding.UTF8.GetBytes(_contents)); return _contents == null
? new MemoryStream()
: new MemoryStream(Encoding.UTF8.GetBytes(_contents));
} }
} }
} }