gnodet-bot commented on code in PR #2306:
URL: https://github.com/apache/maven-resolver/pull/2306#discussion_r4518482311


##########
maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java:
##########
@@ -69,6 +71,8 @@
 @Singleton
 @Named
 public class DefaultInstaller implements Installer {
+    private static final Object SESSION_HARDLINK_LOGGED = new Object();

Review Comment:
   ๐Ÿ”ง **Nit: use `Keys.of` with a `toString()` override for debuggability.** The 
codebase convention (see `DefaultUpdateCheckManager.SESSION_CHECKS` / 
`SESSION_NOT_FOUNDS`) wraps session-data keys with `Keys.of(new Object() { 
@Override public String toString() { return "..."; } })`. A bare `new Object()` 
produces an opaque key (`java.lang.Object@<hex>`) in any session-data dump. 
Also requires adding `import org.eclipse.aether.Keys;`.
   
   ```suggestion
       private static final Object SESSION_HARDLINK_LOGGED = Keys.of(new 
Object() {
           @Override
           public String toString() {
               return "installer.hardLinkLogged";
           }
       });
   ```



##########
maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultInstallerTest.java:
##########
@@ -461,6 +465,73 @@ void testHardLinkDisabledFallsThroughToCopy() throws 
InstallationException, IOEx
                 1, countingProcessor.copyWithTimestampCount, 
"copyWithTimestamp must be called when hardLink=false");
     }
 
+    @Test
+    void testHardLinkFallbackLoggedOncePerSession() throws Exception {
+        // This test verifies that when hard-link creation fails 
(cross-device), the INFO log message
+        // is emitted exactly once per session even if install is called 
multiple times.
+        // The test is only meaningful when the environment is cross-device 
(copy fallback is triggered);
+        // it skips on same-device filesystems where hard links succeed and no 
message is emitted.
+        CountingPathProcessor countingProcessor = new CountingPathProcessor();
+        installer = new DefaultInstaller(
+                countingProcessor,
+                new StubRepositoryEventDispatcher(),
+                Collections.emptyMap(),
+                Collections.emptyMap(),
+                Collections.emptyMap(),
+                new StubSyncContextFactory());
+
+        // First install โ€” detect if we're on a cross-device filesystem (copy 
fallback triggered)
+        request.addArtifact(artifact);
+        installer.install(session, request);
+        assumeTrue(
+                countingProcessor.copyWithTimestampCount == 1,
+                "skipped: same-device filesystem โ€” hard link succeeded, no 
fallback log to deduplicate");
+
+        // We're on a cross-device filesystem. Capture stderr (slf4j-simple 
logs there) and
+        // install a second artifact in the same session โ€” the dedup set must 
suppress the 2nd log.
+        Artifact artifact2 = new DefaultArtifact("gid", "aid2", "jar", "ver");
+        artifact2 = 
artifact2.setFile(TestFileUtils.createTempFile("artifact2".getBytes(), 1));
+
+        PrintStream original = System.err;
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        String log;
+        try (PrintStream capture = new PrintStream(buffer, true, 
StandardCharsets.UTF_8.name())) {
+            System.setErr(capture);
+            InstallRequest request2 = new InstallRequest();
+            request2.addArtifact(artifact2);
+            installer.install(session, request2);
+        } finally {
+            System.setErr(original);
+            log = buffer.toString(StandardCharsets.UTF_8.name());
+        }
+
+        // The second install triggered another copy (cross-device), but the 
log must be suppressed
+        assertEquals(2, countingProcessor.copyWithTimestampCount, "second 
install should also copy (cross-device)");
+        assertFalse(
+                log.contains("Hard link not supported"),
+                "hard-link warning must NOT fire a second time in the same 
session; captured log:\n" + log);
+    }
+
+    /**
+     * Runs the action while capturing the slf4j-simple output written to 
{@code System.err}.
+     */
+    private static String captureStdErr(ThrowingRunnable action) throws 
Exception {
+        PrintStream original = System.err;
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        try (PrintStream capture = new PrintStream(buffer, true, 
StandardCharsets.UTF_8.name())) {
+            System.setErr(capture);
+            action.run();
+        } finally {
+            System.setErr(original);
+        }
+        return buffer.toString(StandardCharsets.UTF_8.name());
+    }
+
+    @FunctionalInterface
+    private interface ThrowingRunnable {
+        void run() throws Exception;
+    }

Review Comment:
   ๐Ÿงน **Dead code: `captureStdErr` helper and `ThrowingRunnable` interface are 
defined but never called.** The test inlines the same `System.setErr` capture 
logic instead of delegating to this helper. Either refactor 
`testHardLinkFallbackLoggedOncePerSession` to use `captureStdErr`, or remove 
the dead code.
   
   Remove option (delete the unused block):
   ```suggestion
   ```



-- 
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