Skip to content

Commit 2bd0a13

Browse files
Hot fix merge config (#1507)
## Why make this change? - To include the fix for the issue with merge config issue. #1493 - Also include the fix for broken schema links/path in test configs ## What is this change? - cherry-picking fix for bugs found in 0.7 --------- Co-authored-by: Shyam Sundar J <shyamsundarj@microsoft.com>
1 parent 2f938a6 commit 2bd0a13

File tree

4 files changed

+75
-8
lines changed

4 files changed

+75
-8
lines changed

src/Cli.Tests/UtilsTests.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public void TestMergeConfig()
222222
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, "Test");
223223
File.WriteAllText("dab-config.json", BASE_CONFIG);
224224
File.WriteAllText("dab-config.Test.json", ENV_BASED_CONFIG);
225-
if (TryMergeConfigsIfAvailable(out string mergedConfig))
225+
if (TryMergeConfigsIfAvailable(out string? mergedConfig))
226226
{
227227
Assert.AreEqual(mergedConfig, "dab-config.Test.merged.json");
228228
Assert.IsTrue(File.Exists(mergedConfig));
@@ -234,6 +234,69 @@ public void TestMergeConfig()
234234
}
235235
}
236236

237+
/// <summary>
238+
/// Test to verify that merged config file is only used for the below scenario
239+
/// 1. Environment value is set.
240+
/// 2. Both Base and envBased config file is present.
241+
/// In all other cases, the TryMergeConfigsIfAvailable method should return false
242+
/// and out param for the mergedConfigFile should be null.
243+
/// </summary>
244+
[DataTestMethod]
245+
[DataRow("", false, false, null, false, DisplayName = "If environment value is not set, merged config file is not generated.")]
246+
[DataRow("", false, true, null, false, DisplayName = "If environment value is not set, merged config file is not generated.")]
247+
[DataRow("", true, false, null, false, DisplayName = "If environment value is not set, merged config file is not generated.")]
248+
[DataRow("", true, true, null, false, DisplayName = "If environment value is not set, merged config file is not generated.")]
249+
[DataRow(null, false, false, null, false, DisplayName = "If environment variable is removed, merged config file is not generated.")]
250+
[DataRow(null, false, true, null, false, DisplayName = "If environment variable is removed, merged config file is not generated.")]
251+
[DataRow(null, true, false, null, false, DisplayName = "If environment variable is removed, merged config file is not generated.")]
252+
[DataRow(null, true, true, null, false, DisplayName = "If environment variable is removed, merged config file is not generated.")]
253+
[DataRow("Test", false, false, null, false, DisplayName = "Environment value set but base config not available, merged config file is not generated.")]
254+
[DataRow("Test", false, true, null, false, DisplayName = "Environment value set but base config not available, merged config file is not generated.")]
255+
[DataRow("Test", true, false, null, false, DisplayName = "Environment value set but env based config not available, merged config file is not generated.")]
256+
[DataRow("Test", true, true, "dab-config.Test.merged.json", true, DisplayName = "Environment value set and both base and envConfig available, merged config file is generated.")]
257+
public void TestMergeConfigAvailability(
258+
string? environmentValue,
259+
bool isBaseConfigPresent,
260+
bool isEnvironmentBasedConfigPresent,
261+
string? expectedMergedConfigFileName,
262+
bool expectedIsMergedConfigAvailable)
263+
{
264+
// Setting up the test scenarios
265+
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
266+
string baseConfig = "dab-config.json";
267+
string envBasedConfig = "dab-config.Test.json";
268+
if (File.Exists(baseConfig))
269+
{
270+
File.Delete(baseConfig);
271+
}
272+
273+
if (File.Exists(envBasedConfig))
274+
{
275+
File.Delete(envBasedConfig);
276+
}
277+
278+
if (isBaseConfigPresent)
279+
{
280+
if (!File.Exists(baseConfig))
281+
{
282+
File.Create(baseConfig).Close();
283+
File.WriteAllText(baseConfig, "{}");
284+
}
285+
}
286+
287+
if (isEnvironmentBasedConfigPresent)
288+
{
289+
if (!File.Exists(envBasedConfig))
290+
{
291+
File.Create(envBasedConfig).Close();
292+
File.WriteAllText(envBasedConfig, "{}");
293+
}
294+
}
295+
296+
Assert.AreEqual(expectedIsMergedConfigAvailable, TryMergeConfigsIfAvailable(out string? mergedConfigFile));
297+
Assert.AreEqual(expectedMergedConfigFileName, mergedConfigFile);
298+
}
299+
237300
[ClassCleanup]
238301
public static void Cleanup()
239302
{

src/Cli/Utils.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -889,31 +889,35 @@ public static bool WriteJsonContentToFile(string file, string jsonContent)
889889
/// If yes, it will try to merge dab-config.json with dab-config.{DAB_ENVIRONMENT}.json
890890
/// and create a merged file called dab-config.{DAB_ENVIRONMENT}.merged.json
891891
/// </summary>
892-
/// <returns>Returns the name of the merged Config if successful.</returns>
893-
public static bool TryMergeConfigsIfAvailable(out string mergedConfigFile)
892+
/// <returns>Returns the name of the merged config if successful.</returns>
893+
public static bool TryMergeConfigsIfAvailable([NotNullWhen(true)] out string? mergedConfigFile)
894894
{
895895
string? environmentValue = Environment.GetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME);
896-
mergedConfigFile = string.Empty;
896+
mergedConfigFile = null;
897897
if (!string.IsNullOrEmpty(environmentValue))
898898
{
899899
string baseConfigFile = RuntimeConfigPath.DefaultName;
900900
string environmentBasedConfigFile = RuntimeConfigPath.GetFileName(environmentValue, considerOverrides: false);
901-
mergedConfigFile = RuntimeConfigPath.GetMergedFileNameForEnvironment(CONFIGFILE_NAME, environmentValue);
902901

903902
if (DoesFileExistInCurrentDirectory(baseConfigFile) && !string.IsNullOrEmpty(environmentBasedConfigFile))
904903
{
905904
try
906905
{
907906
string baseConfigJson = File.ReadAllText(baseConfigFile);
908907
string overrideConfigJson = File.ReadAllText(environmentBasedConfigFile);
908+
string currentDir = Directory.GetCurrentDirectory();
909+
_logger.LogInformation($"Merging {Path.Combine(currentDir, baseConfigFile)}"
910+
+ $" and {Path.Combine(currentDir, environmentBasedConfigFile)}");
909911
string mergedConfigJson = Merge(baseConfigJson, overrideConfigJson);
910-
912+
mergedConfigFile = RuntimeConfigPath.GetMergedFileNameForEnvironment(CONFIGFILE_NAME, environmentValue);
911913
File.WriteAllText(mergedConfigFile, mergedConfigJson);
914+
_logger.LogInformation($"Generated merged config file: {Path.Combine(currentDir, mergedConfigFile)}");
912915
return true;
913916
}
914917
catch (Exception ex)
915918
{
916919
_logger.LogError(ex, $"Failed to merge the config files.");
920+
mergedConfigFile = null;
917921
return false;
918922
}
919923
}

src/Service.Tests/dab-config.MsSql.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "https://dataapibuilder.azureedge.net/schemas/vmajor.minor.patch-beta/dab.draft.schema.json",
2+
"$schema": "../../schemas/dab.draft.schema.json",
33
"data-source": {
44
"database-type": "mssql",
55
"options": {

src/Service.Tests/dab-config.MySql.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "../../../schemas/dab.draft.schema.json",
2+
"$schema": "../../schemas/dab.draft.schema.json",
33
"data-source": {
44
"database-type": "mysql",
55
"connection-string": "server=localhost;database=dab;Allow User Variables=true;uid=root;pwd=REPLACEME"

0 commit comments

Comments
 (0)