-
Notifications
You must be signed in to change notification settings - Fork 15
Make configuration work independent of whether appsettings json files are provided #337
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
Changes from 4 commits
8c61cca
7e77c4a
b4dc253
e266a22
5c94822
5fcbc46
16600bb
319612d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures; | ||
|
|
||
| public class TestProjectFixtureWithoutAppsettings : TestBedFixture | ||
| { | ||
| protected override void AddServices(IServiceCollection services, IConfiguration configuration) | ||
| { | ||
| } | ||
|
|
||
| protected override ValueTask DisposeAsyncCore() => new(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,6 @@ | |
|
|
||
| public record SecretValues | ||
| { | ||
| public string? Secret1 { get; set; } | ||
| public string? Secret2 { get; set; } | ||
| public required string Secret1 { get; set; } | ||
|
||
| public required string Secret2 { get; set; } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,45 @@ | ||
| | ||
| using System.Diagnostics; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Xunit.Microsoft.DependencyInjection.ExampleTests; | ||
|
|
||
| public class UserSecretTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : TestBed<TestProjectFixture>(testOutputHelper, fixture) | ||
| public class UserSecretTests : TestBed<TestProjectFixture> | ||
| { | ||
| private static readonly Guid Guid = Guid.NewGuid(); // Ensure unique user secrets per test run | ||
|
|
||
| private readonly string _secret1Value = $"secret1-{Guid}"; | ||
| private readonly string _secret2Value = $"secret2-{Guid}"; | ||
|
|
||
| public UserSecretTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : base(testOutputHelper, fixture) | ||
| { | ||
| SetSecret(nameof(SecretValues.Secret1), _secret1Value); | ||
| SetSecret(nameof(SecretValues.Secret2), _secret2Value); | ||
| } | ||
|
||
|
|
||
| [Fact] | ||
| public void TestSecretValues() | ||
| { | ||
| /* | ||
| * TODO: Create a user secret entry like the following payload in user secrets and remove the same from appsettings.json file: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ChrisDoernen Please restore this block of comment |
||
| * | ||
| * "SecretValues": { | ||
| * "Secret1": "secret1value", | ||
| * "Secret2": "secret2value" | ||
| * } | ||
| */ | ||
| var secretValues = _fixture.GetService<IOptions<SecretValues>>(_testOutputHelper)!.Value; | ||
| Assert.NotEmpty(secretValues?.Secret1 ?? string.Empty); | ||
| Assert.NotEmpty(secretValues?.Secret1 ?? string.Empty); | ||
| Assert.Equal(secretValues.Secret1, _secret1Value); | ||
| Assert.Equal(secretValues.Secret2, _secret2Value); | ||
| } | ||
|
|
||
| private void SetSecret(string secretName, string secretValue) | ||
|
||
| { | ||
| var startInfo = new ProcessStartInfo | ||
| { | ||
| FileName = "dotnet", | ||
| Arguments = $"user-secrets set {nameof(SecretValues)}:{secretName} {secretValue}", | ||
| WorkingDirectory = Path.Combine(Environment.CurrentDirectory, "..", "..", "..") | ||
| }; | ||
| var proc = Process.Start(startInfo); | ||
| ArgumentNullException.ThrowIfNull(proc); | ||
|
|
||
| proc.WaitForExit(); | ||
| if (proc.ExitCode != 0) | ||
| { | ||
| throw new Exception("Failed to set user secret"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,5 @@ | ||
| { | ||
| "Options": { | ||
| "Rate": 10 | ||
| }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @ChrisDoernen we did not need to remove anything or alter anything related to using secret values. Can you please undo this change? |
||
| "SecretValues": { | ||
| "Secret1": "StoreSecret1InUserSecrets", | ||
| "Secret2": "StoreSecret2InUserSecrets" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,12 +123,12 @@ public async ValueTask DisposeAsync() | |
| /// <summary> | ||
| /// Adds services to the service collection. Called once before building the provider. | ||
| /// </summary> | ||
| protected abstract void AddServices(IServiceCollection services, IConfiguration? configuration); | ||
| protected abstract void AddServices(IServiceCollection services, IConfiguration configuration); | ||
|
|
||
| /// <summary> | ||
| /// Returns the test application settings descriptors (JSON files) to include. | ||
| /// </summary> | ||
| protected abstract IEnumerable<TestAppSettings> GetTestAppSettings(); | ||
| protected virtual IEnumerable<TestAppSettings> GetTestAppSettings() => []; | ||
|
|
||
| /// <summary> | ||
| /// Override to asynchronously clean up resources created by the fixture. | ||
|
|
@@ -147,13 +147,10 @@ protected virtual ILoggingBuilder AddLoggingProvider(ILoggingBuilder loggingBuil | |
| /// </summary> | ||
| protected virtual void AddUserSecrets(IConfigurationBuilder configurationBuilder) { } | ||
|
|
||
| private IConfigurationRoot? GetConfigurationRoot() | ||
| private IConfigurationRoot GetConfigurationRoot() | ||
| { | ||
| var testAppSettings = GetTestAppSettings(); | ||
| return | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ChrisDoernen what is the reason for removing the condition? |
||
| testAppSettings.All(setting => !string.IsNullOrEmpty(setting.Filename)) | ||
| ? GetConfigurationRoot(testAppSettings) | ||
| : default; | ||
| return GetConfigurationRoot(testAppSettings); | ||
| } | ||
|
|
||
| private IConfigurationRoot GetConfigurationRoot(IEnumerable<TestAppSettings> configurationFiles) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChrisDoernen Can you please restore the original implementation of
ConfigurationTerstsclass and instead add a new configuration tests class to verifyTestProjectFixtureWithoutAppsettings's implementation?