|
1 | 1 | /* |
2 | | - * Copyright 2016-2022 DiffPlug |
| 2 | + * Copyright 2016-2023 DiffPlug |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
15 | 15 | */ |
16 | 16 | package com.diffplug.spotless; |
17 | 17 |
|
| 18 | +import java.io.File; |
18 | 19 | import java.nio.charset.Charset; |
19 | 20 | import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.FileSystem; |
| 22 | +import java.nio.file.FileSystems; |
20 | 23 | import java.nio.file.Path; |
21 | 24 | import java.nio.file.Paths; |
22 | 25 | import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
23 | 27 | import java.util.List; |
24 | 28 |
|
25 | 29 | import org.junit.jupiter.api.Assertions; |
26 | 30 | import org.junit.jupiter.api.Test; |
| 31 | +import org.mockito.Mockito; |
27 | 32 |
|
28 | 33 | import com.diffplug.common.base.StandardSystemProperty; |
29 | 34 | import com.diffplug.spotless.generic.EndWithNewlineStep; |
@@ -89,4 +94,95 @@ protected Formatter create() { |
89 | 94 | } |
90 | 95 | }.testEquals(); |
91 | 96 | } |
| 97 | + |
| 98 | + // new File("") as filePath is known to fail |
| 99 | + @Test |
| 100 | + public void testExceptionWithEmptyPath() throws Exception { |
| 101 | + LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy(); |
| 102 | + Charset encoding = StandardCharsets.UTF_8; |
| 103 | + FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError(); |
| 104 | + |
| 105 | + Path rootDir = Paths.get(StandardSystemProperty.USER_DIR.value()); |
| 106 | + |
| 107 | + FormatterStep step = Mockito.mock(FormatterStep.class); |
| 108 | + Mockito.when(step.getName()).thenReturn("someFailingStep"); |
| 109 | + Mockito.when(step.format(Mockito.anyString(), Mockito.any(File.class))).thenThrow(new IllegalArgumentException("someReason")); |
| 110 | + List<FormatterStep> steps = Collections.singletonList(step); |
| 111 | + |
| 112 | + Formatter formatter = Formatter.builder() |
| 113 | + .lineEndingsPolicy(lineEndingsPolicy) |
| 114 | + .encoding(encoding) |
| 115 | + .rootDir(rootDir) |
| 116 | + .steps(steps) |
| 117 | + .exceptionPolicy(exceptionPolicy) |
| 118 | + .build(); |
| 119 | + |
| 120 | + Assertions.assertThrows(IllegalArgumentException.class, () -> formatter.compute("someFileContent", new File(""))); |
| 121 | + } |
| 122 | + |
| 123 | + // If there is no File actually holding the content, one may rely on Formatter.NO_FILE_ON_DISK |
| 124 | + @Test |
| 125 | + public void testExceptionWithSentinelNoFileOnDisk() throws Exception { |
| 126 | + LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy(); |
| 127 | + Charset encoding = StandardCharsets.UTF_8; |
| 128 | + FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError(); |
| 129 | + |
| 130 | + Path rootDir = Paths.get(StandardSystemProperty.USER_DIR.value()); |
| 131 | + |
| 132 | + FormatterStep step = Mockito.mock(FormatterStep.class); |
| 133 | + Mockito.when(step.getName()).thenReturn("someFailingStep"); |
| 134 | + Mockito.when(step.format(Mockito.anyString(), Mockito.any(File.class))).thenThrow(new IllegalArgumentException("someReason")); |
| 135 | + List<FormatterStep> steps = Collections.singletonList(step); |
| 136 | + |
| 137 | + Formatter formatter = Formatter.builder() |
| 138 | + .lineEndingsPolicy(lineEndingsPolicy) |
| 139 | + .encoding(encoding) |
| 140 | + .rootDir(rootDir) |
| 141 | + .steps(steps) |
| 142 | + .exceptionPolicy(exceptionPolicy) |
| 143 | + .build(); |
| 144 | + |
| 145 | + formatter.compute("someFileContent", Formatter.NO_FILE_SENTINEL); |
| 146 | + } |
| 147 | + |
| 148 | + // rootDir may be a path not from the default FileSystem |
| 149 | + @Test |
| 150 | + public void testExceptionWithRootDirIsNotFileSystem() throws Exception { |
| 151 | + LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy(); |
| 152 | + Charset encoding = StandardCharsets.UTF_8; |
| 153 | + FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError(); |
| 154 | + |
| 155 | + Path rootDir = Mockito.mock(Path.class); |
| 156 | + FileSystem customFileSystem = Mockito.mock(FileSystem.class); |
| 157 | + Mockito.when(rootDir.getFileSystem()).thenReturn(customFileSystem); |
| 158 | + |
| 159 | + Path pathFromFile = Mockito.mock(Path.class); |
| 160 | + Mockito.when(customFileSystem.getPath(Mockito.anyString())).thenReturn(pathFromFile); |
| 161 | + |
| 162 | + Path relativized = Mockito.mock(Path.class); |
| 163 | + Mockito.when(rootDir.relativize(Mockito.any(Path.class))).then(invok -> { |
| 164 | + Path filePath = invok.getArgument(0); |
| 165 | + if (filePath.getFileSystem() == FileSystems.getDefault()) { |
| 166 | + throw new IllegalArgumentException("Can not relativize through different FileSystems"); |
| 167 | + } |
| 168 | + |
| 169 | + return relativized; |
| 170 | + }); |
| 171 | + |
| 172 | + FormatterStep step = Mockito.mock(FormatterStep.class); |
| 173 | + Mockito.when(step.getName()).thenReturn("someFailingStep"); |
| 174 | + Mockito.when(step.format(Mockito.anyString(), Mockito.any(File.class))).thenThrow(new IllegalArgumentException("someReason")); |
| 175 | + List<FormatterStep> steps = Collections.singletonList(step); |
| 176 | + |
| 177 | + Formatter formatter = Formatter.builder() |
| 178 | + .lineEndingsPolicy(lineEndingsPolicy) |
| 179 | + .encoding(encoding) |
| 180 | + .rootDir(rootDir) |
| 181 | + .steps(steps) |
| 182 | + .exceptionPolicy(exceptionPolicy) |
| 183 | + .build(); |
| 184 | + |
| 185 | + formatter.compute("someFileContent", new File("/some/folder/some.file")); |
| 186 | + } |
| 187 | + |
92 | 188 | } |
0 commit comments