Skip to content

Commit 52d7bf7

Browse files
committed
Fix typo, add Unittest
1 parent 02338b9 commit 52d7bf7

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.gradle.work.FileChange;
3636
import org.gradle.work.InputChanges;
3737

38+
import com.diffplug.common.annotations.VisibleForTesting;
3839
import com.diffplug.common.base.StringPrinter;
3940
import com.diffplug.spotless.Formatter;
4041
import com.diffplug.spotless.PaddedCell;
@@ -92,7 +93,8 @@ public void performAction(InputChanges inputs) throws Exception {
9293
}
9394
}
9495

95-
private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File input) throws IOException {
96+
@VisibleForTesting
97+
void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File input) throws IOException {
9698
File output = getOutputFile(input);
9799
getLogger().debug("Applying format to {} and writing to {}", input, output);
98100
PaddedCell.DirtyState dirtyState;
@@ -111,7 +113,7 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
111113
// Remove previous output if it exists
112114
Files.deleteIfExists(output.toPath());
113115
} else if (dirtyState.didNotConverge()) {
114-
getLogger().warn("Skipping '{}}' because it does not converge. Run {@code spotlessDiagnose} to understand why", input);
116+
getLogger().warn("Skipping '{}' because it does not converge. Run {@code spotlessDiagnose} to understand why", input);
115117
} else {
116118
Path parentDir = output.toPath().getParent();
117119
if (parentDir == null) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import java.io.File;
19+
20+
import org.assertj.core.api.Assertions;
21+
import org.gradle.api.file.DirectoryProperty;
22+
import org.gradle.api.logging.Logger;
23+
import org.junit.jupiter.api.Test;
24+
import org.mockito.Mockito;
25+
26+
import com.diffplug.spotless.Formatter;
27+
28+
public class SpotlessTaskImplTest {
29+
@Test
30+
public void testThrowsMessageContainsFilename() throws Exception {
31+
SpotlessTaskImpl task = Mockito.mock(SpotlessTaskImpl.class, Mockito.CALLS_REAL_METHODS);
32+
Mockito.when(task.getLogger()).thenReturn(Mockito.mock(Logger.class));
33+
34+
File projectDir = new File("unitTests/projectDir");
35+
DirectoryProperty projectDirProperty = Mockito.mock(DirectoryProperty.class, Mockito.RETURNS_DEEP_STUBS);
36+
Mockito.when(projectDirProperty.get().getAsFile()).thenReturn(projectDir);
37+
38+
Mockito.when(task.getProjectDir()).thenReturn(projectDirProperty);
39+
40+
File input = new File("unitTests/projectDir/someInput");
41+
Formatter formatter = Mockito.mock(Formatter.class);
42+
43+
Assertions.assertThatThrownBy(() -> task.processInputFile(null, formatter, input)).hasMessageContaining("unitTests/projectDir/someInput");
44+
}
45+
}

0 commit comments

Comments
 (0)