gnodet-bot commented on code in PR #14218:
URL: https://github.com/apache/maven/pull/14218#discussion_r4773146600


##########
compat/maven-compat/src/test/java/org/apache/maven/internal/compat/interactivity/LegacyPlexusInteractivityTest.java:
##########
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.internal.compat.interactivity;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+
+import com.google.inject.Inject;
+import org.apache.maven.jline.MessageUtils;
+import org.codehaus.plexus.components.interactivity.InputHandler;
+import org.codehaus.plexus.testing.PlexusTest;
+import org.jline.terminal.Terminal;
+import org.jline.terminal.impl.DumbTerminal;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+@PlexusTest

Review Comment:
   `MessageUtils.systemInstall(Terminal)` sets `MessageUtils.terminal` but does 
**not** reset the statically cached `MessageUtils.reader` field. `getReader()` 
creates the `LineReader` lazily on first call and caches it forever:
   
   ```java
   // MessageUtils.getReader()
   if (r == null) {
       r = LineReaderBuilder.builder().terminal(terminal).build();
       reader = r;  // cached — not reset by systemInstall(Terminal)
   }
   ```
   
   If the `@PlexusTest` harness or any earlier test has already triggered 
`getReader()` during DI container boot, the cached `LineReader` was built from 
a different terminal. `systemInstall(terminal)` replaces 
`MessageUtils.terminal` but the existing reader ignores it — your 
`ByteArrayInputStream` is never read. The test becomes non-deterministic 
depending on test-execution order.
   
   Additionally, the try-with-resources closes the `DumbTerminal` but does not 
reset `MessageUtils.terminal`. Subsequent tests that call 
`MessageUtils.getTerminal()` will get a closed terminal.
   
   Use `MessageUtils.systemUninstall()` to atomically reset both `terminal` and 
`reader`:
   
   ```suggestion
           try (ByteArrayInputStream in = new ByteArrayInputStream(input);
                   Terminal terminal = new DumbTerminal(in, 
OutputStream.nullOutputStream())) {
               MessageUtils.systemUninstall(); // reset both terminal + cached 
reader
               MessageUtils.systemInstall(terminal);
               try {
                   assertEquals("", inputHandler.readLine(), "empty line should 
be readable");
                   assertEquals("Hello World", inputHandler.readLine(), 
"additional lines should be readable");
               } finally {
                   MessageUtils.systemUninstall(); // close terminal and clear 
static state
               }
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to