Handle Clear gracefully in non-interactive console without crashing

This commit is contained in:
Stone_Red
2026-03-06 13:09:23 +01:00
parent b5c0efe4e2
commit 5dd9fdde4e
2 changed files with 15 additions and 5 deletions
@@ -39,14 +39,16 @@ public class ConsoleStatementsTests
}
[TestMethod]
public void ClearThrowsInNonInteractiveConsoleTest()
public void ClearDoesNotCrashInNonInteractiveConsoleTest()
{
List<string> lines =
[
"clear"
"clear",
"var dummy = 0",
"${dummy}"
];
_ = Assert.Throws<IOException>(() => YesNtAssert.GetLastLine(lines));
YesNtAssert.IsLastLineEqual(lines, "0");
}
[TestMethod]
@@ -1,5 +1,6 @@
using System;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums;
@@ -60,7 +61,14 @@ internal class ConsoleStatements : StatementRuntimeInformation
[Statement("clear", SearchMode.Exact, SpaceAround.None, ConsoleColor.Magenta)]
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Won't work if static")]
public void Clear(string _)
{
try
{
Console.Clear();
}
catch (IOException)
{
// Silently fail if the console is not interactive
}
}
}