Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
25 changes: 25 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<!-- Temporarily ignore warnings from dotnet target framework being out of support. When I decide to stop supporting older frameworks I need to remove this setting. For more info see https://andrewlock.net/fixing-build-warning-netsdk1138-when-building-end-of-life-projects-with-dotnet-5/ -->
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<!--
GenerateDocumentationFile is something I wanted enabled for the projects under /src that produce the NuGet packages.
For tests I thought I didn't need this enabled but without it I won't get warnings about 'unused usings' (IDE0005)
when building with the dotnet CLI, I'll only get them when using Visual Studio or when running dotnet format.

For more info see:
- Compiler does not report unused imports when XML doc comments are disabled: https://github.com/dotnet/roslyn/issues/41640
- And this comment for the first workaround presented: https://github.com/dotnet/roslyn/issues/41640#issuecomment-985780130

NOTE:
1) Even if GenerateDocumentationFile is not required anymore to get IDE0005 to work via the dotnet CLI, I found that I should still
have GenerateDocumentationFile enabled even for my test projects. This is because I've chosen to use to add a summary documentation
tag to all my tests to explain what they do and by enabling GenerateDocumentationFile I get some extra checks on what I write.
For instance, on build I get warned if I have incorrect cref values.

2) Hopefully, when https://github.com/dotnet/roslyn/issues/41640 gets closed, I can tidy up this huge comment block

3) Some of the proposed fixes also add some exclusions to NoWarn like: <NoWarn>$(NoWarn);CS1573;CS1591;CS1712</NoWarn>
but I've found I didn't need them, yet at least.

CS1573: Parameter 'parameter' has no matching param tag in the XML comment for 'parameter' (but other parameters do)
CS1591: Missing XML comment for publicly visible type or member 'Type_or_Member'
CS1712: Type parameter 'type_parameter' has no matching typeparam tag in the XML comment on 'type_or_member' (but other type parameters do)
-->
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"note2-about-version": "perhaps these errors will stop on a future version but I've tested 7.0.302 and was still getting errors.",
"note3-about-version": "perhaps these errors will also be gone when I stop supporting older TFMs.",
"note4-about-version": "when not needed anymore, delete all the note-about-version props.",
"version": "7.0.105",
"version": "7.0.400",
"rollForward": "disable",
"allowPrerelease": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class AddTestConfigurationHostTests
/// <summary>
/// Tests that <see cref="Host.CreateDefaultBuilder()"/> adds two <see cref="JsonConfigurationProvider"/>
/// to the app configuration.
/// This test serves as a control test because all the tests use the <see cref="WebHost.CreateDefaultBuilder()"/> as a way
/// This test serves as a control test because all the tests use the <see cref="Host.CreateDefaultBuilder()"/> as a way
/// to setup a <see cref="IWebHost"/> with several <see cref="ConfigurationProvider"/> and at least two <see cref="JsonConfigurationProvider"/>.
/// If this changes in the future then I could start having false positives on the other tests.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ namespace DotNet.Sdk.Extensions.Testing.Tests.Configuration;
[Trait("Category", XUnitCategories.Configuration)]
public class AddTestConfigurationWebHostTests
{
// I have to disable CS1574 because for some reason the cref to WebHost.CreateDefaultBuilder() only
// works if I have the full namespace:
// - works: <see cref="Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()"/>
// - doesn't work: <see cref="WebHost.CreateDefaultBuilder()"/>
//
// EVEN THOUGH I have a global using for Microsoft.AspNetCore. This might be some weird bug on the dotnet
// sdk or some analyzer.
//
// I need this ignored because otherwise my dotnet format GitHub workflow will attempt to remove the
// Microsoft.AspNetCore from the cref, which makes sense since I have the global using, but then
// the build fails because I get a CS1574 saying the <see cref="WebHost.CreateDefaultBuilder()"/> is
// invalid.
#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
/// <summary>
/// Tests that <see cref="WebHost.CreateDefaultBuilder()"/> adds two <see cref="JsonConfigurationProvider"/>
/// to the app configuration.
/// This test serves as a control test because all the tests use the <see cref="WebHost.CreateDefaultBuilder()"/> as a way
/// to setup a <see cref="IWebHost"/> with several <see cref="ConfigurationProvider"/> and at least two <see cref="JsonConfigurationProvider"/>.
/// If this changes in the future then I could start having false positives on the other tests.
/// </summary>
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
[Fact]
public void ControlTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Where2ValidateArguments()
}

