Skip to content

Commit 3e0a756

Browse files
committed
Fix bug in Yaml Extensions
Was caused by using a dedicated PhysicalFileProvider for rooted file paths.
1 parent e686584 commit 3e0a756

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

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
}

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)