Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Aug 11, 2024
1 parent 6c3c585 commit e231580
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace RandomizerTMF.Logic.Exceptions;

[Serializable]
public class ConfigCorruptedException : Exception
{
public ConfigCorruptedException() { }
public ConfigCorruptedException(string message) : base(message) { }
public ConfigCorruptedException(string message, Exception inner) : base(message, inner) { }
}
5 changes: 3 additions & 2 deletions Src/RandomizerTMF.Logic/Services/RandomizerConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using RandomizerTMF.Logic.Exceptions;
using System.IO.Abstractions;
using System.Reflection;
using YamlDotNet.Core;
Expand Down Expand Up @@ -101,12 +102,12 @@ public static RandomizerConfig GetOrCreate(ILogger logger, IFileSystem fileSyste
catch (YamlException ex)
{
logger.LogWarning(ex.InnerException, "Error while deserializing the config file ({configPath}; [{start}] - [{end}]).", Constants.ConfigYml, ex.Start, ex.End);
throw new Exception("Config file is corrupted or incorrectly formatted.");
throw new ConfigCorruptedException("Config file is corrupted or incorrectly formatted.", ex);
}
catch (Exception ex)
{
logger.LogWarning(ex, "Error while deserializing the config file ({configPath}).", Constants.ConfigYml);
throw new Exception("Config file is corrupted or there's another problem.");
throw new ConfigCorruptedException("Config file is corrupted or there's another problem.", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Moq;
using RandomizerTMF.Logic.Exceptions;
using RandomizerTMF.Logic.Services;
using System.IO.Abstractions.TestingHelpers;

Expand Down Expand Up @@ -48,7 +49,7 @@ public void GetOrCreate_ExistingConfig_Read()
[Theory]
[InlineData(@"GameDirectory: [C:\GameDirectory")]
[InlineData("ReplayParseFailRetries: number")]
public void GetOrCreate_CorruptedConfig_Overwrite(string configContent)
public void GetOrCreate_CorruptedConfig_Throws(string configContent)
{
// Arrange
var mockLogger = new Mock<ILogger>();
Expand All @@ -59,16 +60,7 @@ public void GetOrCreate_CorruptedConfig_Overwrite(string configContent)
var lastWriteTime = fileSystem.File.GetLastWriteTime("Config.yml");
var defaultConfig = new RandomizerConfig();

// Act
var config = RandomizerConfig.GetOrCreate(mockLogger.Object, fileSystem);

// Assert
Assert.True(fileSystem.FileExists("Config.yml"));
Assert.NotEqual(expected: lastWriteTime, fileSystem.File.GetLastWriteTime("Config.yml"));
Assert.Equal(expected: defaultConfig.GameDirectory, actual: config.GameDirectory);
Assert.Equal(expected: defaultConfig.DownloadedMapsDirectory, actual: config.DownloadedMapsDirectory);
Assert.Equal(expected: defaultConfig.ReplayParseFailRetries, actual: config.ReplayParseFailRetries);
Assert.Equal(expected: defaultConfig.ReplayParseFailDelayMs, actual: config.ReplayParseFailDelayMs);
Assert.Equal(expected: defaultConfig.ReplayFileFormat, actual: config.ReplayFileFormat);
// Act & Assert
Assert.Throws<ConfigCorruptedException>(() => RandomizerConfig.GetOrCreate(mockLogger.Object, fileSystem));
}
}

0 comments on commit e231580

Please sign in to comment.