/// <summary>
/// Validates the arguments for the <see cref="HttpResponseMessageMockBuilder.RespondWith(HttpResponseMessage)"/> method.
/// Validates the arguments for the <see cref="HttpResponseMessageMockBuilder.RespondWith(Func{HttpResponseMessage})"/> method.
/// </summary>
[Fact]
public void Respond1WithValidateArguments()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task RepliesAsConfigured2()
/// In linux this test fails with error:
/// System.Net.Http.HttpRequestException : The SSL connection could not be established, see inner exception.
/// because dev certificate does not exist
/// For more info on how to resolve this issue see: https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-5.0&tabs=visual-studio#ssl-linux.
/// For more info on how to resolve this issue see: https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl#ssl-linux.
/// </remarks>
[Fact]
public async Task RepliesAsConfigured()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Options\AddOptionsValue\Auxiliary\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace DotNet.Sdk.Extensions.Tests.Options;
using Microsoft.AspNetCore;

namespace DotNet.Sdk.Extensions.Tests.Options.AddOptionsValue;

[Trait("Category", XUnitCategories.Options)]
public class AddOptionsValueTests
Expand All @@ -14,7 +16,7 @@ public void AddsOptionsType1()
var serviceCollection = new ServiceCollection();
serviceCollection.AddOptionsValue<MyOptions>(configuration);
var serviceProvider = serviceCollection.BuildServiceProvider();
var myOptions = serviceProvider.GetRequiredService<MyOptions>();
var myOptions = serviceProvider.GetService<MyOptions>();
myOptions.ShouldNotBeNull();
}

Expand Down Expand Up @@ -93,7 +95,7 @@ public void AddsOptionsType3()
.Bind(configuration)
.AddOptionsValue();
var serviceProvider = serviceCollection.BuildServiceProvider();
var myOptions = serviceProvider.GetRequiredService<MyOptions>();
var myOptions = serviceProvider.GetService<MyOptions>();
myOptions.ShouldNotBeNull();
}

Expand All @@ -111,6 +113,39 @@ public void ValidatesArguments3()
optionsBuilderArgumentNullException.Message.ShouldBe("Value cannot be null. (Parameter 'optionsBuilder')");
}

[Fact]
public void AddsOptionsType4()
{
using var webHost = WebHost
.CreateDefaultBuilder()
.Configure((_, _) =>
{
// this is required just to provide a configuration for the webhost
// or else it fails when calling webHostBuilder.Build()
})
.ConfigureAppConfiguration(config =>
{
var memoryConfigurationSource = new MemoryConfigurationSource
{
InitialData = new List<KeyValuePair<string, string?>>
{
new KeyValuePair<string, string?>("MyOptionsSection:SomeOption", "original-value"),
},
};
config.Add(memoryConfigurationSource);
})
.ConfigureServices((context, services) =>
{
var namedConfigSection = context.Configuration.GetSection("MyOptionsSection");
services.Configure<MyOptions>(context.Configuration.GetSection("MyOptionsSection"));
})
.UseConfigurationValue("MyOptionsSection:SomeOption", "test-value")
.Build();

var myOptions = webHost.Services.GetService<IOptions<MyOptions>>();
myOptions.ShouldNotBeNull();
}

private sealed class MyOptions
{
public string? SomeOption { get; set; }
Expand Down