Skip to content

Commit f443402

Browse files
committed
EXPERIMENTAL: Reduce legacy tokenisation logic as modern versions of IDEA handle this themselves
1 parent 9a02b9e commit f443402

File tree

6 files changed

+9
-237
lines changed

6 files changed

+9
-237
lines changed

src/main/java/org/infernus/idea/checkstyle/util/ProjectFilePaths.java

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class ProjectFilePaths {
1313

1414
private static final Logger LOG = Logger.getInstance(ProjectFilePaths.class);
1515

16-
private static final String IDEA_PROJECT_DIR = "$PROJECT_DIR$";
17-
1816
private final ProjectPaths projectPaths;
1917
private final Project project;
2018
private final char separatorChar;
@@ -75,69 +73,12 @@ public String makeProjectRelative(@Nullable final String path) {
7573

7674
@Nullable
7775
public String tokenise(@Nullable final String fsPath) {
78-
if (fsPath == null) {
79-
return null;
80-
}
81-
82-
if (project.isDefault()) {
83-
if (new File(fsPath).exists() || fsPath.startsWith(IDEA_PROJECT_DIR)) {
84-
return toUnixPath(fsPath);
85-
} else {
86-
return IDEA_PROJECT_DIR + toUnixPath(separatorChar + fsPath);
87-
}
88-
}
89-
90-
final File projectPath = projectPath();
91-
if (projectPath != null && fsPath.startsWith(absolutePathOf.apply(projectPath) + separatorChar)) {
92-
return IDEA_PROJECT_DIR
93-
+ toUnixPath(fsPath.substring(absolutePathOf.apply(projectPath).length()));
94-
}
95-
96-
return toUnixPath(fsPath);
76+
return fsPath;
9777
}
9878

9979
@Nullable
10080
public String detokenise(@Nullable final String tokenisedPath) {
101-
if (tokenisedPath == null) {
102-
return null;
103-
}
104-
105-
String detokenisedPath = replaceProjectToken(tokenisedPath);
106-
107-
if (detokenisedPath == null) {
108-
detokenisedPath = toSystemPath(tokenisedPath);
109-
}
110-
return detokenisedPath;
111-
}
112-
113-
private String replaceProjectToken(final String path) {
114-
int prefixLocation = path.indexOf(IDEA_PROJECT_DIR);
115-
if (prefixLocation >= 0) {
116-
final File projectPath = projectPath();
117-
if (projectPath != null) {
118-
final String projectRelativePath = toSystemPath(path.substring(prefixLocation + IDEA_PROJECT_DIR.length()));
119-
final String completePath = projectPath + File.separator + projectRelativePath;
120-
return absolutePathOf.apply(new File(completePath));
121-
122-
} else {
123-
LOG.warn("Could not detokenise path as project dir is unset: " + path);
124-
}
125-
}
126-
return null;
127-
}
128-
129-
private String toUnixPath(final String systemPath) {
130-
if (separatorChar == '/') {
131-
return systemPath;
132-
}
133-
return systemPath.replace(separatorChar, '/');
134-
}
135-
136-
private String toSystemPath(final String unixPath) {
137-
if (separatorChar == '/') {
138-
return unixPath;
139-
}
140-
return unixPath.replace('/', separatorChar);
81+
return tokenisedPath;
14182
}
14283

14384
@Nullable

src/test/java/org/infernus/idea/checkstyle/config/LegacyProjectConfigurationStateDeserialiserTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void configurationLocationsCanBeDeserialised() {
7272
assertThat(configList, equalTo(List.of(
7373
Descriptor.parse("BUNDLED:(bundled):Sun Checks;All", project).toConfigurationLocation(project),
7474
Descriptor.parse("BUNDLED:(bundled):Google Checks;All", project).toConfigurationLocation(project),
75-
Descriptor.parse("LOCAL_FILE:$PROJECT_DIR$/test-configs/issue-545.xml:545;All", project).toConfigurationLocation(project),
76-
Descriptor.parse("LOCAL_FILE:$PROJECT_DIR$/test-configs/working-checkstyle-rules-8.24.xml:Working;All", project).toConfigurationLocation(project),
75+
Descriptor.parse("LOCAL_FILE:/a/project/path/test-configs/issue-545.xml:545;All", project).toConfigurationLocation(project),
76+
Descriptor.parse("LOCAL_FILE:/a/project/path/test-configs/working-checkstyle-rules-8.24.xml:Working;All", project).toConfigurationLocation(project),
7777
Descriptor.parse("HTTP_URL:http://demo:demo@localhost:8000/working-checkstyle-rules-8.24.xml:Working HTTP;All", project).toConfigurationLocation(project))
7878
));
7979
assertThat(pluginConfiguration.getActiveLocations(), hasSize(1));
@@ -90,21 +90,21 @@ public void configurationLocationsCanBeDeserialised() {
9090
@NotNull
9191
private ProjectConfigurationState.ProjectSettings testConfiguration() {
9292
final Map<String, String> configuration = new HashMap<>();
93-
configuration.put("active-configuration-0", "LOCAL_FILE:$PROJECT_DIR$/test-configs/working-checkstyle-rules-8.24.xml:Working;All");
93+
configuration.put("active-configuration-0", "LOCAL_FILE:/a/project/path/test-configs/working-checkstyle-rules-8.24.xml:Working;All");
9494
configuration.put("checkstyle-version", "10.2");
9595
configuration.put("copy-libs", "false");
9696
configuration.put("location-0", "BUNDLED:(bundled):Sun Checks;All");
9797
configuration.put("location-1", "BUNDLED:(bundled):Google Checks;All");
98-
configuration.put("location-2", "LOCAL_FILE:$PROJECT_DIR$/test-configs/issue-545.xml:545;All");
99-
configuration.put("location-3", "LOCAL_FILE:$PROJECT_DIR$/test-configs/working-checkstyle-rules-8.24.xml:Working;All");
98+
configuration.put("location-2", "LOCAL_FILE:/a/project/path/test-configs/issue-545.xml:545;All");
99+
configuration.put("location-3", "LOCAL_FILE:/a/project/path/test-configs/working-checkstyle-rules-8.24.xml:Working;All");
100100
configuration.put("location-7", "HTTP_URL:http://demo:demo@localhost:8000/working-checkstyle-rules-8.24.xml:Working HTTP;All");
101101
configuration.put("property-1.org.checkstyle.google.suppressionfilter.config", "notxpath");
102102
configuration.put("property-1.org.checkstyle.google.suppressionxpathfilter.config", "xpath");
103103
configuration.put("property-2.checkstyle.classdataabstractioncoupling.excludeclassesregexps", "");
104104
configuration.put("scan-before-checkin", "false");
105105
configuration.put("scanscope", "JavaOnlyWithTests");
106106
configuration.put("suppress-errors", "false");
107-
configuration.put("thirdparty-classpath", "$PROJECT_DIR$/test-configs/spring-javaformat-checkstyle-0.0.31.jar;$PROJECT_DIR$/test-configs/spring-javaformat-config-0.0.31.jar");
107+
configuration.put("thirdparty-classpath", "/a/project/path/test-configs/spring-javaformat-checkstyle-0.0.31.jar;/a/project/path/test-configs/spring-javaformat-config-0.0.31.jar");
108108

109109
return new ProjectConfigurationState.ProjectSettings(configuration);
110110
}

src/test/java/org/infernus/idea/checkstyle/config/ModuleConfigurationStateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void theActiveLocationCanBeDeserialised() {
9898
@NotNull
9999
private Map<String, String> testConfiguration() {
100100
return Map.of(
101-
"active-configuration", "LOCAL_FILE:$PROJECT_DIR$/test-configs/working-checkstyle-rules-module.xml:Working Module;All"
101+
"active-configuration", "LOCAL_FILE:/a/project/path/test-configs/working-checkstyle-rules-module.xml:Working Module;All"
102102
);
103103
}
104104

src/test/java/org/infernus/idea/checkstyle/model/FileConfigurationLocationTest.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,6 @@ public void descriptorShouldContainsTypeLocationAndDescription() {
4949
assertThat(Descriptor.of(underTest, project).toString(), is(equalTo("LOCAL_FILE:aLocation:aDescription;test")));
5050
}
5151

52-
@Test
53-
public void theProjectDirectoryShouldBeTokenisedInDescriptorForUnixPaths() {
54-
underTest.setLocation(PROJECT_BASE_PATH + "/a-path/to/checkstyle.xml");
55-
56-
assertThat(Descriptor.of(underTest, project).toString(), is(equalTo("LOCAL_FILE:$PROJECT_DIR$/a-path/to/checkstyle.xml:aDescription;test")));
57-
}
58-
59-
@Test
60-
public void directoryTraversalsInARelativePathShouldNotBeAlteredByTokenisation() {
61-
underTest.setLocation(PROJECT_BASE_PATH + "/../a-path/to/checkstyle.xml");
62-
63-
assertThat(Descriptor.of(underTest, project).toString(), is(equalTo("LOCAL_FILE:$PROJECT_DIR$/../a-path/to/checkstyle.xml:aDescription;test")));
64-
}
65-
66-
@Test
67-
public void theProjectDirectoryShouldBeTokenisedInDescriptorForWindowsPaths() {
68-
underTest = useWindowsFilePaths();
69-
70-
underTest.setLocation("c:\\some-where\\a-project\\a\\file\\location-in\\checkstyle.xml");
71-
underTest.setDescription("aDescription");
72-
73-
assertThat(Descriptor.of(underTest, project).toString(), is(equalTo("LOCAL_FILE:$PROJECT_DIR$/a/file/location-in/checkstyle.xml:aDescription;test")));
74-
}
75-
7652
@Test
7753
public void aUnixLocationContainingTheProjectPathShouldBeDetokenisedCorrectly() {
7854
underTest.setLocation(PROJECT_BASE_PATH + "/a-path/to/checkstyle.xml");
@@ -103,39 +79,9 @@ public void aUnixLocationShouldBeStoredAndRetrievedCorrectlyWhenTheProjectPathIs
10379
assertThat(underTest.getLocation(), is(equalTo(PROJECT_BASE_PATH + "-sibling/a-path/to/checkstyle.xml")));
10480
}
10581

106-
@Test
107-
public void aWindowsLocationContainingTheProjectPathShouldBeDetokenisedCorrectly() {
108-
underTest = useWindowsFilePaths();
109-
110-
underTest.setLocation("c:\\some-where\\a-project\\a\\file\\location\\checkstyle.xml");
111-
112-
assertThat(underTest.getLocation(), is(equalTo("c:\\some-where\\a-project\\a\\file\\location\\checkstyle.xml")));
113-
}
114-
115-
@Test
116-
public void aWindowsLocationShouldBeStoredAndRetrievedCorrectlyWhenTheProjectPathIsNotUsed() {
117-
underTest = useWindowsFilePaths();
118-
119-
underTest.setLocation("c:\\a\\file\\location\\checkstyle.xml");
120-
121-
assertThat(underTest.getLocation(), is(equalTo("c:\\a\\file\\location\\checkstyle.xml")));
122-
}
123-
124-
private FileConfigurationLocation useWindowsFilePaths() {
125-
ProjectFilePaths testProjectFilePaths = testProjectFilePaths('\\', project);
126-
when(project.getService(ProjectFilePaths.class)).thenReturn(testProjectFilePaths);
127-
when(projectPaths.projectPath(project)).thenReturn(projectBase);
128-
when(projectPaths.projectPath(project)).thenReturn(projectBase);
129-
when(projectBase.getPath()).thenReturn("c:/some-where/a-project");
130-
131-
return new FileConfigurationLocation(project, "winTest");
132-
}
133-
13482
private FileConfigurationLocation useUnixPaths() {
13583
ProjectFilePaths testProjectFilePaths = testProjectFilePaths('/', project);
13684
when(project.getService(ProjectFilePaths.class)).thenReturn(testProjectFilePaths);
137-
when(projectPaths.projectPath(project)).thenReturn(projectBase);
138-
when(projectBase.getPath()).thenReturn(PROJECT_BASE_PATH);
13985

14086
return new FileConfigurationLocation(project, "unixTest");
14187
}

src/test/java/org/infernus/idea/checkstyle/model/RelativeFileConfigurationLocationTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,6 @@ public void aPathWithRelativeElementsIsStoredAsProjectRelative() {
7171
assertThat(underTest.getLocation(), is(equalTo(PROJECT_BASE_PATH + "/../somewhere/rules.xml")));
7272
}
7373

74-
@Test
75-
public void aTokenisedPathWithRelativeElementsIsStoredAsProjectRelative() {
76-
underTest.setLocation("$PROJECT_DIR$/../../../somewhere/rules.xml");
77-
78-
assertThat(underTest.getLocation(), is(equalTo(PROJECT_BASE_PATH + "/../../../somewhere/rules.xml")));
79-
}
80-
8174
@Test
8275
public void aPathWithNoCommonElementsIsStoredAsProjectRelative() {
8376
underTest.setLocation("/somewhere/else/entirely/another-project/rules.xml");

src/test/java/org/infernus/idea/checkstyle/util/ProjectFilePathsTest.java

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.intellij.openapi.project.Project;
44
import com.intellij.openapi.vfs.VirtualFile;
5-
import org.apache.commons.io.FilenameUtils;
65
import org.junit.Before;
76
import org.junit.Test;
87

@@ -16,7 +15,6 @@
1615
public class ProjectFilePathsTest {
1716

1817
private static final String UNIX_PROJECT_BASE_PATH = "/the/base-project/path";
19-
private static final String WINDOWS_PROJECT_BASE_PATH = "c:/some-where/a-project";
2018

2119
private ProjectFilePaths underTest;
2220

@@ -43,94 +41,6 @@ public void aPathWithNoCommonElementsCanBeMadeProjectRelative() {
4341
is(equalTo(UNIX_PROJECT_BASE_PATH + "/../../../somewhere/else/entirely/another-project/rules.xml")));
4442
}
4543

46-
@Test
47-
public void directoryTraversalsInARelativePathShouldNotBeAlteredByTokenisation() {
48-
assertThat(underTest.tokenise(UNIX_PROJECT_BASE_PATH + "/../a-path/to/checkstyle.xml"),
49-
is(equalTo("$PROJECT_DIR$/../a-path/to/checkstyle.xml")));
50-
}
51-
52-
@Test
53-
public void aUnixLocationContainingTheProjectPathShouldBeDetokenisedCorrectly() {
54-
assertThat(underTest.detokenise(UNIX_PROJECT_BASE_PATH + "/a-path/to/checkstyle.xml"),
55-
is(equalTo(UNIX_PROJECT_BASE_PATH + "/a-path/to/checkstyle.xml")));
56-
}
57-
58-
@Test
59-
public void aUnixLocationContainingTheProjectPathTokenShouldBeDetokenisedCorrectly() {
60-
assertThat(underTest.detokenise("$PROJECT_DIR$/a-path/to/checkstyle.xml"),
61-
is(equalTo(UNIX_PROJECT_BASE_PATH + "/a-path/to/checkstyle.xml")));
62-
}
63-
64-
@Test
65-
public void directoryTraversalsInARelativePathShouldNotBeAlteredByDetokenisation() {
66-
assertThat(underTest.detokenise(UNIX_PROJECT_BASE_PATH + "/../a-path/to/checkstyle.xml"),
67-
is(equalTo(UNIX_PROJECT_BASE_PATH + "/../a-path/to/checkstyle.xml")));
68-
}
69-
70-
@Test
71-
public void aUnixLocationWhereTheProjectPathIsNotUsedShouldBeUnalteredByTokenisation() {
72-
assertThat(underTest.tokenise("/a-volume/a-path/to/checkstyle.xml"),
73-
is(equalTo("/a-volume/a-path/to/checkstyle.xml")));
74-
}
75-
76-
@Test
77-
public void aUnixLocationWhereTheProjectPathIsNotUsedShouldBeUnalteredByDetokenisation() {
78-
assertThat(underTest.detokenise("/a-volume/a-path/to/checkstyle.xml"),
79-
is(equalTo("/a-volume/a-path/to/checkstyle.xml")));
80-
}
81-
82-
@Test
83-
public void aUnixLocationWhereTheProjectPathIsNotUsedAndTheFileExistsInAPartiallyMatchingSiblingDirectoryShouldBeUnalteredByTokenisation() {
84-
assertThat(underTest.tokenise(UNIX_PROJECT_BASE_PATH + "-sibling/a-path/to/checkstyle.xml"),
85-
is(equalTo(UNIX_PROJECT_BASE_PATH + "-sibling/a-path/to/checkstyle.xml")));
86-
}
87-
88-
@Test
89-
public void aUnixLocationWhereTheProjectPathIsNotUsedAndTheFileExistsInAPartiallyMatchingSiblingDirectoryShouldBeUnalteredByDetokenisation() {
90-
assertThat(underTest.detokenise(UNIX_PROJECT_BASE_PATH + "-sibling/a-path/to/checkstyle.xml"),
91-
is(equalTo(UNIX_PROJECT_BASE_PATH + "-sibling/a-path/to/checkstyle.xml")));
92-
}
93-
94-
@Test
95-
public void theProjectDirectoryShouldBeTokenisedInDescriptorForWindowsPaths() {
96-
underTest = projectFilePathsForWindows();
97-
98-
assertThat(underTest.tokenise("c:\\some-where\\a-project\\a\\file\\location-in\\checkstyle.xml"),
99-
is(equalTo("$PROJECT_DIR$/a/file/location-in/checkstyle.xml")));
100-
}
101-
102-
@Test
103-
public void aWindowsLocationContainingTheProjectPathTokenShouldBeDetokenisedCorrectly() {
104-
underTest = projectFilePathsForWindows();
105-
106-
assertThat(underTest.detokenise("$PROJECT_DIR$\\a\\file\\location\\checkstyle.xml"),
107-
is(equalTo("c:\\some-where\\a-project\\a\\file\\location\\checkstyle.xml")));
108-
}
109-
110-
@Test
111-
public void aWindowsLocationContainingTheProjectPathShouldBeDetokenisedCorrectly() {
112-
underTest = projectFilePathsForWindows();
113-
114-
assertThat(underTest.detokenise("c:\\some-where\\a-project\\a\\file\\location\\checkstyle.xml"),
115-
is(equalTo("c:\\some-where\\a-project\\a\\file\\location\\checkstyle.xml")));
116-
}
117-
118-
@Test
119-
public void aWindowsLocationWhereTheProjectPathIsNotUsedShouldNotBeAlteredByTokenisation() {
120-
underTest = projectFilePathsForWindows();
121-
122-
assertThat(underTest.tokenise("c:\\a\\file\\location\\checkstyle.xml"),
123-
is(equalTo("c:/a/file/location/checkstyle.xml")));
124-
}
125-
126-
@Test
127-
public void aWindowsLocationWhereTheProjectPathIsNotUsedShouldNotBeAlteredByDetokenisation() {
128-
underTest = projectFilePathsForWindows();
129-
130-
assertThat(underTest.detokenise("c:\\a\\file\\location\\checkstyle.xml"),
131-
is(equalTo("c:\\a\\file\\location\\checkstyle.xml")));
132-
}
133-
13444
private ProjectFilePaths projectFilePathsForUnix() {
13545
Project project = mock(Project.class);
13646
VirtualFile projectBaseFile = mock(VirtualFile.class);
@@ -142,22 +52,4 @@ private ProjectFilePaths projectFilePathsForUnix() {
14252
return ProjectFilePaths.testInstanceWith(project, '/', File::getAbsolutePath, projectPaths);
14353
}
14454

145-
private ProjectFilePaths projectFilePathsForWindows() {
146-
Project project = mock(Project.class);
147-
VirtualFile projectBaseFile = mock(VirtualFile.class);
148-
ProjectPaths projectPaths = mock(ProjectPaths.class);
149-
150-
when(projectBaseFile.getPath()).thenReturn(WINDOWS_PROJECT_BASE_PATH);
151-
when(projectPaths.projectPath(project)).thenReturn(projectBaseFile);
152-
153-
return ProjectFilePaths.testInstanceWith(project, '\\', file -> {
154-
// a nasty hack to pretend we're on a Windows box when required...
155-
if (file.getPath().startsWith("c:")) {
156-
return file.getPath().replace('/', '\\').replaceAll("\\\\\\\\", "\\\\");
157-
}
158-
159-
return FilenameUtils.separatorsToUnix(file.getPath());
160-
}, projectPaths);
161-
}
162-
16355
}

0 commit comments

Comments
 (0)