// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.Extensions.Configuration;
namespace AgentConformance.IntegrationTests.Support;
///
/// Helper for loading test configuration settings.
///
public sealed class TestConfiguration
{
private static readonly IConfiguration s_configuration = new ConfigurationBuilder()
.AddJsonFile(path: "testsettings.json", optional: true)
.AddJsonFile(path: "testsettings.development.json", optional: true)
.AddEnvironmentVariables()
.AddUserSecrets()
.Build();
///
/// Loads the type of configuration using a section name based on the type name.
///
/// The type of config to load.
/// The loaded configuration section of the specified type.
/// Thrown if the configuration section cannot be loaded.
public static T LoadSection()
{
var configType = typeof(T);
var configTypeName = configType.Name;
const string TrimText = "Configuration";
if (configTypeName.EndsWith(TrimText, StringComparison.OrdinalIgnoreCase))
{
configTypeName = configTypeName.Substring(0, configTypeName.Length - TrimText.Length);
}
return s_configuration.GetRequiredSection(configTypeName).Get() ??
throw new InvalidOperationException($"Could not load config for {configTypeName}.");
}
}