elharo commented on code in PR #282:
URL: 
https://github.com/apache/maven-artifact-plugin/pull/282#discussion_r4480399744


##########
src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java:
##########
@@ -451,8 +451,30 @@ private Toolchain getToolchain() {
 
     protected String relative(File file) {
         File basedir = session.getTopLevelProject().getBasedir();
-        int length = basedir.getPath().length();
-        String path = file.getPath();
-        return path.substring(length + 1);
+        return relativize(basedir.toPath(), file.toPath());
+    }
+
+    /**

Review Comment:
   javadoc belongs on the published API



##########
src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java:
##########
@@ -451,8 +451,30 @@ private Toolchain getToolchain() {
 
     protected String relative(File file) {
         File basedir = session.getTopLevelProject().getBasedir();
-        int length = basedir.getPath().length();
-        String path = file.getPath();
-        return path.substring(length + 1);
+        return relativize(basedir.toPath(), file.toPath());
+    }
+
+    /**
+     * Computes the path of {@code file} relative to {@code basedir}, 
correctly handling a {@code file} that
+     * is not actually below {@code basedir} (for example a build artifact 
copied outside the reactor), instead
+     * of assuming {@code basedir}'s path is always a literal prefix of {@code 
file}'s path.
+     *
+     * <p>Falls back to an absolute, normalized path if {@code basedir} and 
{@code file} cannot be relativized
+     * directly, which happens when one is absolute and the other relative, or 
when they have different roots
+     * (for example different drives on Windows); {@link 
Path#relativize(Path)} throws
+     * {@link IllegalArgumentException} in those cases.
+     */
+    static String relativize(Path basedir, Path file) {

Review Comment:
   you can still inline this and test only through existing API



##########
src/test/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojoRelativizeTest.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class AbstractBuildinfoMojoRelativizeTest {
+    @Test
+    void relativizesFileUnderBasedir() {
+        assertEquals(
+                "target" + File.separator + "app.jar",
+                AbstractBuildinfoMojo.relativize(Paths.get("/a/b"), 
Paths.get("/a/b/target/app.jar")));
+    }
+
+    @Test
+    void doesNotTreatSiblingDirectoryWithSharedPrefixAsUnderBasedir() {
+        // basedir=/a/b, file=/a/bc/d: a naive string prefix-strip would 
wrongly return "d"
+        assertEquals(
+                ".." + File.separator + "bc" + File.separator + "d",
+                AbstractBuildinfoMojo.relativize(Paths.get("/a/b"), 
Paths.get("/a/bc/d")));
+    }
+
+    @Test
+    void doesNotThrowWhenFileIsShorterThanBasedir() {
+        // a naive substring(length + 1) throws 
StringIndexOutOfBoundsException here
+        assertEquals("..", AbstractBuildinfoMojo.relativize(Paths.get("/a/b"), 
Paths.get("/a")));
+    }
+
+    @Test
+    void fallsBackInsteadOfThrowingWhenOneSideIsNotAbsolute() {
+        // Path#relativize throws IllegalArgumentException when exactly one of 
the two paths is absolute.
+        // relativize() must recover by retrying with both paths made 
absolute, rather than propagate it.
+        Path basedir = Paths.get("/a/b");
+        Path file = Paths.get("relative/child.jar");
+
+        String result = assertDoesNotThrow(() -> 
AbstractBuildinfoMojo.relativize(basedir, file));

Review Comment:
   no, just assert the correct outcome here



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