Skip to content

Commit 071db0e

Browse files
committed
fix tests
1 parent 16271e3 commit 071db0e

3 files changed

Lines changed: 47 additions & 40 deletions

File tree

src/Config/ObjectModel/RuntimeConfig.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,14 @@ public RuntimeConfig(
380380
e.InnerException);
381381
}
382382
}
383-
else
383+
else if (fileSystem.File.Exists(dataSourceFile))
384384
{
385+
// The file exists but failed to load (e.g. invalid JSON, deserialization error).
386+
// Throw to prevent silently skipping a broken child config.
387+
// Non-existent files are skipped gracefully to support late-configured scenarios
388+
// where data-source-files may reference files not present on the host.
385389
throw new DataApiBuilderException(
386-
message: $"Failed to load datasource file: {dataSourceFile}. The file may not exist, contain invalid JSON, or have other configuration errors.",
390+
message: $"Failed to load datasource file: {dataSourceFile}. The file exists but contains invalid JSON or has other configuration errors.",
387391
statusCode: HttpStatusCode.ServiceUnavailable,
388392
subStatusCode: DataApiBuilderException.SubStatusCodes.ConfigValidationError);
389393
}

src/Service.Tests/Configuration/RuntimeConfigLoaderTests.cs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,42 +227,57 @@ public async Task ChildConfigWithMissingEnvVarsLoadsSuccessfully()
227227
}
228228

229229
/// <summary>
230-
/// Validates that when a child config file cannot be loaded (e.g. file not found, invalid JSON),
230+
/// Validates that when a child config file exists but contains invalid content,
231231
/// the parent config loading fails instead of silently skipping the child.
232+
/// Non-existent child files are intentionally skipped to support late-configured scenarios.
232233
/// Regression test for https://github.com/Azure/data-api-builder/issues/3271
233234
/// </summary>
234235
[TestMethod]
235236
public async Task ChildConfigLoadFailureHaltsParentConfigLoading()
236237
{
237238
string parentConfig = await File.ReadAllTextAsync("Multidab-config.MsSql.json");
238239

239-
JObject parentJson = JObject.Parse(parentConfig);
240-
parentJson.Add("data-source-files", new JArray("nonexistent-child.json"));
241-
string parentJsonStr = parentJson.ToString();
240+
// Use a real temp file with invalid JSON so the file exists but fails to parse.
241+
string invalidChildPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".json");
242242

243-
MockFileSystem fs = new(new Dictionary<string, MockFileData>()
243+
try
244244
{
245-
{ "dab-config.json", new MockFileData(parentJsonStr) }
246-
// nonexistent-child.json intentionally NOT added to the mock file system
247-
});
245+
await File.WriteAllTextAsync(invalidChildPath, "{ this is not valid json }");
248246

249-
FileSystemRuntimeConfigLoader loader = new(fs);
247+
JObject parentJson = JObject.Parse(parentConfig);
248+
parentJson.Add("data-source-files", new JArray(invalidChildPath));
249+
string parentJsonStr = parentJson.ToString();
250250

251-
TextWriter originalError = Console.Error;
252-
StringWriter sw = new();
253-
Console.SetError(sw);
251+
MockFileSystem fs = new(new Dictionary<string, MockFileData>()
252+
{
253+
{ "dab-config.json", new MockFileData(parentJsonStr) }
254+
});
254255

255-
try
256-
{
257-
bool loaded = loader.TryLoadConfig("dab-config.json", out RuntimeConfig _);
258-
string error = sw.ToString();
256+
FileSystemRuntimeConfigLoader loader = new(fs);
257+
258+
TextWriter originalError = Console.Error;
259+
StringWriter sw = new();
260+
Console.SetError(sw);
261+
262+
try
263+
{
264+
bool loaded = loader.TryLoadConfig("dab-config.json", out RuntimeConfig _);
265+
string error = sw.ToString();
259266

260-
Assert.IsFalse(loaded, "Config loading should fail when a child config file cannot be loaded.");
261-
Assert.IsTrue(error.Contains("Failed to load datasource file"), "Error message should indicate the child config file that failed to load.");
267+
Assert.IsFalse(loaded, "Config loading should fail when a child config file exists but cannot be parsed.");
268+
Assert.IsTrue(error.Contains("Failed to load datasource file"), "Error message should indicate the child config file that failed to load.");
269+
}
270+
finally
271+
{
272+
Console.SetError(originalError);
273+
}
262274
}
263275
finally
264276
{
265-
Console.SetError(originalError);
277+
if (File.Exists(invalidChildPath))
278+
{
279+
File.Delete(invalidChildPath);
280+
}
266281
}
267282
}
268283
}

src/Service.Tests/UnitTests/RuntimeConfigLoaderJsonDeserializerTests.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Data;
77
using System.IO;
88
using System.IO.Abstractions.TestingHelpers;
9+
using System.Linq;
910
using System.Text;
1011
using System.Text.Json;
1112
using Azure.DataApiBuilder.Config;
@@ -485,10 +486,10 @@ public void TestLoadRuntimeConfigFailures(
485486
}
486487

487488
/// <summary>
488-
/// Method to validate that config loading fails when a sub-data source file is not found.
489-
/// Previously this test asserted that the missing child was silently skipped;
490-
/// after the fix for https://github.com/Azure/data-api-builder/issues/3271,
491-
/// a missing child config now correctly causes the parent config to fail loading.
489+
/// Method to validate that when a sub-data source file is not found, it is gracefully
490+
/// skipped and the parent config loads successfully. Non-existent child files are
491+
/// tolerated to support late-configured scenarios where data-source-files may reference
492+
/// files not present on the host.
492493
/// </summary>
493494
[TestMethod]
494495
public void TestLoadRuntimeConfigSubFilesFails()
@@ -508,21 +509,8 @@ public void TestLoadRuntimeConfigSubFilesFails()
508509
""data-source-files"":[""FileNotFound.json""],
509510
""entities"":{ }
510511
}";
511-
512-
TextWriter originalError = Console.Error;
513-
StringWriter sw = new();
514-
Console.SetError(sw);
515-
516-
try
517-
{
518-
Assert.IsFalse(RuntimeConfigLoader.TryParseConfig(actualJson, out RuntimeConfig _), "Config loading should fail when a child config file cannot be found.");
519-
string error = sw.ToString();
520-
Assert.IsTrue(error.Contains("Failed to load datasource file"), "Error message should indicate the child config file that failed.");
521-
}
522-
finally
523-
{
524-
Console.SetError(originalError);
525-
}
512+
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(actualJson, out RuntimeConfig runtimeConfig), "Should parse the data-source-files correctly.");
513+
Assert.IsTrue(runtimeConfig.ListAllDataSources().Count() == 1);
526514
}
527515

528516
#endregion Negative Tests

0 commit comments

Comments
 (0)