gnodet-bot commented on code in PR #13860: URL: https://github.com/apache/maven/pull/13860#discussion_r4488272392
########## compat/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliLoggingTest.java: ########## @@ -0,0 +1,358 @@ +/* + * 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.cli; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.FutureTask; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.maven.cling.logging.Slf4jConfiguration; +import org.apache.maven.cling.logging.Slf4jConfigurationFactory; +import org.apache.maven.jline.FastTerminal; +import org.apache.maven.jline.MessageUtils; +import org.apache.maven.slf4j.MavenBaseLogger; +import org.jline.terminal.Terminal; +import org.jline.terminal.impl.DumbTerminal; +import org.jline.terminal.spi.TerminalExt; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.api.parallel.Isolated; +import org.slf4j.LoggerFactory; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; + +@Isolated("Changes JVM-wide streams, system properties, terminal state, and logging configuration") +class MavenCliLoggingTest { + + private PrintStream savedOut; + private Terminal savedTerminal; + private boolean savedColor; + private String savedLogFile; + private String savedCacheOutputStream; + + @BeforeEach + void setUp() throws Exception { + savedOut = System.out; + savedTerminal = getTerminalField(); + savedColor = getColorEnabled(); + // Null out the terminal so leftover FastTerminal threads from other tests cannot + // race with this test's stream setup via their consumer callbacks. + injectTerminal(null); + savedLogFile = System.getProperty(MavenBaseLogger.LEGACY_PREFIX + "logFile"); + savedCacheOutputStream = System.getProperty(MavenBaseLogger.LEGACY_PREFIX + "cacheOutputStream"); + } + + @AfterEach + void tearDown() throws Exception { + System.setOut(savedOut); + injectTerminal(savedTerminal); + setColorEnabled(savedColor); + restoreProperty(MavenBaseLogger.LEGACY_PREFIX + "logFile", savedLogFile); + restoreProperty(MavenBaseLogger.LEGACY_PREFIX + "cacheOutputStream", savedCacheOutputStream); + // Restore the logger to write to the original System.err (default). + System.clearProperty(MavenBaseLogger.LEGACY_PREFIX + "logFile"); + System.clearProperty(MavenBaseLogger.LEGACY_PREFIX + "cacheOutputStream"); Review Comment: 🔍 **Dead code:** `restoreProperty(key, savedValue)` is called, then `System.clearProperty(key)` immediately overwrites it for the same two keys — the restore is effectively a no-op. If those properties were set before the test run (e.g. via `-DlogFile=...` on the JVM command line), `tearDown` would incorrectly discard the original value instead of restoring it. Pick one cleanup: - Remove the `restoreProperty` calls (lines 80–81) and keep only `clearProperty` + `activateSlf4j()` — if the intent is always "reset logger to default", clearing is correct. - Remove the `clearProperty` calls (lines 83–84) and `activateSlf4j()`, and let `restoreProperty` do the full restore — if the intent is "leave the JVM in the same state it was before the test". -- 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]
