From e50a9ab1a329afd5a0ea1ed3c7f5b44f9a60ae53 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Wed, 3 Apr 2024 17:52:39 +0200 Subject: [PATCH] done; this disables the progress bar monitor when the terminal is not an active console --- .../rascalmpl/repl/TerminalProgressBarMonitor.java | 2 ++ src/org/rascalmpl/shell/RascalShell.java | 4 ++-- .../RascalJUnitParallelRecursiveTestRunner.java | 10 ++++++++-- .../test/infrastructure/RascalJUnitTestRunner.java | 13 ++++++++++--- .../test/infrastructure/TestFramework.java | 14 ++++++++++---- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/org/rascalmpl/repl/TerminalProgressBarMonitor.java b/src/org/rascalmpl/repl/TerminalProgressBarMonitor.java index f88d4703984..cee6a231352 100644 --- a/src/org/rascalmpl/repl/TerminalProgressBarMonitor.java +++ b/src/org/rascalmpl/repl/TerminalProgressBarMonitor.java @@ -62,6 +62,8 @@ public TerminalProgressBarMonitor(OutputStream out, Terminal tm) { PrintWriter theWriter = new PrintWriter(out, true, Charset.forName(encoding)); this.writer = debug ? new PrintWriter(new AlwaysFlushAlwaysShowCursor(theWriter)) : theWriter; this.lineWidth = tm.getWidth(); + + assert System.console() != null: "interactive progress bar needs a terminal, and we should not print this into a file anyway."; } /** diff --git a/src/org/rascalmpl/shell/RascalShell.java b/src/org/rascalmpl/shell/RascalShell.java index e5618e2cc72..6ae18d925fa 100644 --- a/src/org/rascalmpl/shell/RascalShell.java +++ b/src/org/rascalmpl/shell/RascalShell.java @@ -69,12 +69,12 @@ public static void main(String[] args) throws IOException { } IRascalMonitor monitor - = term.isAnsiSupported() + = System.console() != null ? new TerminalProgressBarMonitor(System.out, term) : new ConsoleRascalMonitor(); IDEServices services = new BasicIDEServices(new PrintWriter(System.err), monitor); - runner = new REPLRunner(System.in, System.err, term.isAnsiSupported() ? (OutputStream) monitor : System.out, term, services); + runner = new REPLRunner(System.in, System.err, System.console() != null ? (OutputStream) monitor : System.out, term, services); } runner.run(args); diff --git a/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java b/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java index 8927b6c7a09..0aa244b6160 100644 --- a/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java +++ b/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java @@ -12,6 +12,7 @@ package org.rascalmpl.test.infrastructure; import java.io.IOException; +import java.io.OutputStream; import java.io.PrintWriter; import java.lang.annotation.Annotation; import java.util.ArrayList; @@ -30,6 +31,7 @@ import org.junit.runner.Runner; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; +import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.interpreter.Evaluator; import org.rascalmpl.interpreter.ITestResultListener; import org.rascalmpl.interpreter.NullRascalMonitor; @@ -55,7 +57,10 @@ * */ public class RascalJUnitParallelRecursiveTestRunner extends Runner { - private final static TerminalProgressBarMonitor monitor = new TerminalProgressBarMonitor(System.out, TerminalFactory.get()); + private final static IRascalMonitor monitor = System.console() != null + ? new TerminalProgressBarMonitor(System.out, TerminalFactory.get()) + : new NullRascalMonitor(); + private final int numberOfWorkers; private final Semaphore importsCompleted = new Semaphore(0); private final Semaphore waitForRunning = new Semaphore(0); @@ -302,7 +307,8 @@ private void initializeEvaluator() { heap = new GlobalEnvironment(); root = heap.addModule(new ModuleEnvironment("___junit_test___", heap)); - evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, monitor, root, heap); + var outStream = System.console() != null ? (TerminalProgressBarMonitor) monitor : System.out; + evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, outStream, root, heap); evaluator.setMonitor(monitor); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); evaluator.getConfiguration().setErrors(true); diff --git a/src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java b/src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java index 73f2362fa00..49ab31a6f37 100644 --- a/src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java +++ b/src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java @@ -25,6 +25,7 @@ import org.junit.runner.Runner; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; +import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.interpreter.Evaluator; import org.rascalmpl.interpreter.ITestResultListener; import org.rascalmpl.interpreter.NullRascalMonitor; @@ -66,10 +67,16 @@ public class RascalJUnitTestRunner extends Runner { root = heap.addModule(new ModuleEnvironment("___junit_test___", heap)); Terminal tm = TerminalFactory.get(); - TerminalProgressBarMonitor monitor = new TerminalProgressBarMonitor(System.out, tm); + IRascalMonitor monitor = System.console() != null + ? new TerminalProgressBarMonitor(System.out, tm) + : new NullRascalMonitor(); - evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, monitor, root, heap); - evaluator.setMonitor(monitor); + var outStream = System.console() != null + ? (TerminalProgressBarMonitor) monitor + : System.out; + + evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, outStream, root, heap); + evaluator.setMonitor(System.console() != null ? monitor : new NullRascalMonitor()); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); evaluator.getConfiguration().setErrors(true); } diff --git a/src/org/rascalmpl/test/infrastructure/TestFramework.java b/src/org/rascalmpl/test/infrastructure/TestFramework.java index 2c351bcb985..ef67efc701c 100644 --- a/src/org/rascalmpl/test/infrastructure/TestFramework.java +++ b/src/org/rascalmpl/test/infrastructure/TestFramework.java @@ -28,7 +28,10 @@ import java.util.Set; import org.junit.After; +import org.rascalmpl.debug.IRascalMonitor; +import org.rascalmpl.interpreter.ConsoleRascalMonitor; import org.rascalmpl.interpreter.Evaluator; +import org.rascalmpl.interpreter.NullRascalMonitor; import org.rascalmpl.interpreter.env.GlobalEnvironment; import org.rascalmpl.interpreter.env.ModuleEnvironment; import org.rascalmpl.interpreter.load.StandardLibraryContributor; @@ -48,7 +51,10 @@ public class TestFramework { - private final static TerminalProgressBarMonitor monitor = new TerminalProgressBarMonitor(System.out, TerminalFactory.get()); + private final static IRascalMonitor monitor = System.console() != null + ? new TerminalProgressBarMonitor(System.out, TerminalFactory.get()) + : new NullRascalMonitor(); + private final static Evaluator evaluator; private final static GlobalEnvironment heap; private final static ModuleEnvironment root; @@ -60,9 +66,9 @@ public class TestFramework { heap = new GlobalEnvironment(); root = heap.addModule(new ModuleEnvironment("___test___", heap)); - stderr = new PrintWriter(System.err); - stdout = new PrintWriter(monitor); - evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, monitor, root, heap); + stderr = new PrintWriter(System.err, true); + stdout = System.console() != null ? new PrintWriter((TerminalProgressBarMonitor) monitor) : new PrintWriter(System.out, true); + evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.console() != null ? (TerminalProgressBarMonitor) monitor : System.out , root, heap); evaluator.setMonitor(monitor); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());