Skip to content

Commit 2229b5b

Browse files
CopilotJusterZhu
andcommitted
Add tests for JSON configuration file loading functionality
Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
1 parent 6ed754d commit 2229b5b

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

src/c#/CoreTest/Shared/ConfiginfoBuilderTests.cs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Runtime.InteropServices;
45
using GeneralUpdate.Common.Shared.Object;
56
using Xunit;
@@ -705,5 +706,121 @@ public void CompleteScenario_BuildsValidConfiginfo()
705706
}
706707

707708
#endregion
709+
710+
#region JSON Configuration File Tests
711+
712+
/// <summary>
713+
/// Tests that ConfiginfoBuilder loads configuration from update_config.json file if present.
714+
/// </summary>
715+
[Fact]
716+
public void Create_WithConfigFile_LoadsFromFile()
717+
{
718+
// Arrange - Create a test config file
719+
var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "update_config.json");
720+
var testConfig = new
721+
{
722+
UpdateUrl = "https://config-file.example.com/updates",
723+
Token = "config-file-token",
724+
Scheme = "https",
725+
AppName = "ConfigFileApp.exe",
726+
MainAppName = "ConfigFileMain.exe",
727+
ClientVersion = "9.9.9",
728+
InstallPath = "/config/file/path"
729+
};
730+
731+
try
732+
{
733+
// Write test config file
734+
File.WriteAllText(configFilePath, System.Text.Json.JsonSerializer.Serialize(testConfig));
735+
736+
// Act - Create should load from file instead of using parameters
737+
var config = ConfiginfoBuilder.Create(
738+
"https://should-be-ignored.com/updates",
739+
"should-be-ignored-token",
740+
"http"
741+
).Build();
742+
743+
// Assert - Values should come from config file, not parameters
744+
Assert.Equal("https://config-file.example.com/updates", config.UpdateUrl);
745+
Assert.Equal("config-file-token", config.Token);
746+
Assert.Equal("https", config.Scheme);
747+
Assert.Equal("ConfigFileApp.exe", config.AppName);
748+
Assert.Equal("ConfigFileMain.exe", config.MainAppName);
749+
Assert.Equal("9.9.9", config.ClientVersion);
750+
Assert.Equal("/config/file/path", config.InstallPath);
751+
}
752+
finally
753+
{
754+
// Cleanup - Delete test config file
755+
if (File.Exists(configFilePath))
756+
{
757+
File.Delete(configFilePath);
758+
}
759+
}
760+
}
761+
762+
/// <summary>
763+
/// Tests that ConfiginfoBuilder uses parameters when no config file exists.
764+
/// </summary>
765+
[Fact]
766+
public void Create_WithoutConfigFile_UsesParameters()
767+
{
768+
// Arrange - Ensure no config file exists
769+
var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "update_config.json");
770+
if (File.Exists(configFilePath))
771+
{
772+
File.Delete(configFilePath);
773+
}
774+
775+
try
776+
{
777+
// Act - Create should use parameters
778+
var config = ConfiginfoBuilder.Create(TestUpdateUrl, TestToken, TestScheme).Build();
779+
780+
// Assert - Values should come from parameters and defaults
781+
Assert.Equal(TestUpdateUrl, config.UpdateUrl);
782+
Assert.Equal(TestToken, config.Token);
783+
Assert.Equal(TestScheme, config.Scheme);
784+
Assert.Equal("Update.exe", config.AppName); // Default value
785+
}
786+
finally
787+
{
788+
// No cleanup needed since we're ensuring file doesn't exist
789+
}
790+
}
791+
792+
/// <summary>
793+
/// Tests that ConfiginfoBuilder handles invalid JSON gracefully.
794+
/// </summary>
795+
[Fact]
796+
public void Create_WithInvalidConfigFile_FallsBackToParameters()
797+
{
798+
// Arrange - Create an invalid config file
799+
var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "update_config.json");
800+
801+
try
802+
{
803+
// Write invalid JSON
804+
File.WriteAllText(configFilePath, "{ invalid json content !!!");
805+
806+
// Act - Create should fall back to parameters
807+
var config = ConfiginfoBuilder.Create(TestUpdateUrl, TestToken, TestScheme).Build();
808+
809+
// Assert - Values should come from parameters (fallback)
810+
Assert.Equal(TestUpdateUrl, config.UpdateUrl);
811+
Assert.Equal(TestToken, config.Token);
812+
Assert.Equal(TestScheme, config.Scheme);
813+
}
814+
finally
815+
{
816+
// Cleanup - Delete test config file
817+
if (File.Exists(configFilePath))
818+
{
819+
File.Delete(configFilePath);
820+
}
821+
}
822+
}
823+
824+
#endregion
708825
}
709826
}

0 commit comments

Comments
 (0)