Skip to content

Commit

Permalink
Fix add user secrets (#44838)
Browse files Browse the repository at this point in the history
  • Loading branch information
galakt authored Nov 20, 2020
1 parent 2f18501 commit 5301748
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static IConfigurationBuilder AddUserSecrets(this IConfigurationBuilder co
UserSecretsIdAttribute attribute = assembly.GetCustomAttribute<UserSecretsIdAttribute>();
if (attribute != null)
{
return AddUserSecrets(configuration, attribute.UserSecretsId, reloadOnChange);
return AddUserSecretsInternal(configuration, attribute.UserSecretsId, optional, reloadOnChange);
}

if (!optional)
Expand Down Expand Up @@ -169,6 +169,9 @@ public static IConfigurationBuilder AddUserSecrets(this IConfigurationBuilder co
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
/// <returns>The configuration builder.</returns>
public static IConfigurationBuilder AddUserSecrets(this IConfigurationBuilder configuration, string userSecretsId, bool reloadOnChange)
=> AddUserSecretsInternal(configuration, userSecretsId, true, reloadOnChange);

private static IConfigurationBuilder AddUserSecretsInternal(IConfigurationBuilder configuration, string userSecretsId, bool optional, bool reloadOnChange)
{
if (configuration == null)
{
Expand All @@ -180,16 +183,16 @@ public static IConfigurationBuilder AddUserSecrets(this IConfigurationBuilder co
throw new ArgumentNullException(nameof(userSecretsId));
}

return AddSecretsFile(configuration, PathHelper.GetSecretsPathFromSecretsId(userSecretsId), reloadOnChange);
return AddSecretsFile(configuration, PathHelper.GetSecretsPathFromSecretsId(userSecretsId), optional, reloadOnChange);
}

private static IConfigurationBuilder AddSecretsFile(IConfigurationBuilder configuration, string secretPath, bool reloadOnChange)
private static IConfigurationBuilder AddSecretsFile(IConfigurationBuilder configuration, string secretPath, bool optional, bool reloadOnChange)
{
string directoryPath = Path.GetDirectoryName(secretPath);
PhysicalFileProvider fileProvider = Directory.Exists(directoryPath)
? new PhysicalFileProvider(directoryPath)
: null;
return configuration.AddJsonFile(fileProvider, PathHelper.SecretsFileName, optional: true, reloadOnChange);
return configuration.AddJsonFile(fileProvider, PathHelper.SecretsFileName, optional, reloadOnChange);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ public void AddUserSecrets_DoesNotThrowsIfOptional()
Assert.Empty(config.AsEnumerable());
}

[Fact]
public void AddUserSecrets_DoesThrowsIfNotOptionalAndSecretDoesNotExist()
{
var secretId = Assembly.GetExecutingAssembly().GetName().Name;
var secretPath = PathHelper.GetSecretsPathFromSecretsId(secretId);
if (File.Exists(secretPath))
{
File.Delete(secretPath);
}

Assert.Throws<FileNotFoundException>(() => new ConfigurationBuilder().AddUserSecrets(Assembly.GetExecutingAssembly(), false).Build());
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34580", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public void AddUserSecrets_With_SecretsId_Passed_Explicitly()
Expand Down

0 comments on commit 5301748

Please sign in to comment.