Improve tests

pull/678/head
Tyrrrz 3 years ago
parent 85d53d0e94
commit d735d2acd3

@ -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

@ -36,6 +36,8 @@ namespace DiscordChatExporter.Cli.Tests
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json");
// Act
var jsonData = await GlobalCache.WrapAsync("mention-specs-output-json", async () =>
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
@ -45,7 +47,9 @@ namespace DiscordChatExporter.Cli.Tests
OutputPath = outputFilePath
}.ExecuteAsync(new FakeConsole());
var jsonData = await File.ReadAllTextAsync(outputFilePath);
return await File.ReadAllTextAsync(outputFilePath);
});
_testOutput.WriteLine(jsonData);
var json = Json.Parse(jsonData);
@ -81,6 +85,8 @@ namespace DiscordChatExporter.Cli.Tests
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html");
// Act
var htmlData = await GlobalCache.WrapAsync("mention-specs-output-html", async () =>
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
@ -90,7 +96,9 @@ namespace DiscordChatExporter.Cli.Tests
OutputPath = outputFilePath
}.ExecuteAsync(new FakeConsole());
var htmlData = await File.ReadAllTextAsync(outputFilePath);
return await File.ReadAllTextAsync(outputFilePath);
});
_testOutput.WriteLine(htmlData);
var html = Html.Parse(htmlData);
@ -110,6 +118,8 @@ namespace DiscordChatExporter.Cli.Tests
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json");
// Act
var jsonData = await GlobalCache.WrapAsync("mention-specs-output-json", async () =>
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
@ -119,7 +129,9 @@ namespace DiscordChatExporter.Cli.Tests
OutputPath = outputFilePath
}.ExecuteAsync(new FakeConsole());
var jsonData = await File.ReadAllTextAsync(outputFilePath);
return await File.ReadAllTextAsync(outputFilePath);
});
_testOutput.WriteLine(jsonData);
var json = Json.Parse(jsonData);
@ -148,6 +160,8 @@ namespace DiscordChatExporter.Cli.Tests
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html");
// Act
var htmlData = await GlobalCache.WrapAsync("mention-specs-output-html", async () =>
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
@ -157,7 +171,9 @@ namespace DiscordChatExporter.Cli.Tests
OutputPath = outputFilePath
}.ExecuteAsync(new FakeConsole());
var htmlData = await File.ReadAllTextAsync(outputFilePath);
return await File.ReadAllTextAsync(outputFilePath);
});
_testOutput.WriteLine(htmlData);
var html = Html.Parse(htmlData);
@ -176,6 +192,8 @@ namespace DiscordChatExporter.Cli.Tests
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json");
// Act
var jsonData = await GlobalCache.WrapAsync("mention-specs-output-json", async () =>
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
@ -185,7 +203,9 @@ namespace DiscordChatExporter.Cli.Tests
OutputPath = outputFilePath
}.ExecuteAsync(new FakeConsole());
var jsonData = await File.ReadAllTextAsync(outputFilePath);
return await File.ReadAllTextAsync(outputFilePath);
});
_testOutput.WriteLine(jsonData);
var json = Json.Parse(jsonData);
@ -214,6 +234,8 @@ namespace DiscordChatExporter.Cli.Tests
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html");
// Act
var htmlData = await GlobalCache.WrapAsync("mention-specs-output-html", async () =>
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
@ -223,7 +245,9 @@ namespace DiscordChatExporter.Cli.Tests
OutputPath = outputFilePath
}.ExecuteAsync(new FakeConsole());
var htmlData = await File.ReadAllTextAsync(outputFilePath);
return await File.ReadAllTextAsync(outputFilePath);
});
_testOutput.WriteLine(htmlData);
var html = Html.Parse(htmlData);
@ -242,6 +266,8 @@ namespace DiscordChatExporter.Cli.Tests
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json");
// Act
var jsonData = await GlobalCache.WrapAsync("mention-specs-output-json", async () =>
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
@ -251,7 +277,9 @@ namespace DiscordChatExporter.Cli.Tests
OutputPath = outputFilePath
}.ExecuteAsync(new FakeConsole());
var jsonData = await File.ReadAllTextAsync(outputFilePath);
return await File.ReadAllTextAsync(outputFilePath);
});
_testOutput.WriteLine(jsonData);
var json = Json.Parse(jsonData);
@ -280,6 +308,8 @@ namespace DiscordChatExporter.Cli.Tests
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html");
// Act
var htmlData = await GlobalCache.WrapAsync("mention-specs-output-html", async () =>
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
@ -289,7 +319,9 @@ namespace DiscordChatExporter.Cli.Tests
OutputPath = outputFilePath
}.ExecuteAsync(new FakeConsole());
var htmlData = await File.ReadAllTextAsync(outputFilePath);
return await File.ReadAllTextAsync(outputFilePath);
});
_testOutput.WriteLine(htmlData);
var html = Html.Parse(htmlData);

@ -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<string, object?> Dictionary = new();
public static async Task<T> WrapAsync<T>(string key, Func<Task<T>> getAsync)
{
if (Dictionary.TryGetValue(key, out var value) && value is T existing)
return existing;
var result = await getAsync();
Dictionary[key] = result;
return result;
}
}
}
Loading…
Cancel
Save