From 5dd9fdde4e75f309aae174539bf8cfa5d01bca23 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 6 Mar 2026 13:09:23 +0100 Subject: [PATCH] Handle Clear gracefully in non-interactive console without crashing --- .../ConsoleStatementsTests.cs | 8 +++++--- .../Statements/ConsoleStatements.cs | 12 ++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/YesNt.Interpreter.Tests/ConsoleStatementsTests.cs b/src/YesNt.Interpreter.Tests/ConsoleStatementsTests.cs index 1ba7cbc..0901d10 100644 --- a/src/YesNt.Interpreter.Tests/ConsoleStatementsTests.cs +++ b/src/YesNt.Interpreter.Tests/ConsoleStatementsTests.cs @@ -39,14 +39,16 @@ public class ConsoleStatementsTests } [TestMethod] - public void ClearThrowsInNonInteractiveConsoleTest() + public void ClearDoesNotCrashInNonInteractiveConsoleTest() { List lines = [ - "clear" + "clear", + "var dummy = 0", + "${dummy}" ]; - _ = Assert.Throws(() => YesNtAssert.GetLastLine(lines)); + YesNtAssert.IsLastLineEqual(lines, "0"); } [TestMethod] diff --git a/src/YesNt.Interpreter/Statements/ConsoleStatements.cs b/src/YesNt.Interpreter/Statements/ConsoleStatements.cs index 7df8c71..0c391d7 100644 --- a/src/YesNt.Interpreter/Statements/ConsoleStatements.cs +++ b/src/YesNt.Interpreter/Statements/ConsoleStatements.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Diagnostics.CodeAnalysis; +using System.IO; using YesNt.Interpreter.Attributes; using YesNt.Interpreter.Enums; @@ -61,6 +62,13 @@ internal class ConsoleStatements : StatementRuntimeInformation [SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Won't work if static")] public void Clear(string _) { - Console.Clear(); + try + { + Console.Clear(); + } + catch (IOException) + { + // Silently fail if the console is not interactive + } } } \ No newline at end of file