Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve absolute path detection #921

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/edu/hm/hafner/util/PathUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package edu.hm.hafner.util;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
Expand Down Expand Up @@ -259,6 +261,15 @@
* @return {@code true} if this path is an absolute path, {@code false} if a relative path
*/
public boolean isAbsolute(final String fileName) {
try {
URI uri = new URI(fileName);

Check warning on line 265 in src/main/java/edu/hm/hafner/util/PathUtil.java

View check run for this annotation

Jenkins (hafner.hm.edu) / Error Prone

Varifier

NORMAL: Consider using `var` here to avoid boilerplate.
Raw output
Did you mean: <pre><code>var uri = new URI(fileName);</code></pre><p><a href="https://errorprone.info/bugpattern/Varifier">See ErrorProne documentation.</a></p>
if (uri.isAbsolute()) {
return true;
}
}
catch (URISyntaxException ignored) {
// catch and ignore as system paths are not URI, and we need to check them separately
}
return FilenameUtils.getPrefixLength(fileName) > 0;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/edu/hm/hafner/util/PathUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ void shouldFindResourceFolder() {

@DisplayName("Should verify valid absolute paths")
@ParameterizedTest(name = "[{index}] path={0}")
@ValueSource(strings = {"/", "/tmp", "C:\\", "c:\\", "C:\\Tmp"})
@ValueSource(strings = {"/", "/tmp", "C:\\", "c:\\", "C:\\Tmp", "C:/tmp/absolute.txt", "file:///project/src/main/java/com/app/ui/model/Activity.kt"})
void shouldFindAbsolutePaths(final String path) {
var pathUtil = new PathUtil();

assertThat(pathUtil.isAbsolute(path)).isTrue();
assertThat(pathUtil.isAbsolute(path)).as("Show be detected as absolute path").isTrue();
}

@Test
Expand Down
Loading