Copilot commented on code in PR #165: URL: https://github.com/apache/maven-dependency-tree/pull/165#discussion_r4628977604
########## src/test/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitorTest.java: ########## @@ -0,0 +1,81 @@ +/* + * 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.shared.dependency.graph.traversal; + +import java.io.StringWriter; +import java.util.Arrays; +import java.util.Collections; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyNode; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SerializingDependencyNodeVisitorTest { + + @Test + void serializesTreeWithExpectedIndentation() { + DefaultDependencyNode root = node("root", null); + DefaultDependencyNode firstChild = node("first-child", root); + DefaultDependencyNode grandchild = node("grandchild", firstChild); + DefaultDependencyNode secondChild = node("second-child", root); + root.setChildren(Arrays.asList(firstChild, secondChild)); + firstChild.setChildren(Collections.singletonList(grandchild)); + grandchild.setChildren(Collections.emptyList()); + secondChild.setChildren(Collections.emptyList()); + + StringWriter output = new StringWriter(); + root.accept(new SerializingDependencyNodeVisitor(output, SerializingDependencyNodeVisitor.STANDARD_TOKENS)); + + assertEquals( + lines( + "group:root:jar:1.0:compile", + "+- group:first-child:jar:1.0:compile", + "| \\- group:grandchild:jar:1.0:compile", + "\\- group:second-child:jar:1.0:compile"), + output.toString()); + } + + @Test + void interleavedTraversalsDoNotShareDepth() { + DefaultDependencyNode firstRoot = node("first", null); + DefaultDependencyNode secondRoot = node("second", null); + StringWriter output = new StringWriter(); + SerializingDependencyNodeVisitor visitor = + new SerializingDependencyNodeVisitor(output, SerializingDependencyNodeVisitor.STANDARD_TOKENS); + + visitor.visit(firstRoot); + visitor.visit(secondRoot); + visitor.endVisit(secondRoot); + visitor.endVisit(firstRoot); Review Comment: This regression test does not actually expose the old depth bug: both calls at lines 65–66 are for root nodes, so the old visitor emits both without indentation even though its depth becomes incorrect. Please interleave a root with a child traversal (for example, visit `firstRoot`, then `secondRoot` and `secondRoot`'s child before ending the roots) and assert the child is indented; that sequence fails with the previous stateful implementation. ########## src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java: ########## @@ -132,11 +126,12 @@ public SerializingDependencyNodeVisitor(Writer writer, GraphTokens tokens) { */ @Override public boolean visit(DependencyNode node) { - indent(node); - - writer.println(node.toNodeString()); + synchronized (writer) { + indent(node); - depth++; + writer.print(node.toNodeString()); + writer.write('\n'); Review Comment: Replacing `println` with `print` plus `write('\n')` changes the flushing contract. The constructor wraps ordinary writers with `new PrintWriter(writer, true)`, and `PrintWriter` only auto-flushes on `println`/`printf`/`format`; `write('\n')` does not flush. As a result, callers that previously observed each serialized node immediately (including a supplied auto-flushing `PrintWriter`) can now see buffered output until an explicit flush or close. Keep the line emission as `println` while inside this synchronized block. -- 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]
