Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/c#/GeneralUpdate.Core/Bootstrap/GeneralUpdateBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,37 @@ public GeneralUpdateBootstrap SetCustomSkipOption(Func<bool>? func)
return this;
}

/// <summary>
/// Load configuration from a local JSON file.
/// </summary>
/// <param name="filePath">
/// Config file path.
/// If just a filename (no directory separator), resolves relative to the current directory.
/// Relative or absolute paths are used as-is.
/// </param>
public GeneralUpdateBootstrap SetConfig(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
throw new ArgumentNullException(nameof(filePath));

// Resolve filename-only paths to current directory
var hasPathChar = filePath.Contains(Path.DirectorySeparatorChar)
|| filePath.Contains(Path.AltDirectorySeparatorChar);
var fullPath = hasPathChar
? Path.GetFullPath(filePath)
: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath);

if (!File.Exists(fullPath))
throw new FileNotFoundException($"Config file not found: {fullPath}");

var json = File.ReadAllText(fullPath);
var config = JsonSerializer.Deserialize(json, JsonContext.HttpParameterJsonContext.Default.Configinfo);
if (config == null)
throw new InvalidOperationException($"Failed to parse config file: {fullPath}");

return SetConfig(config);
}

public GeneralUpdateBootstrap AddListenerUpdatePrecheck(Func<UpdateInfoEventArgs, bool> func)
{
_updatePrecheck = func ?? throw new ArgumentNullException(nameof(func));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using GeneralUpdate.Core.Configuration;
using GeneralUpdate.Core.Download.Abstractions;

namespace GeneralUpdate.Core.JsonContext;
Expand All @@ -12,4 +13,5 @@ namespace GeneralUpdate.Core.JsonContext;
[JsonSerializable(typeof(Dictionary<string, object>))]
[JsonSerializable(typeof(PacketDTO))]
[JsonSerializable(typeof(List<PacketDTO>))]
[JsonSerializable(typeof(Configinfo))]
public partial class HttpParameterJsonContext: JsonSerializerContext;
7 changes: 4 additions & 3 deletions tests/CoreTest/Integration/OssIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ await Assert.ThrowsAsync<InvalidOperationException>(() =>
}

[Fact]
public async Task OSSUpdateStrategy_RequiresConfig()
public async Task OSSUpdateStrategy_WithoutConfig_ReturnsWithoutError()
{
// OSS client without UpdateUrl or local version config: no exception, just returns
var strategy = new OSSUpdateStrategy();
var config = new GlobalConfigInfo
{
Expand All @@ -83,8 +84,8 @@ public async Task OSSUpdateStrategy_RequiresConfig()
};
strategy.Create(config);

await Assert.ThrowsAsync<System.IO.FileNotFoundException>(() =>
strategy.ExecuteAsync());
var ex = await Record.ExceptionAsync(() => strategy.ExecuteAsync());
Assert.Null(ex);
}

[Fact]
Expand Down
Loading