-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
FileConfigurationProviderTest.cs
157 lines (132 loc) · 6.55 KB
/
FileConfigurationProviderTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration.Test;
using Microsoft.Extensions.FileProviders;
using Moq;
using Xunit;
namespace Microsoft.Extensions.Configuration.FileExtensions.Test
{
public class FileConfigurationProviderTest
{
// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
public void ProviderDisposesChangeTokenRegistration()
{
var changeToken = new ConfigurationRootTest.ChangeToken();
var fileProviderMock = new Mock<IFileProvider>();
fileProviderMock.Setup(fp => fp.Watch(It.IsAny<string>())).Returns(changeToken);
var provider = new FileConfigurationProviderImpl(new FileConfigurationSourceImpl
{
FileProvider = fileProviderMock.Object,
ReloadOnChange = true,
});
Assert.NotEmpty(changeToken.Callbacks);
provider.Dispose();
Assert.Empty(changeToken.Callbacks);
}
public static readonly IEnumerable<object[]> ProviderThrowsInvalidDataExceptionInput = new[]
{
new object[] { @$"C:{Path.DirectorySeparatorChar}{Guid.NewGuid()}{Path.DirectorySeparatorChar}configuration.txt" },
new object[] { @$"{Path.DirectorySeparatorChar}{Guid.NewGuid()}{Path.DirectorySeparatorChar}configuration.txt" }
};
// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52319", TestPlatforms.Android)]
public void ProviderThrowsInvalidDataExceptionWhenLoadFails()
{
var tempFile = Path.GetTempFileName();
try
{
File.WriteAllText(tempFile, "Test::FileData");
var fileProviderMock = new Mock<IFileProvider>();
fileProviderMock.Setup(fp => fp.Watch(It.IsAny<string>())).Returns(new ConfigurationRootTest.ChangeToken());
fileProviderMock.Setup(fp => fp.GetFileInfo(It.IsAny<string>())).Returns(new FileInfoImpl(tempFile));
var source = new FileConfigurationSourceImpl
{
FileProvider = fileProviderMock.Object,
ReloadOnChange = true,
};
var provider = new ThrowOnLoadFileConfigurationProviderImpl(source);
var exception = Assert.Throws<InvalidDataException>(() => provider.Load());
Assert.Contains($"Failed to load configuration from file '{tempFile}'", exception.Message);
}
finally
{
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[MemberData(nameof(ProviderThrowsInvalidDataExceptionInput))]
public void ProviderThrowsFileNotFoundExceptionWhenNotFound(string physicalPath)
{
var fileProviderMock = new Mock<IFileProvider>();
fileProviderMock.Setup(fp => fp.Watch(It.IsAny<string>())).Returns(new ConfigurationRootTest.ChangeToken());
fileProviderMock.Setup(fp => fp.GetFileInfo(It.IsAny<string>())).Returns(new FileInfoImpl(physicalPath, false));
var source = new FileConfigurationSourceImpl
{
FileProvider = fileProviderMock.Object,
ReloadOnChange = true,
};
var provider = new FileConfigurationProviderImpl(source);
var exception = Assert.Throws<FileNotFoundException>(() => provider.Load());
Assert.Contains(physicalPath, exception.Message);
}
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[MemberData(nameof(ProviderThrowsInvalidDataExceptionInput))]
public void ProviderThrowsDirectoryNotFoundExceptionWhenNotFound(string physicalPath)
{
var fileProviderMock = new Mock<IFileProvider>();
fileProviderMock.Setup(fp => fp.Watch(It.IsAny<string>())).Returns(new ConfigurationRootTest.ChangeToken());
fileProviderMock.Setup(fp => fp.GetFileInfo(It.IsAny<string>())).Returns(new FileInfoImpl(physicalPath));
var source = new FileConfigurationSourceImpl
{
FileProvider = fileProviderMock.Object,
ReloadOnChange = true,
};
var provider = new FileConfigurationProviderImpl(source);
var exception = Assert.Throws<DirectoryNotFoundException>(() => provider.Load());
Assert.Contains(physicalPath, exception.Message);
}
public class FileInfoImpl : IFileInfo
{
public FileInfoImpl(string physicalPath, bool exists = true) =>
(PhysicalPath, Exists) = (physicalPath, exists);
public Stream CreateReadStream() => new MemoryStream();
public bool Exists { get; set; }
public bool IsDirectory => false;
public DateTimeOffset LastModified => default;
public long Length => default;
public string Name => default;
public string PhysicalPath { get; }
}
public class FileConfigurationProviderImpl : FileConfigurationProvider
{
public FileConfigurationProviderImpl(FileConfigurationSource source)
: base(source)
{ }
public override void Load(Stream stream)
{ }
}
public class ThrowOnLoadFileConfigurationProviderImpl : FileConfigurationProvider
{
public ThrowOnLoadFileConfigurationProviderImpl(FileConfigurationSource source)
: base(source)
{ }
public override void Load(Stream stream) => throw new Exception("This is a test exception.");
}
public class FileConfigurationSourceImpl : FileConfigurationSource
{
public override IConfigurationProvider Build(IConfigurationBuilder builder)
{
EnsureDefaults(builder);
return new FileConfigurationProviderImpl(this);
}
}
}
}