Fix path name handling under Windows

This commit is contained in:
Andreas Dangel committed 2022-12-08 17:36:27 +01:00
1 parent 130f8c2719
commit f011056aa9
5 files changed
+69 -14

No files matched your search

@@ -265,7 +265,7 @@ public final class FileCollector implements AutoCloseable {
static String getDisplayName(Path file, List<String> relativizeRoots) {
String fileName = file.toString();
if ("jar".equals(file.toUri().getScheme())) {
fileName = URI.create(file.toUri().getSchemeSpecificPart()).getPath();
fileName = new File(URI.create(file.toUri().getSchemeSpecificPart()).getPath()).toString();
}
for (String root : relativizeRoots) {
if (file.startsWith(root)) {
@@ -5,7 +5,9 @@
package net.sourceforge.pmd.lang.document;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.FileSystem;
import java.nio.file.Files;
@@ -55,7 +57,13 @@ class NioTextFile extends BaseCloseable implements TextFile {
@Override
public @NonNull String getDisplayName() {
return displayName == null ? path.toString() : displayName;
if (displayName != null) {
return displayName;
}
if ("jar".equals(path.toUri().getScheme())) {
return new File(URI.create(path.toUri().getSchemeSpecificPart()).getPath()).toString();
}
return path.toString();
}
@Override
@@ -251,8 +251,7 @@ class CoreCliTest {
void testZipFileAsSource() throws Exception {
Path zipArchive = createTemporaryZipArchive("sources.zip");
String log = SystemLambda.tapSystemErrAndOut(() -> {
StatusCode code = PMD.runPmd("--no-cache", "--dir", zipArchive.toString(), "--rulesets", "rulesets/dummy/basic.xml");
assertEquals(StatusCode.VIOLATIONS_FOUND, code);
runPmd(StatusCode.VIOLATIONS_FOUND, "--no-cache", "--no-progress", "--dir", zipArchive, "--rulesets", "rulesets/dummy/basic.xml");
});
assertThat(log, not(containsStringIgnoringCase("Cannot open zip file")));
String reportPath = IOUtil.normalizePath(zipArchive.toString() + "!/someSource.dummy");
@@ -263,8 +262,7 @@ class CoreCliTest {
void testJarFileAsSource() throws Exception {
Path jarArchive = createTemporaryZipArchive("sources.jar");
String log = SystemLambda.tapSystemErrAndOut(() -> {
StatusCode code = PMD.runPmd("--no-cache", "--dir", jarArchive.toString(), "--rulesets", "rulesets/dummy/basic.xml");
assertEquals(StatusCode.VIOLATIONS_FOUND, code);
runPmd(StatusCode.VIOLATIONS_FOUND, "--no-cache", "--no-progress", "--dir", jarArchive, "--rulesets", "rulesets/dummy/basic.xml");
});
assertThat(log, not(containsStringIgnoringCase("Cannot open zip file")));
String reportPath = IOUtil.normalizePath(jarArchive.toString() + "!/someSource.dummy");
@@ -0,0 +1,50 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.document;
import static org.junit.Assert.assertEquals;
import java.io.FileOutputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import net.sourceforge.pmd.lang.DummyLanguageModule;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.util.IOUtil;
class NioTextFileTest {
@TempDir
private Path tempDir;
@Test
void zipFileDisplayName() throws Exception {
Path zipArchive = tempDir.resolve("sources.zip");
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipArchive.toFile()))) {
ZipEntry zipEntry = new ZipEntry("path/inside/someSource.dummy");
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write("dummy text".getBytes(StandardCharsets.UTF_8));
zipOutputStream.closeEntry();
}
try (FileSystem fileSystem = FileSystems.newFileSystem(URI.create("jar:" + zipArchive.toUri()), Collections.<String, Object>emptyMap())) {
Path path = fileSystem.getPath("path/inside/someSource.dummy");
LanguageRegistry.PMD.getLanguageById("dummy");
LanguageVersion languageVersion = DummyLanguageModule.getInstance().getDefaultVersion();
TextFile textFile = TextFile.builderForPath(path, StandardCharsets.UTF_8, languageVersion).build();
assertEquals(zipArchive.toAbsolutePath() + "!" + IOUtil.normalizePath("/path/inside/someSource.dummy"),
textFile.getDisplayName());
}
}
}
@@ -16,19 +16,18 @@ import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import net.sourceforge.pmd.util.IOUtil;
public class PathDataSourceTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
class PathDataSourceTest {
@TempDir
private Path tempDir;
@Test
public void testZipFileNiceName() throws Exception {
Path zipArchive = tempFolder.getRoot().toPath().resolve("sources.zip");
void testZipFileNiceName() throws Exception {
Path zipArchive = tempDir.resolve("sources.zip");
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipArchive.toFile()))) {
ZipEntry zipEntry = new ZipEntry("path/inside/someSource.dummy");
zipOutputStream.putNextEntry(zipEntry);