From d735d2acd396d584eb5dd6132d67829062d99df6 Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Mon, 19 Jul 2021 20:19:32 +0300 Subject: [PATCH] Improve tests --- .github/workflows/CI.yml | 3 + DiscordChatExporter.Cli.Tests/MentionSpecs.cs | 176 +++++++++++------- .../Utils/GlobalCache.cs | 22 +++ 3 files changed, 129 insertions(+), 72 deletions(-) create mode 100644 DiscordChatExporter.Cli.Tests/Utils/GlobalCache.cs diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 1e6b93f..10caeaa 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -22,6 +22,9 @@ jobs: - name: Build & test run: dotnet test --configuration Release --logger GitHubActions + env: + DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }} + DISCORD_TOKEN_BOT: true - name: Upload coverage uses: codecov/codecov-action@v1.0.5 diff --git a/DiscordChatExporter.Cli.Tests/MentionSpecs.cs b/DiscordChatExporter.Cli.Tests/MentionSpecs.cs index 6b0a049..e7daa91 100644 --- a/DiscordChatExporter.Cli.Tests/MentionSpecs.cs +++ b/DiscordChatExporter.Cli.Tests/MentionSpecs.cs @@ -36,16 +36,20 @@ namespace DiscordChatExporter.Cli.Tests var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json"); // Act - await new ExportChannelsCommand + var jsonData = await GlobalCache.WrapAsync("mention-specs-output-json", async () => { - TokenValue = Secrets.DiscordToken, - IsBotToken = Secrets.IsDiscordTokenBot, - ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, - ExportFormat = ExportFormat.Json, - OutputPath = outputFilePath - }.ExecuteAsync(new FakeConsole()); - - var jsonData = await File.ReadAllTextAsync(outputFilePath); + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, + ExportFormat = ExportFormat.Json, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + _testOutput.WriteLine(jsonData); var json = Json.Parse(jsonData); @@ -81,16 +85,20 @@ namespace DiscordChatExporter.Cli.Tests var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html"); // Act - await new ExportChannelsCommand + var htmlData = await GlobalCache.WrapAsync("mention-specs-output-html", async () => { - TokenValue = Secrets.DiscordToken, - IsBotToken = Secrets.IsDiscordTokenBot, - ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, - ExportFormat = ExportFormat.HtmlDark, - OutputPath = outputFilePath - }.ExecuteAsync(new FakeConsole()); - - var htmlData = await File.ReadAllTextAsync(outputFilePath); + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, + ExportFormat = ExportFormat.HtmlDark, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + _testOutput.WriteLine(htmlData); var html = Html.Parse(htmlData); @@ -110,16 +118,20 @@ namespace DiscordChatExporter.Cli.Tests var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json"); // Act - await new ExportChannelsCommand + var jsonData = await GlobalCache.WrapAsync("mention-specs-output-json", async () => { - TokenValue = Secrets.DiscordToken, - IsBotToken = Secrets.IsDiscordTokenBot, - ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, - ExportFormat = ExportFormat.Json, - OutputPath = outputFilePath - }.ExecuteAsync(new FakeConsole()); - - var jsonData = await File.ReadAllTextAsync(outputFilePath); + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, + ExportFormat = ExportFormat.Json, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + _testOutput.WriteLine(jsonData); var json = Json.Parse(jsonData); @@ -148,16 +160,20 @@ namespace DiscordChatExporter.Cli.Tests var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html"); // Act - await new ExportChannelsCommand + var htmlData = await GlobalCache.WrapAsync("mention-specs-output-html", async () => { - TokenValue = Secrets.DiscordToken, - IsBotToken = Secrets.IsDiscordTokenBot, - ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, - ExportFormat = ExportFormat.HtmlDark, - OutputPath = outputFilePath - }.ExecuteAsync(new FakeConsole()); - - var htmlData = await File.ReadAllTextAsync(outputFilePath); + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, + ExportFormat = ExportFormat.HtmlDark, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + _testOutput.WriteLine(htmlData); var html = Html.Parse(htmlData); @@ -176,16 +192,20 @@ namespace DiscordChatExporter.Cli.Tests var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json"); // Act - await new ExportChannelsCommand + var jsonData = await GlobalCache.WrapAsync("mention-specs-output-json", async () => { - TokenValue = Secrets.DiscordToken, - IsBotToken = Secrets.IsDiscordTokenBot, - ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, - ExportFormat = ExportFormat.Json, - OutputPath = outputFilePath - }.ExecuteAsync(new FakeConsole()); - - var jsonData = await File.ReadAllTextAsync(outputFilePath); + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, + ExportFormat = ExportFormat.Json, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + _testOutput.WriteLine(jsonData); var json = Json.Parse(jsonData); @@ -214,16 +234,20 @@ namespace DiscordChatExporter.Cli.Tests var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html"); // Act - await new ExportChannelsCommand + var htmlData = await GlobalCache.WrapAsync("mention-specs-output-html", async () => { - TokenValue = Secrets.DiscordToken, - IsBotToken = Secrets.IsDiscordTokenBot, - ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, - ExportFormat = ExportFormat.HtmlDark, - OutputPath = outputFilePath - }.ExecuteAsync(new FakeConsole()); - - var htmlData = await File.ReadAllTextAsync(outputFilePath); + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, + ExportFormat = ExportFormat.HtmlDark, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + _testOutput.WriteLine(htmlData); var html = Html.Parse(htmlData); @@ -242,16 +266,20 @@ namespace DiscordChatExporter.Cli.Tests var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json"); // Act - await new ExportChannelsCommand + var jsonData = await GlobalCache.WrapAsync("mention-specs-output-json", async () => { - TokenValue = Secrets.DiscordToken, - IsBotToken = Secrets.IsDiscordTokenBot, - ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, - ExportFormat = ExportFormat.Json, - OutputPath = outputFilePath - }.ExecuteAsync(new FakeConsole()); - - var jsonData = await File.ReadAllTextAsync(outputFilePath); + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, + ExportFormat = ExportFormat.Json, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + _testOutput.WriteLine(jsonData); var json = Json.Parse(jsonData); @@ -280,16 +308,20 @@ namespace DiscordChatExporter.Cli.Tests var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html"); // Act - await new ExportChannelsCommand + var htmlData = await GlobalCache.WrapAsync("mention-specs-output-html", async () => { - TokenValue = Secrets.DiscordToken, - IsBotToken = Secrets.IsDiscordTokenBot, - ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, - ExportFormat = ExportFormat.HtmlDark, - OutputPath = outputFilePath - }.ExecuteAsync(new FakeConsole()); - - var htmlData = await File.ReadAllTextAsync(outputFilePath); + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)}, + ExportFormat = ExportFormat.HtmlDark, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + _testOutput.WriteLine(htmlData); var html = Html.Parse(htmlData); diff --git a/DiscordChatExporter.Cli.Tests/Utils/GlobalCache.cs b/DiscordChatExporter.Cli.Tests/Utils/GlobalCache.cs new file mode 100644 index 0000000..e179d8b --- /dev/null +++ b/DiscordChatExporter.Cli.Tests/Utils/GlobalCache.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DiscordChatExporter.Cli.Tests.Utils +{ + internal static class GlobalCache + { + private static readonly Dictionary Dictionary = new(); + + public static async Task WrapAsync(string key, Func> getAsync) + { + if (Dictionary.TryGetValue(key, out var value) && value is T existing) + return existing; + + var result = await getAsync(); + Dictionary[key] = result; + + return result; + } + } +} \ No newline at end of file