diff --git a/DiscordChatExporter.Cli.Tests/Fixtures/ExportWrapperFixture.cs b/DiscordChatExporter.Cli.Tests/Fixtures/ExportWrapperFixture.cs index 07f630d..b933c36 100644 --- a/DiscordChatExporter.Cli.Tests/Fixtures/ExportWrapperFixture.cs +++ b/DiscordChatExporter.Cli.Tests/Fixtures/ExportWrapperFixture.cs @@ -34,13 +34,22 @@ public class ExportWrapperFixture : IDisposable // Perform export only if it hasn't been done before if (!File.Exists(filePath)) { - await new ExportChannelsCommand + try { - Token = Secrets.DiscordToken, - ChannelIds = new[] { channelId }, - ExportFormat = format, - OutputPath = filePath - }.ExecuteAsync(new FakeConsole()); + await new ExportChannelsCommand + { + Token = Secrets.DiscordToken, + ChannelIds = new[] { channelId }, + ExportFormat = format, + OutputPath = filePath + }.ExecuteAsync(new FakeConsole()); + } + catch + { + // If the export fails, delete the file to prevent it from being used by tests + File.Delete(filePath); + throw; + } } return await File.ReadAllTextAsync(filePath); diff --git a/DiscordChatExporter.Core/Exporting/MessageExporter.cs b/DiscordChatExporter.Core/Exporting/MessageExporter.cs index 75d8e5c..c525f31 100644 --- a/DiscordChatExporter.Core/Exporting/MessageExporter.cs +++ b/DiscordChatExporter.Core/Exporting/MessageExporter.cs @@ -23,9 +23,16 @@ internal partial class MessageExporter : IAsyncDisposable { if (_writer is not null) { - await _writer.WritePostambleAsync(cancellationToken); - await _writer.DisposeAsync(); - _writer = null; + try + { + await _writer.WritePostambleAsync(cancellationToken); + } + // Writer must be disposed, even if writing postamble fails + finally + { + await _writer.DisposeAsync(); + _writer = null; + } } } diff --git a/DiscordChatExporter.Core/Exporting/Writers/HtmlMessageWriter.cs b/DiscordChatExporter.Core/Exporting/Writers/HtmlMessageWriter.cs index 97dc71d..bc6cf77 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/HtmlMessageWriter.cs +++ b/DiscordChatExporter.Core/Exporting/Writers/HtmlMessageWriter.cs @@ -67,9 +67,7 @@ internal class HtmlMessageWriter : MessageWriter } // Use to preserve blocks of code inside the templates - private string Minify(string html) => _minifier - .Minify(html, false) - .MinifiedContent; + private string Minify(string html) => _minifier.Minify(html, false).MinifiedContent; public override async ValueTask WritePreambleAsync( CancellationToken cancellationToken = default)