-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add comprehensive unit tests for Microsoft.Extensions.Hosting projects #120632
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
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/add-unit-tests-hosting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+595
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
03775ec
Initial plan
Copilot bc1acbe
Add unit tests for HostOptions, ConsoleLifetimeOptions, HostApplicati…
Copilot b70c952
Add unit tests for Microsoft.Extensions.Hosting.Abstractions
Copilot a209475
Address PR feedback: consolidate redundant tests and use AssertExtens…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/libraries/Microsoft.Extensions.Hosting.Abstractions/tests/EnvironmentsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Hosting.Tests | ||
{ | ||
public class EnvironmentsTests | ||
{ | ||
[Fact] | ||
public void EnvironmentConstants_HaveExpectedValues() | ||
{ | ||
Assert.Equal("Development", Environments.Development); | ||
Assert.Equal("Staging", Environments.Staging); | ||
Assert.Equal("Production", Environments.Production); | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
src/libraries/Microsoft.Extensions.Hosting.Abstractions/tests/HostBuilderContextTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.FileProviders; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Hosting.Tests | ||
{ | ||
using AssertExtensions = System.AssertExtensions; | ||
|
||
public class HostBuilderContextTests | ||
{ | ||
[Fact] | ||
public void Constructor_WithProperties_InitializesProperties() | ||
{ | ||
var properties = new Dictionary<object, object>(); | ||
var context = new HostBuilderContext(properties); | ||
|
||
Assert.Same(properties, context.Properties); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_WithNullProperties_ThrowsArgumentNullException() | ||
{ | ||
AssertExtensions.Throws<ArgumentNullException>("properties", () => new HostBuilderContext(null)); | ||
} | ||
|
||
[Fact] | ||
public void HostingEnvironment_CanBeSet() | ||
{ | ||
var properties = new Dictionary<object, object>(); | ||
var context = new HostBuilderContext(properties); | ||
var environment = new TestHostEnvironment(); | ||
|
||
context.HostingEnvironment = environment; | ||
|
||
Assert.Same(environment, context.HostingEnvironment); | ||
} | ||
|
||
[Fact] | ||
public void Configuration_CanBeSet() | ||
{ | ||
var properties = new Dictionary<object, object>(); | ||
var context = new HostBuilderContext(properties); | ||
var configuration = new ConfigurationBuilder().Build(); | ||
|
||
context.Configuration = configuration; | ||
|
||
Assert.Same(configuration, context.Configuration); | ||
} | ||
|
||
[Fact] | ||
public void Properties_CanBeModified() | ||
{ | ||
var properties = new Dictionary<object, object>(); | ||
var context = new HostBuilderContext(properties); | ||
|
||
context.Properties["key1"] = "value1"; | ||
context.Properties["key2"] = 42; | ||
|
||
Assert.Equal("value1", context.Properties["key1"]); | ||
Assert.Equal(42, context.Properties["key2"]); | ||
} | ||
|
||
[Fact] | ||
public void Properties_SharedWithConstructorDictionary() | ||
{ | ||
var properties = new Dictionary<object, object> | ||
{ | ||
["existing"] = "value" | ||
}; | ||
var context = new HostBuilderContext(properties); | ||
|
||
properties["new"] = "added"; | ||
|
||
Assert.Equal("added", context.Properties["new"]); | ||
} | ||
|
||
[Fact] | ||
public void AllProperties_CanBeSetTogether() | ||
{ | ||
var properties = new Dictionary<object, object>(); | ||
var context = new HostBuilderContext(properties) | ||
{ | ||
HostingEnvironment = new TestHostEnvironment(), | ||
Configuration = new ConfigurationBuilder().Build() | ||
}; | ||
|
||
Assert.NotNull(context.HostingEnvironment); | ||
Assert.NotNull(context.Configuration); | ||
Assert.Same(properties, context.Properties); | ||
} | ||
|
||
private class TestHostEnvironment : IHostEnvironment | ||
{ | ||
public string EnvironmentName { get; set; } = string.Empty; | ||
public string ApplicationName { get; set; } = string.Empty; | ||
public string ContentRootPath { get; set; } = string.Empty; | ||
public IFileProvider ContentRootFileProvider { get; set; } = null!; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/libraries/Microsoft.Extensions.Hosting.Abstractions/tests/HostDefaultsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Hosting.Tests | ||
{ | ||
public class HostDefaultsTests | ||
{ | ||
[Fact] | ||
public void KeyConstants_HaveExpectedValues() | ||
{ | ||
Assert.Equal("applicationName", HostDefaults.ApplicationKey); | ||
Assert.Equal("environment", HostDefaults.EnvironmentKey); | ||
Assert.Equal("contentRoot", HostDefaults.ContentRootKey); | ||
} | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
...ries/Microsoft.Extensions.Hosting.Abstractions/tests/HostEnvironmentEnvExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using Microsoft.Extensions.FileProviders; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Hosting.Tests | ||
{ | ||
public class HostEnvironmentEnvExtensionsTests | ||
{ | ||
[Fact] | ||
public void IsDevelopment_WithDevelopmentEnvironment_ReturnsTrue() | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = Environments.Development }; | ||
|
||
Assert.True(environment.IsDevelopment()); | ||
} | ||
|
||
[Fact] | ||
public void IsDevelopment_WithNonDevelopmentEnvironment_ReturnsFalse() | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = Environments.Production }; | ||
|
||
Assert.False(environment.IsDevelopment()); | ||
} | ||
|
||
[Fact] | ||
public void IsDevelopment_WithNullEnvironment_ThrowsArgumentNullException() | ||
{ | ||
IHostEnvironment environment = null; | ||
|
||
Assert.Throws<ArgumentNullException>(() => environment.IsDevelopment()); | ||
} | ||
|
||
[Fact] | ||
public void IsStaging_WithStagingEnvironment_ReturnsTrue() | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = Environments.Staging }; | ||
|
||
Assert.True(environment.IsStaging()); | ||
} | ||
|
||
[Fact] | ||
public void IsStaging_WithNonStagingEnvironment_ReturnsFalse() | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = Environments.Production }; | ||
|
||
Assert.False(environment.IsStaging()); | ||
} | ||
|
||
[Fact] | ||
public void IsStaging_WithNullEnvironment_ThrowsArgumentNullException() | ||
{ | ||
IHostEnvironment environment = null; | ||
|
||
Assert.Throws<ArgumentNullException>(() => environment.IsStaging()); | ||
} | ||
|
||
[Fact] | ||
public void IsProduction_WithProductionEnvironment_ReturnsTrue() | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = Environments.Production }; | ||
|
||
Assert.True(environment.IsProduction()); | ||
} | ||
|
||
[Fact] | ||
public void IsProduction_WithNonProductionEnvironment_ReturnsFalse() | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = Environments.Development }; | ||
|
||
Assert.False(environment.IsProduction()); | ||
} | ||
|
||
[Fact] | ||
public void IsProduction_WithNullEnvironment_ThrowsArgumentNullException() | ||
{ | ||
IHostEnvironment environment = null; | ||
|
||
Assert.Throws<ArgumentNullException>(() => environment.IsProduction()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Development", "Development", true)] | ||
[InlineData("Development", "development", true)] | ||
[InlineData("Development", "DEVELOPMENT", true)] | ||
[InlineData("Development", "Production", false)] | ||
[InlineData("Production", "Production", true)] | ||
[InlineData("Staging", "staging", true)] | ||
[InlineData("Custom", "Custom", true)] | ||
[InlineData("Custom", "custom", true)] | ||
public void IsEnvironment_ComparesEnvironmentNameCaseInsensitive(string actualEnvironment, string testEnvironment, bool expected) | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = actualEnvironment }; | ||
|
||
Assert.Equal(expected, environment.IsEnvironment(testEnvironment)); | ||
} | ||
|
||
[Fact] | ||
public void IsEnvironment_WithNullEnvironment_ThrowsArgumentNullException() | ||
{ | ||
IHostEnvironment environment = null; | ||
|
||
Assert.Throws<ArgumentNullException>(() => environment.IsEnvironment("Development")); | ||
} | ||
|
||
[Fact] | ||
public void IsEnvironment_WithEmptyString_ReturnsFalseForNonEmptyEnvironmentName() | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = "Development" }; | ||
|
||
Assert.False(environment.IsEnvironment(string.Empty)); | ||
} | ||
|
||
[Fact] | ||
public void IsEnvironment_WithEmptyEnvironmentName_ReturnsTrueForEmptyString() | ||
{ | ||
var environment = new TestHostEnvironment { EnvironmentName = string.Empty }; | ||
|
||
Assert.True(environment.IsEnvironment(string.Empty)); | ||
} | ||
|
||
[Fact] | ||
public void EnvironmentChecks_AreCaseInsensitive() | ||
{ | ||
var devEnv = new TestHostEnvironment { EnvironmentName = "development" }; | ||
var stagingEnv = new TestHostEnvironment { EnvironmentName = "STAGING" }; | ||
var prodEnv = new TestHostEnvironment { EnvironmentName = "PrOdUcTiOn" }; | ||
|
||
Assert.True(devEnv.IsDevelopment()); | ||
Assert.True(stagingEnv.IsStaging()); | ||
Assert.True(prodEnv.IsProduction()); | ||
} | ||
|
||
private class TestHostEnvironment : IHostEnvironment | ||
{ | ||
public string EnvironmentName { get; set; } = string.Empty; | ||
public string ApplicationName { get; set; } = string.Empty; | ||
public string ContentRootPath { get; set; } = string.Empty; | ||
public IFileProvider ContentRootFileProvider { get; set; } = null!; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ensions.Hosting.Abstractions/tests/Microsoft.Extensions.Hosting.Abstractions.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks> | ||
<EnableDefaultItems>true</EnableDefaultItems> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Hosting.Abstractions\src\Microsoft.Extensions.Hosting.Abstractions.csproj" /> | ||
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration\src\Microsoft.Extensions.Configuration.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
...s/Microsoft.Extensions.Hosting/tests/UnitTests/BackgroundServiceExceptionBehaviorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Hosting.Tests | ||
{ | ||
public class BackgroundServiceExceptionBehaviorTests | ||
{ | ||
[Fact] | ||
public void EnumValues_HaveExpectedValues() | ||
{ | ||
Assert.Equal(0, (int)BackgroundServiceExceptionBehavior.StopHost); | ||
Assert.Equal(1, (int)BackgroundServiceExceptionBehavior.Ignore); | ||
} | ||
|
||
[Fact] | ||
public void CanCompareValues() | ||
{ | ||
var stopHost = BackgroundServiceExceptionBehavior.StopHost; | ||
var ignore = BackgroundServiceExceptionBehavior.Ignore; | ||
|
||
Assert.True(stopHost == BackgroundServiceExceptionBehavior.StopHost); | ||
Assert.True(ignore == BackgroundServiceExceptionBehavior.Ignore); | ||
Assert.False(stopHost == ignore); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/ConsoleLifetimeOptionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Hosting.Tests | ||
{ | ||
public class ConsoleLifetimeOptionsTests | ||
{ | ||
[Fact] | ||
public void SuppressStatusMessages_CanBeToggled() | ||
{ | ||
var options = new ConsoleLifetimeOptions(); | ||
Assert.False(options.SuppressStatusMessages); | ||
|
||
options.SuppressStatusMessages = true; | ||
Assert.True(options.SuppressStatusMessages); | ||
|
||
options.SuppressStatusMessages = false; | ||
Assert.False(options.SuppressStatusMessages); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.