From c91a0a87d1497041b3f9f1c0e815c5f2d7422456 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Fri, 16 Aug 2024 20:56:09 +0800 Subject: [PATCH] WIP: Implement diff test --- .github/workflows/test.yml | 2 +- scripts/test.sh | 1 + scripts/test_all.sh | 1 + tests/DiffTests.cs | 116 +++++++++++++++++++++++++++++++++++-- tests/tests.csproj | 1 + 5 files changed, 116 insertions(+), 5 deletions(-) create mode 100755 scripts/test.sh create mode 100755 scripts/test_all.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0dda122..88ae0fe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,4 +8,4 @@ jobs: steps: - uses: actions/checkout@v4 - name: Test - run: dotnet test tests/tests.csproj + run: ./script/test_all.sh diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..f84ca04 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1 @@ +dotnet test tests/tests.csproj --filter "DisplayName~%22$1.mario%22" diff --git a/scripts/test_all.sh b/scripts/test_all.sh new file mode 100755 index 0000000..856d0e8 --- /dev/null +++ b/scripts/test_all.sh @@ -0,0 +1 @@ +dotnet test tests/tests.csproj diff --git a/tests/DiffTests.cs b/tests/DiffTests.cs index 6fadb97..2179762 100644 --- a/tests/DiffTests.cs +++ b/tests/DiffTests.cs @@ -1,8 +1,116 @@ +using System.Diagnostics; + namespace tests; -public class DiffTests { - [Fact] - public void ExecuteDiffTests() { - Assert.True(true); // TODO +public enum GitStatus { + Modified, + Renamed, + Added, + Other +} + +enum TestResult { + Success, + Fail, + Timeout +} + +public class GitDiffData: IDisposable { + private Dictionary fileStatus = new Dictionary(); + + public GitDiffData() { + var shell = new Process(); + shell.StartInfo.FileName = "bash"; // TODO: different system + shell.StartInfo.UseShellExecute = false; + shell.StartInfo.RedirectStandardInput = true; + shell.StartInfo.RedirectStandardOutput = true; + shell.StartInfo.RedirectStandardError = false; + shell.StartInfo.CreateNoWindow = true; + shell.Start(); + shell.StandardInput.WriteLine("git status --porcelain " + String.Join("/", System.Environment.CurrentDirectory, DiffTests.testPath)); + shell.StandardInput.Close(); + string res = shell.StandardOutput.ReadToEnd(); + shell.WaitForExit(); + shell.Close(); + + Array.ForEach(res.Split('\n'), line => { + if (line.Length < 3) { return; } + + string s = line.TrimStart()[..2]; + string path = line[(line.LastIndexOf("/") + 1)..]; + path = path[..(path.Length - 1)]; + if (path.EndsWith(DiffTests.testExtension)) { + var status = GitStatus.Other; + switch (s) { + case "A ": + case "??": + status = GitStatus.Added; + break; + case "M ": + status = GitStatus.Modified; + break; + case "R ": + status = GitStatus.Renamed; + break; + } + fileStatus[path] = status; + } + }); + } + + public void Dispose(){} + + public bool NeedToTest(string filename) { + GitStatus s; + bool flag = fileStatus.TryGetValue(filename, out s); + return fileStatus.Count == 0 ? true : !flag && s != GitStatus.Other; + } +} + +public class DiffTests: IClassFixture { + private readonly static int timeLimit = 3000; // ms + + public readonly static string testPath = "../../../mario"; + public readonly static string testExtension = ".mario"; + + private GitDiffData diffData; + public DiffTests(GitDiffData data) { + diffData = data; + } + + [SkippableTheory] + [MemberData(nameof(GetFileList))] + public void ExecuteDiffTests(string filename) { + string caseName = filename[(filename.LastIndexOf("/") + 1)..]; + Skip.IfNot(diffData.NeedToTest(caseName)); + var reader = new StreamReader(filename); + var result = TestResult.Success; + DateTime begin = DateTime.Now; + // TODO: test + DateTime end = DateTime.Now; + int time = (end - begin).Milliseconds; + var color = ConsoleColor.Green; + + if (result == TestResult.Success && time > timeLimit) { + color = ConsoleColor.Gray; + } + else if (result == TestResult.Fail) { + color = ConsoleColor.Red; + } + ConsoleColor backup = Console.ForegroundColor; + Console.ForegroundColor = color; + Console.WriteLine("> Case " + caseName + ": " + time.ToString() + " ms."); + Console.ForegroundColor = backup; + Console.WriteLine(); + reader.Close(); + } + + public static IEnumerable GetFileList() { + var directory = new DirectoryInfo(testPath); + foreach (FileInfo file in directory.GetFiles()) { + if (file.Extension == testExtension) { + yield return new object[] { file.FullName }; + } + } } } diff --git a/tests/tests.csproj b/tests/tests.csproj index c93f164..7289938 100644 --- a/tests/tests.csproj +++ b/tests/tests.csproj @@ -19,6 +19,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all +