Skip to content

Commit 2d1aa7e

Browse files
committed
Move config file to jsonc, make config class nullable
1 parent 025d441 commit 2d1aa7e

4 files changed

Lines changed: 73 additions & 49 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ MigrationBackup/
370370

371371
# Config Json file
372372
**/config.json
373+
**/config.jsonc
373374

374375
# Generated Milestone PR metadata files
375376
.milestone-prs/

src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Config.cs

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,63 @@
66
using System.IO;
77
using Newtonsoft.Json;
88

9+
#nullable enable
10+
911
namespace Microsoft.Data.SqlClient.TestUtilities
1012
{
1113
public class Config
1214
{
13-
public string TCPConnectionString = null;
14-
public string NPConnectionString = null;
15-
public string TCPConnectionStringHGSVBS = null;
16-
public string TCPConnectionStringNoneVBS = null;
17-
public string TCPConnectionStringAASSGX = null;
15+
public string? TCPConnectionString = null;
16+
public string? NPConnectionString = null;
17+
public string? TCPConnectionStringHGSVBS = null;
18+
public string? TCPConnectionStringNoneVBS = null;
19+
public string? TCPConnectionStringAASSGX = null;
1820
public bool EnclaveEnabled = false;
1921
public bool TracingEnabled = false;
20-
public string AADAuthorityURL = null;
21-
public string AADPasswordConnectionString = null;
22-
public string AADServicePrincipalId = null;
23-
public string AADServicePrincipalSecret = null;
24-
public string AzureKeyVaultURL = null;
25-
public string AzureKeyVaultTenantId = null;
22+
public string? AADAuthorityURL = null;
23+
public string? AADPasswordConnectionString = null;
24+
public string? AADServicePrincipalId = null;
25+
public string? AADServicePrincipalSecret = null;
26+
public string? AzureKeyVaultURL = null;
27+
public string? AzureKeyVaultTenantId = null;
2628
public bool SupportsIntegratedSecurity = false;
27-
public string LocalDbAppName = null;
28-
public string LocalDbSharedInstanceName = null;
29-
public string FileStreamDirectory = null;
29+
public string? LocalDbAppName = null;
30+
public string? LocalDbSharedInstanceName = null;
31+
public string? FileStreamDirectory = null;
3032
public bool UseManagedSNIOnWindows = false;
31-
public string DNSCachingConnString = null;
32-
public string DNSCachingServerCR = null; // this is for the control ring
33-
public string DNSCachingServerTR = null; // this is for the tenant ring
33+
public string? DNSCachingConnString = null;
34+
public string? DNSCachingServerCR = null; // this is for the control ring
35+
public string? DNSCachingServerTR = null; // this is for the tenant ring
3436
public bool IsDNSCachingSupportedCR = false; // this is for the control ring
3537
public bool IsDNSCachingSupportedTR = false; // this is for the tenant ring
36-
public string EnclaveAzureDatabaseConnString = null;
38+
public string? EnclaveAzureDatabaseConnString = null;
3739
public bool ManagedIdentitySupported = true;
38-
public string UserManagedIdentityClientId = null;
39-
public string PowerShellPath = null;
40-
public string AliasName = null;
41-
public string KerberosDomainPassword = null;
42-
public string KerberosDomainUser = null;
40+
public string? UserManagedIdentityClientId = null;
41+
public string? PowerShellPath = null;
42+
public string? AliasName = null;
43+
public string? KerberosDomainPassword = null;
44+
public string? KerberosDomainUser = null;
4345
public bool IsManagedInstance = false;
4446

45-
public static Config Load(string configPath = @"config.json")
47+
public static Config Load(string configPath)
4648
{
47-
// Allow an override of the config path via an environment variable.
48-
configPath = Environment.GetEnvironmentVariable("TEST_MDS_CONFIG") ?? configPath;
49+
Config config = LoadInternal(Environment.GetEnvironmentVariable("TEST_MDS_CONFIG")) ??
50+
LoadInternal(configPath) ??
51+
throw new FileNotFoundException("Could not find test configuration file.");
4952

50-
Config config;
51-
try
52-
{
53-
using (StreamReader r = new StreamReader(configPath))
54-
{
55-
config = JsonConvert.DeserializeObject<Config>(r.ReadToEnd())
56-
?? throw new InvalidOperationException($"Failed to deserialize config from '{configPath}'.");
57-
}
58-
}
59-
catch
60-
{
61-
throw;
62-
}
53+
// Allow environment variables to override individual config values.
54+
SetFromEnv("MDS_TCPConnectionString", ref config.TCPConnectionString);
6355

64-
static void SetFromEnv(string envVar, ref string configValue)
65-
{
66-
string envValue = Environment.GetEnvironmentVariable(envVar);
67-
if (!string.IsNullOrEmpty(envValue))
68-
{
69-
configValue = envValue;
70-
}
71-
}
56+
return config;
57+
}
58+
59+
public static Config Load()
60+
{
61+
// Load config from environment variable first, jsonc file second, json file last.
62+
Config config = LoadInternal(Environment.GetEnvironmentVariable("TEST_MDS_CONFIG")) ??
63+
LoadInternal("config.jsonc") ??
64+
LoadInternal("config.json") ??
65+
throw new FileNotFoundException("Could not find test configuration file.");
7266

7367
// Allow environment variables to override individual config values.
7468
SetFromEnv("MDS_TCPConnectionString", ref config.TCPConnectionString);
@@ -81,5 +75,34 @@ public static void UpdateConfig(Config updatedConfig, string configPath = @"conf
8175
string config = JsonConvert.SerializeObject(updatedConfig);
8276
File.WriteAllText(configPath, config);
8377
}
78+
79+
private static Config? LoadInternal(string? configPath)
80+
{
81+
if (configPath is null)
82+
{
83+
return null;
84+
}
85+
86+
try
87+
{
88+
using StreamReader sr = new StreamReader(configPath);
89+
return JsonConvert.DeserializeObject<Config>(sr.ReadToEnd()) ??
90+
throw new InvalidOperationException($"Failed to deserialize config from '{configPath}'");
91+
}
92+
catch (FileNotFoundException)
93+
{
94+
// File did not exist at the path given. We will try a different location.
95+
return null;
96+
}
97+
}
98+
99+
private static void SetFromEnv(string envVar, ref string? configValue)
100+
{
101+
string? envValue = Environment.GetEnvironmentVariable(envVar);
102+
if (!string.IsNullOrEmpty(envValue))
103+
{
104+
configValue = envValue;
105+
}
106+
}
84107
}
85108
}

src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Microsoft.Data.SqlClient.TestUtilities.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
</PropertyGroup>
55
<Target Name="CopyConfig" BeforeTargets="Compile">
6-
<Copy SourceFiles="config.default.json" DestinationFiles="config.json" Condition="!Exists('config.json')" />
6+
<Copy SourceFiles="config.default.jsonc" DestinationFiles="config.jsonc" Condition="!Exists('config.jsonc')" />
77
</Target>
88
<ItemGroup>
9-
<None Update="config.json">
9+
<None Update="config.jsonc">
1010
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1111
</None>
1212
<PackageReference Include="Newtonsoft.Json" />

src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/config.default.json renamed to src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/config.default.jsonc

File renamed without changes.

0 commit comments

Comments
 (0)