Skip to content

Commit 851d9a4

Browse files
committed
Improve error messages provided by FileUtils
1 parent 84da4c9 commit 851d9a4

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/FileUtils.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,21 @@ public static void assertValidRootName(@Nullable String name) {
111111
Objects.requireNonNull(name, "name");
112112

113113
if (name.isBlank()) {
114-
throw new IllegalArgumentException("Directory name cannot be blank");
114+
throw new IllegalArgumentException(
115+
"Directory name cannot be blank: " + StringUtils.quoted(name)
116+
);
115117
}
116118

117119
if (!name.equals(name.trim())) {
118-
throw new IllegalArgumentException("Directory name cannot begin or end in spaces");
120+
throw new IllegalArgumentException(
121+
"Directory name cannot begin or end in spaces: " + StringUtils.quoted(name)
122+
);
119123
}
120124

121125
if (name.contains("/") || name.contains("\\") || name.contains("..")) {
122-
throw new IllegalArgumentException("Invalid file name provided");
126+
throw new IllegalArgumentException(
127+
"Invalid file name provided: " + StringUtils.quoted(name)
128+
);
123129
}
124130
}
125131

@@ -132,7 +138,7 @@ public static void assertValidRootName(@Nullable String name) {
132138
*/
133139
public static String pathToBinaryName(Path path) {
134140
if (path.isAbsolute()) {
135-
throw new IllegalArgumentException("Path cannot be absolute (got " + path + ")");
141+
throw new IllegalArgumentException("Path cannot be absolute: " + StringUtils.quoted(path));
136142
}
137143

138144
var count = path.getNameCount();

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/utils/FileUtilsTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import io.github.ascopes.jct.tests.helpers.UtilityClassTestTemplate;
4242
import io.github.ascopes.jct.utils.FileUtils;
43+
import io.github.ascopes.jct.utils.StringUtils;
4344
import java.io.IOException;
4445
import java.net.MalformedURLException;
4546
import java.net.URI;
@@ -120,7 +121,7 @@ void resolvePathRecursivelyResolvesThePathRecursively(
120121
String rootString,
121122
String partsString,
122123
String expectString
123-
) throws IOException {
124+
) {
124125
// Given
125126
try (var fs = someTemporaryFileSystem()) {
126127
var root = fs.getFileSystem().getPath(rootString);
@@ -165,7 +166,7 @@ void assertValidRootNameFailsForInvalidRootNames(String rootName, String expecte
165166
// Then
166167
assertThatThrownBy(() -> assertValidRootName(rootName))
167168
.isInstanceOf(IllegalArgumentException.class)
168-
.hasMessage(expectedError);
169+
.hasMessage(expectedError + ": " + StringUtils.quoted(rootName));
169170
}
170171

171172
@DisplayName("pathToBinaryName throws an IllegalArgumentException for absolute paths")
@@ -179,7 +180,7 @@ void pathToBinaryNameThrowsIllegalArgumentExceptionForAbsolutePaths() {
179180
// Then
180181
assertThatThrownBy(() -> pathToBinaryName(path))
181182
.isInstanceOf(IllegalArgumentException.class)
182-
.hasMessage("Path cannot be absolute (got /foo/bar/baz)");
183+
.hasMessage("Path cannot be absolute: \"/foo/bar/baz\"");
183184
}
184185

185186
@DisplayName("pathToBinaryName converts relative paths as expected")
@@ -192,8 +193,7 @@ void pathToBinaryNameThrowsIllegalArgumentExceptionForAbsolutePaths() {
192193
"org/example/foo/bar/Baz$Bork, org.example.foo.bar.Baz$Bork",
193194
})
194195
@ParameterizedTest(name = "pathToBinaryName(\"{0}\") should return \"{1}\"")
195-
void pathToBinaryNameConvertsRelativePathsAsExpected(String input, String expected)
196-
throws IOException {
196+
void pathToBinaryNameConvertsRelativePathsAsExpected(String input, String expected) {
197197

198198
try (var fs = someTemporaryFileSystem()) {
199199
// Given
@@ -262,7 +262,7 @@ void binaryNameToPathShouldReturnTheExpectedOutput(
262262
String binaryName,
263263
Kind kind,
264264
String expected
265-
) throws IOException {
265+
) {
266266
try (var fs = someTemporaryFileSystem()) {
267267
// Given
268268
var directoryPath = fs.getFileSystem().getPath(directory);
@@ -293,7 +293,7 @@ void packageNameToPathShouldReturnTheExpectedOutput(
293293
String directory,
294294
String packageName,
295295
String expected
296-
) throws IOException {
296+
) {
297297
try (var fs = someTemporaryFileSystem()) {
298298
// Given
299299
var directoryPath = fs.getFileSystem().getPath(directory);
@@ -325,7 +325,7 @@ void simpleClassNameToPathShouldReturnTheExpectedOutput(
325325
String className,
326326
Kind kind,
327327
String expected
328-
) throws IOException {
328+
) {
329329
try (var fs = someTemporaryFileSystem()) {
330330
// Given
331331
var packageDirectoryPath = fs.getFileSystem().getPath(packageDirectory);
@@ -364,7 +364,7 @@ void resourceNameToPathShouldReturnTheExpectedOutput(
364364
String packageName,
365365
String relativeName,
366366
String expected
367-
) throws IOException {
367+
) {
368368
try (var fs = someTemporaryFileSystem()) {
369369
// Given
370370
var directoryPath = fs.getFileSystem().getPath(directory);
@@ -391,7 +391,7 @@ void relativeResourceNameToPathShouldReturnTheExpectedOutput(
391391
String fragment,
392392
String[] fragments,
393393
String expected
394-
) throws IOException {
394+
) {
395395
try (var fs = someTemporaryFileSystem()) {
396396
// Given
397397
var directoryPath = fs.getFileSystem().getPath(directory);
@@ -437,7 +437,7 @@ void relativeResourceNameToPathShouldReturnTheExpectedOutput(
437437
"'', OTHER",
438438
})
439439
@ParameterizedTest(name = "pathToKind(\"{0}\") should return Kind.{1}")
440-
void pathToKindReturnsTheExpectedOutput(String pathName, Kind expectedKind) throws IOException {
440+
void pathToKindReturnsTheExpectedOutput(String pathName, Kind expectedKind) {
441441
try (var fs = someTemporaryFileSystem()) {
442442
// Given
443443
var path = fs.getFileSystem().getPath(pathName);
@@ -460,8 +460,7 @@ void pathToKindReturnsTheExpectedOutput(String pathName, Kind expectedKind) thro
460460
@ParameterizedTest(
461461
name = "fileWithAnyKind([{0}])'s predicate should return false for non-existing path \"{1}\""
462462
)
463-
void fileWithAnyKindShouldFailIfThePathDoesNotExist(Kind kind, String pathName)
464-
throws IOException {
463+
void fileWithAnyKindShouldFailIfThePathDoesNotExist(Kind kind, String pathName) {
465464
try (var fs = someTemporaryFileSystem()) {
466465
// Given
467466
var path = fs.getFileSystem().getPath(pathName);

0 commit comments

Comments
 (0)