Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix add user secrets #44838

Merged
merged 2 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, usually we name bool parameters. It is not a big deal, but for next time. It helps understand what the true is for when reading the call site.


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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ideally you'd only want to keep the method that you're asserting behavior within the Assert.Throws block.

}

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