Adding back GetBasePath for UserSecrets for now
This commit is contained in:
Родитель
dc10cf2a48
Коммит
d7da902a0a
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
namespace Microsoft.Extensions.Configuration
|
||||
|
@ -9,6 +10,7 @@ namespace Microsoft.Extensions.Configuration
|
|||
public static class FileConfigurationExtensions
|
||||
{
|
||||
private static string FileProviderKey = "FileProvider";
|
||||
private static string BasePathKey = "BasePath";
|
||||
|
||||
/// <summary>
|
||||
/// Sets the default <see cref="IFileProvider"/> to be used for file-based providers.
|
||||
|
@ -50,14 +52,7 @@ namespace Microsoft.Extensions.Configuration
|
|||
return builder.Properties[FileProviderKey] as IFileProvider;
|
||||
}
|
||||
|
||||
#if NET451
|
||||
var stringBasePath = AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string
|
||||
?? AppDomain.CurrentDomain.BaseDirectory
|
||||
?? string.Empty;
|
||||
return new PhysicalFileProvider(stringBasePath);
|
||||
#else
|
||||
return new PhysicalFileProvider(AppContext.BaseDirectory ?? string.Empty);
|
||||
#endif
|
||||
return new PhysicalFileProvider(builder.GetBasePath());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -78,7 +73,37 @@ namespace Microsoft.Extensions.Configuration
|
|||
throw new ArgumentNullException(nameof(basePath));
|
||||
}
|
||||
|
||||
builder.Properties[BasePathKey] = basePath;
|
||||
return builder.SetFileProvider(new PhysicalFileProvider(basePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the base path to discover files in for file-based providers.
|
||||
/// </summary>
|
||||
/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
|
||||
/// <returns>The base path.</returns>
|
||||
public static string GetBasePath(this IConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
if (configurationBuilder == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configurationBuilder));
|
||||
}
|
||||
|
||||
object basePath;
|
||||
if (configurationBuilder.Properties.TryGetValue(BasePathKey, out basePath))
|
||||
{
|
||||
return (string)basePath;
|
||||
}
|
||||
|
||||
#if NET451
|
||||
var stringBasePath = AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ??
|
||||
AppDomain.CurrentDomain.BaseDirectory ??
|
||||
string.Empty;
|
||||
|
||||
return Path.GetFullPath(stringBasePath);
|
||||
#else
|
||||
return AppContext.BaseDirectory ?? string.Empty;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,64 @@ namespace Microsoft.Extensions.Configuration.Json
|
|||
{
|
||||
public class FileConfigurationBuilderExtensionsTest
|
||||
{
|
||||
[Fact]
|
||||
public void SetBasePath_ThrowsIfBasePathIsNull()
|
||||
{
|
||||
// Arrange
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
|
||||
// Act and Assert
|
||||
var ex = Assert.Throws<ArgumentNullException>(() => configurationBuilder.SetBasePath(null));
|
||||
Assert.Equal("basePath", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetBasePath_CheckPropertiesValueOnBuilder()
|
||||
{
|
||||
var expectedBasePath = Directory.GetCurrentDirectory();
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
|
||||
configurationBuilder.SetBasePath(expectedBasePath);
|
||||
Assert.Equal(expectedBasePath, configurationBuilder.Properties["BasePath"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetBasePath_ReturnBaseBathIfSet()
|
||||
{
|
||||
// Arrange
|
||||
var testDir = Path.GetDirectoryName(Path.GetTempFileName());
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
configurationBuilder.SetBasePath(testDir);
|
||||
|
||||
// Act
|
||||
var actualPath = configurationBuilder.GetBasePath();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(testDir, actualPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetBasePath_ReturnBaseDirectoryIfNotSet()
|
||||
{
|
||||
// Arrange
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
|
||||
// Act
|
||||
var actualPath = configurationBuilder.GetBasePath();
|
||||
|
||||
string expectedPath;
|
||||
|
||||
#if NETSTANDARDAPP1_5
|
||||
expectedPath = AppContext.BaseDirectory;
|
||||
#else
|
||||
expectedPath = Path.GetFullPath(AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ??
|
||||
AppDomain.CurrentDomain.BaseDirectory);
|
||||
#endif
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedPath, actualPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetFileProvider_ThrowsIfBasePathIsNull()
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче