|
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.FileSystems; |
20 | 22 | import java.nio.file.Path;
|
21 | 23 | import java.nio.file.Paths;
|
22 | 24 | import java.util.ArrayList;
|
| 25 | +import java.util.Collections; |
23 | 26 | import java.util.List;
|
24 | 27 |
|
25 | 28 | import org.junit.jupiter.api.Assertions;
|
26 | 29 | import org.junit.jupiter.api.Test;
|
| 30 | +import org.mockito.Mockito; |
27 | 31 |
|
28 | 32 | import com.diffplug.common.base.StandardSystemProperty;
|
29 | 33 | import com.diffplug.spotless.generic.EndWithNewlineStep;
|
@@ -89,4 +93,64 @@ protected Formatter create() {
|
89 | 93 | }
|
90 | 94 | }.testEquals();
|
91 | 95 | }
|
| 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 | + |
92 | 156 | }
|
0 commit comments