Skip to content

Commit 1876f13

Browse files
authored
Merge pull request #55 from andrewlock/issue-49
Fix #49 in Yaml Extensions
2 parents e686584 + c84a1b4 commit 1876f13

File tree

5 files changed

+45
-38
lines changed

5 files changed

+45
-38
lines changed

src/NetEscapades.Configuration.Yaml/NetEscapades.Configuration.Yaml.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>YAML configuration provider implementation to use with Microsoft.Extensions.Configuration.</Description>
5-
<VersionPrefix>2.0.0</VersionPrefix>
5+
<VersionPrefix>2.0.1</VersionPrefix>
66
<TargetFrameworks>net461;net472;netstandard2.0</TargetFrameworks>
77
<AssemblyName>NetEscapades.Configuration.Yaml</AssemblyName>
88
<PackageId>NetEscapades.Configuration.Yaml</PackageId>

src/NetEscapades.Configuration.Yaml/YamlConfigurationExtensions.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,24 @@ public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder build
7272
{
7373
throw new ArgumentException(Resources.FormatError_InvalidFilePath(), nameof(path));
7474
}
75-
76-
if (provider == null && Path.IsPathRooted(path))
77-
{
78-
provider = new PhysicalFileProvider(Path.GetDirectoryName(path));
79-
path = Path.GetFileName(path);
80-
}
81-
var source = new YamlConfigurationSource
75+
76+
return builder.AddYamlFile(s =>
8277
{
83-
FileProvider = provider,
84-
Path = path,
85-
Optional = optional,
86-
ReloadOnChange = reloadOnChange
87-
};
88-
builder.Add(source);
89-
return builder;
78+
s.FileProvider = provider;
79+
s.Path = path;
80+
s.Optional = optional;
81+
s.ReloadOnChange = reloadOnChange;
82+
s.ResolveFileProvider();
83+
});
9084
}
85+
86+
/// <summary>
87+
/// Adds a YAML configuration source to <paramref name="builder"/>.
88+
/// </summary>
89+
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
90+
/// <param name="configureSource">Configures the source.</param>
91+
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
92+
public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, Action<YamlConfigurationSource> configureSource)
93+
=> builder.Add(configureSource);
9194
}
9295
}

src/NetEscapades.Configuration.Yaml/YamlConfigurationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class YamlConfigurationSource : FileConfigurationSource
99
{
1010
public override IConfigurationProvider Build(IConfigurationBuilder builder)
1111
{
12-
FileProvider = FileProvider ?? builder.GetFileProvider();
12+
EnsureDefaults(builder);
1313
return new YamlConfigurationProvider(this);
1414
}
1515
}

src/NetEscapades.Configuration.Yaml/releasenotes.props

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
<Project>
22
<PropertyGroup>
3-
<PackageReleaseNotes Condition="'$(VersionPrefix)' == '2.0.0'">
3+
<PackageReleaseNotes Condition="'$(VersionPrefix)' == '2.0.1'">
44
<![CDATA[
55
Features:
6-
* Upgrade YamlDotNet to 6.1.2 - this is a breaking change in the dependency as the YamlDotNet.Signed and YamlDotNet packages were merged
7-
* Update supported frameworks - only supports .NET 461, .NET 472, and .NET Standard 2.0 (dropped support for 1.x)
6+
* Fix bug #49 causing exception when using an absolute file path for YAML file path, where directory did not exist.
87
9-
The following values are interpreted as 'null' during parsing (as per v1.1 of the spec http://yaml.org/type/null.html)
10-
11-
```yaml
12-
nullValue1: Null
13-
nullValue2: null
14-
nullValue3: Null
15-
nullValue4: ~
16-
```
17-
18-
Yaml is case sensitive, so be aware that the following values are NOT parsed as null.
19-
Instead they are parsed as their string equivalent (NuLL, NUll, and empty strings)
20-
21-
```yaml
22-
notNull1: NuLL
23-
notNull2: NUll
24-
notNull3:
25-
notNull4: ''
26-
```
278
]]>
289
</PackageReleaseNotes>
2910
</PropertyGroup>

test/NetEscapades.Configuration.Yaml.Tests/YamlConfigurationExtensionsTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.FileProviders;
78
using NetEscapades.Configuration.Yaml;
89
using Xunit;
910

@@ -26,7 +27,7 @@ public void AddYamlFile_ThrowsIfFilePathIsNullOrEmpty(string path)
2627
}
2728

2829
[Fact]
29-
public void AddYamlFile_ThrowsIfFileDoesNotExistAtPath()
30+
public void AddYamlFile_ThrowsIfFileDoesNotExistAtPathAndIsOptional()
3031
{
3132
// Arrange
3233
var path = "file-does-not-exist.Yaml";
@@ -35,5 +36,27 @@ public void AddYamlFile_ThrowsIfFileDoesNotExistAtPath()
3536
var ex = Assert.Throws<FileNotFoundException>(() => new ConfigurationBuilder().AddYamlFile(path).Build());
3637
Assert.StartsWith($"The configuration file '{path}' was not found and is not optional.", ex.Message);
3738
}
39+
40+
[Fact]
41+
public void AddYamlFile_DoesNotThrowIfFileDoesNotExistAndIsOptional()
42+
{
43+
// Arrange
44+
var path = "file-does-not-exist.Yaml";
45+
46+
// Act and Assert
47+
new ConfigurationBuilder().AddYamlFile(path, optional: true).Build();
48+
}
49+
50+
[Fact]
51+
public void AddYamlFile_DoesNotThrowIfAbsolutePathDirectoryDoesNotExist()
52+
{
53+
// Arrange
54+
var path = Path.Combine(Directory.GetCurrentDirectory(), "does", "not", "exist", "file-does-not-exist.Yaml");
55+
56+
// Act and Assert
57+
new ConfigurationBuilder()
58+
.AddYamlFile(path, optional: true)
59+
.Build();
60+
}
3861
}
3962
}

0 commit comments

Comments
 (0)