Skip to content
Open
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
@@ -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);
}
}
}
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!;
}
}
}
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);
}
}
}
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!;
}
}
}
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>
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);
}
}
}
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);
}
}
}
Loading
Loading