Skip to content

Commit

Permalink
Fix FileConfigScope tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 27, 2024
1 parent 0975fe8 commit adf6f46
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
32 changes: 21 additions & 11 deletions NAPS2.Lib.Tests/Config/FileConfigScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public class FileConfigScopeTests : ContextualTests
public void FileScope()
{
var configPath = Path.Combine(FolderPath, "config.xml");
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);

var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite,
TimeSpan.FromMilliseconds(100));

// Nothing should be created yet
Assert.False(File.Exists(configPath));

Expand All @@ -28,7 +30,7 @@ public void FileScope()

var doc = XDocument.Load(configPath);
Assert.Equal("UserConfig", doc.Root?.Name);

var docValue = doc.Descendants("Culture").Single().Value;
Assert.Equal("fr", docValue);

Expand Down Expand Up @@ -88,7 +90,8 @@ public void ReadWithBadXml()
{
var configPath = Path.Combine(FolderPath, "config.xml");
File.WriteAllText(configPath, @"blah");
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);
var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);

Assert.False(scope.Has(c => c.Culture));
}
Expand All @@ -98,7 +101,8 @@ public void ReadWithBadConfig()
{
var configPath = Path.Combine(FolderPath, "config.xml");
File.WriteAllText(configPath, @"<?xml version=""1.0"" encoding=""utf-8""?><Blah><Culture>fr</Culture></Blah>");
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite);
var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite);

Assert.False(scope.Has(c => c.Culture));
}
Expand All @@ -107,7 +111,8 @@ public void ReadWithBadConfig()
public void ReadWithMissingFile()
{
var configPath = Path.Combine(FolderPath, "config.xml");
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite);
var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite);

Assert.False(scope.Has(c => c.Culture));
}
Expand All @@ -117,8 +122,10 @@ public void ReadAppSettings()
{
var configPath = Path.Combine(FolderPath, "appsettings.xml");
File.WriteAllText(configPath, ConfigData.AppSettings);
var defaultsScope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var lockedScope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var defaultsScope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var lockedScope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);

Assert.False(lockedScope.Has(c => c.AlwaysRememberDevice));
Assert.True(lockedScope.TryGet(c => c.PdfSettings.Compat, out var pdfCompat));
Expand All @@ -138,8 +145,10 @@ public void ReadNewAppSettings()
{
var configPath = Path.Combine(FolderPath, "appsettings.xml");
File.WriteAllText(configPath, ConfigData.NewAppSettings);
var defaultsScope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var lockedScope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var defaultsScope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var lockedScope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);

Assert.False(lockedScope.Has(c => c.SingleInstance));
Assert.True(lockedScope.TryGet(c => c.DeleteAfterSaving, out var deleteAfterSaving));
Expand All @@ -155,7 +164,8 @@ public void ReadWithOldConfig()
{
var configPath = Path.Combine(FolderPath, "config.xml");
File.WriteAllText(configPath, ConfigData.OldUserConfig);
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);
var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);

Assert.False(scope.Has(c => c.LockSystemProfiles));
Assert.True(scope.TryGet(c => c.OcrMode, out var ocrMode));
Expand Down
7 changes: 4 additions & 3 deletions NAPS2.Lib/Config/Model/FileConfigScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ namespace NAPS2.Config.Model;
public class FileConfigScope<TConfig> : ConfigScope<TConfig>
{
private static readonly TimeSpan READ_INTERVAL = TimeSpan.FromMilliseconds(5000);

private readonly string _filePath;
private readonly ISerializer<ConfigStorage<TConfig>> _serializer;
private ConfigStorage<TConfig> _cache = new();
private ConfigStorage<TConfig> _changes = new();
private readonly TimedThrottle _readHandshakeThrottle;

public FileConfigScope(string filePath, ISerializer<ConfigStorage<TConfig>> serializer, ConfigScopeMode mode) : base(mode)
public FileConfigScope(string filePath, ISerializer<ConfigStorage<TConfig>> serializer, ConfigScopeMode mode,
TimeSpan? readInterval = null) : base(mode)
{
_filePath = filePath;
_serializer = serializer;
_readHandshakeThrottle = new TimedThrottle(ReadHandshake, READ_INTERVAL);
_readHandshakeThrottle = new TimedThrottle(ReadHandshake, readInterval ?? READ_INTERVAL);
}

protected override bool TryGetInternal(ConfigLookup lookup, out object? value)
Expand Down

0 comments on commit adf6f46

Please sign in to comment.