Copilot commented on code in PR #283: URL: https://github.com/apache/maven-artifact-plugin/pull/283#discussion_r4480212530
########## src/test/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriterConsumerPomTest.java: ########## @@ -0,0 +1,122 @@ +/* + * 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.plugins.artifact.buildinfo; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.stream.Stream; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.apache.maven.model.Build; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.SystemStreamLog; +import org.apache.maven.project.MavenProject; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Covers issue #245: the temporary copy {@link BuildInfoWriter#printArtifacts(MavenProject)} makes of a Maven 4 + * transient consumer POM must be cleaned up once read, and a copy failure must be reported rather than silently + * leaving a POM artifact pointed at a file that was never written. + */ +class BuildInfoWriterConsumerPomTest { + + @Test + void consumerPomTempFileIsDeletedAfterUse(@TempDir Path tempDir) throws Exception { + MavenProject project = newProject(tempDir); + Path sourcePom = tempDir.resolve("source.pom"); + writeFile(sourcePom, "<project/>"); + attachConsumerPom(project, sourcePom); + + newWriter().printArtifacts(project); + + try (Stream<Path> files = Files.list(tempDir)) { + assertFalse( + files.anyMatch(f -> f.getFileName().toString().startsWith("consumer-")), + "the temporary consumer POM copy must be deleted once it has been read"); + } + } + + @Test + void consumerPomCopyFailureIsReportedInsteadOfMasked(@TempDir Path tempDir) { + MavenProject project = newProject(tempDir); + // points at a source file that does not exist, so Files.copy fails with an IOException + attachConsumerPom(project, tempDir.resolve("does-not-exist.pom")); + + MojoExecutionException exception = + assertThrows(MojoExecutionException.class, () -> newWriter().printArtifacts(project)); + assertTrue(exception.getMessage().contains("consumer POM"), exception.getMessage()); + } + + private static MavenProject newProject(Path tempDir) { + MavenProject project = new MavenProject(); + project.setGroupId("org.apache.maven.plugins.it"); + project.setArtifactId("consumer-pom-it"); + project.setVersion("1.0"); + Build build = new Build(); + build.setDirectory(tempDir.toString()); + project.setBuild(build); + File pom = tempDir.resolve("pom.xml").toFile(); + writeFile(pom.toPath(), "<project/>"); + project.setFile(pom); + return project; + } + + @SuppressWarnings("deprecation") // MavenProject#attachArtifact requires an ArtifactHandlerManager we don't have Review Comment: The suppression comment references `MavenProject#attachArtifact`, but this helper uses `project.addAttachedArtifact(consumerPom)` instead. Please update the comment to match the actual deprecated API being used (or remove the suppression if nothing here is deprecated), to avoid misleading maintenance and potential style/static-analysis failures. -- 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]
