Skip to content

Commit 76b626c

Browse files
committed
Handle specifically empty filePath, and not default FS rootDir
1 parent bba3ea1 commit 76b626c

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ VER_DURIAN=1.2.0
2828
VER_JGIT=5.13.1.202206130422-r
2929
VER_JUNIT=5.9.2
3030
VER_ASSERTJ=3.24.2
31-
VER_MOCKITO=4.11.0
31+
VER_MOCKITO=5.0.0

lib/src/main/java/com/diffplug/spotless/Formatter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -237,8 +237,14 @@ public String compute(String unix, File file) {
237237
unix = LineEnding.toUnix(formatted);
238238
}
239239
} catch (Throwable e) {
240-
String relativePath = rootDir.relativize(file.toPath()).toString();
241-
exceptionPolicy.handleError(e, step, relativePath);
240+
if (file.getPath().isEmpty()) {
241+
// Path.relativize would fail if rootDir is an absolute path
242+
exceptionPolicy.handleError(e, step, "");
243+
} else {
244+
// Path may be forged from a different FileSystem than Filesystem.default
245+
String relativePath = rootDir.relativize(rootDir.getFileSystem().getPath(file.getPath())).toString();
246+
exceptionPolicy.handleError(e, step, relativePath);
247+
}
242248
}
243249
}
244250
return unix;

testlib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies {
1212
api "com.diffplug.durian:durian-testlib:${VER_DURIAN}"
1313
api "org.junit.jupiter:junit-jupiter:${VER_JUNIT}"
1414
api "org.assertj:assertj-core:${VER_ASSERTJ}"
15+
api "org.mockito:mockito-core:$VER_MOCKITO"
1516

1617
implementation "com.diffplug.durian:durian-io:${VER_DURIAN}"
1718
implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}"

testlib/src/test/java/com/diffplug/spotless/FormatterTest.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,15 +15,19 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import java.io.File;
1819
import java.nio.charset.Charset;
1920
import java.nio.charset.StandardCharsets;
21+
import java.nio.file.FileSystems;
2022
import java.nio.file.Path;
2123
import java.nio.file.Paths;
2224
import java.util.ArrayList;
25+
import java.util.Collections;
2326
import java.util.List;
2427

2528
import org.junit.jupiter.api.Assertions;
2629
import org.junit.jupiter.api.Test;
30+
import org.mockito.Mockito;
2731

2832
import com.diffplug.common.base.StandardSystemProperty;
2933
import com.diffplug.spotless.generic.EndWithNewlineStep;
@@ -89,4 +93,64 @@ protected Formatter create() {
8993
}
9094
}.testEquals();
9195
}
96+
97+
// new File("") can be used if there is no File representing this content. It should not conflict with rootDir.relativize(...)
98+
@Test
99+
public void testExceptionWithEmptyPath() throws Exception {
100+
LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy();
101+
Charset encoding = StandardCharsets.UTF_8;
102+
FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError();
103+
104+
Path rootDir = Paths.get(StandardSystemProperty.USER_DIR.value());
105+
106+
FormatterStep step = Mockito.mock(FormatterStep.class);
107+
Mockito.when(step.getName()).thenReturn("someFailingStep");
108+
Mockito.when(step.format(Mockito.anyString(), Mockito.any(File.class))).thenThrow(new IllegalArgumentException("someReason"));
109+
List<FormatterStep> steps = Collections.singletonList(step);
110+
111+
Formatter formatter = Formatter.builder()
112+
.lineEndingsPolicy(lineEndingsPolicy)
113+
.encoding(encoding)
114+
.rootDir(rootDir)
115+
.steps(steps)
116+
.exceptionPolicy(exceptionPolicy)
117+
.build();
118+
119+
formatter.compute("someFileContent", new File(""));
120+
}
121+
122+
// rootDir may be a path not from the default FileSystem
123+
@Test
124+
public void testExceptionWithRootDirIsNotFileSystem() throws Exception {
125+
LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy();
126+
Charset encoding = StandardCharsets.UTF_8;
127+
FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError();
128+
129+
Path rootDir = Mockito.mock(Path.class);
130+
Path relativized = Mockito.mock(Path.class);
131+
Mockito.when(rootDir.relativize(Mockito.any(Path.class))).then(invok -> {
132+
Path filePath = invok.getArgument(0);
133+
if (filePath.getFileSystem() == FileSystems.getDefault()) {
134+
throw new IllegalArgumentException("Can not relativize through different FileSystems");
135+
}
136+
137+
return relativized;
138+
});
139+
140+
FormatterStep step = Mockito.mock(FormatterStep.class);
141+
Mockito.when(step.getName()).thenReturn("someFailingStep");
142+
Mockito.when(step.format(Mockito.anyString(), Mockito.any(File.class))).thenThrow(new IllegalArgumentException("someReason"));
143+
List<FormatterStep> steps = Collections.singletonList(step);
144+
145+
Formatter formatter = Formatter.builder()
146+
.lineEndingsPolicy(lineEndingsPolicy)
147+
.encoding(encoding)
148+
.rootDir(rootDir)
149+
.steps(steps)
150+
.exceptionPolicy(exceptionPolicy)
151+
.build();
152+
153+
formatter.compute("someFileContent", new File(""));
154+
}
155+
92156
}

0 commit comments

Comments
 (0)