1
1
/*
2
- * Copyright 2016-2021 DiffPlug
2
+ * Copyright 2016-2024 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.
19
19
import java .io .IOException ;
20
20
import java .io .Writer ;
21
21
import java .nio .charset .StandardCharsets ;
22
+ import java .util .stream .Stream ;
22
23
23
24
import org .assertj .core .api .Assertions ;
25
+ import org .gradle .testkit .runner .GradleRunner ;
24
26
import org .junit .jupiter .api .BeforeEach ;
25
- import org .junit .jupiter .api .Test ;
27
+ import org .junit .jupiter .api .TestInstance ;
28
+ import org .junit .jupiter .params .ParameterizedTest ;
29
+ import org .junit .jupiter .params .provider .MethodSource ;
26
30
27
31
import com .diffplug .common .base .StringPrinter ;
28
32
import com .diffplug .common .io .Files ;
29
33
34
+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
30
35
class IdeHookTest extends GradleIntegrationHarness {
31
36
private String output , error ;
32
37
private File dirty , clean , diverge , outofbounds ;
@@ -40,11 +45,11 @@ void before() throws IOException {
40
45
"spotless {" ,
41
46
" format 'misc', {" ,
42
47
" target 'DIRTY.md', 'CLEAN.md'" ,
43
- " custom 'lowercase', { str -> str.toLowerCase(Locale.ROOT) } " ,
48
+ " addStep com.diffplug.spotless.TestingOnly.lowercase() " ,
44
49
" }" ,
45
50
" format 'diverge', {" ,
46
51
" target 'DIVERGE.md'" ,
47
- " custom ' diverge', { str -> str + ' ' } " ,
52
+ " addStep com.diffplug.spotless.TestingOnly. diverge() " ,
48
53
" }" ,
49
54
"}" );
50
55
dirty = new File (rootFolder (), "DIRTY.md" );
@@ -57,12 +62,16 @@ void before() throws IOException {
57
62
Files .write ("ABC" .getBytes (StandardCharsets .UTF_8 ), outofbounds );
58
63
}
59
64
60
- private void runWith (String ... arguments ) throws IOException {
65
+ private static Stream <Boolean > configurationCacheProvider () {
66
+ return Stream .of (false , true );
67
+ }
68
+
69
+ private void runWith (boolean configurationCache , String ... arguments ) throws IOException {
61
70
StringBuilder output = new StringBuilder ();
62
71
StringBuilder error = new StringBuilder ();
63
72
try (Writer outputWriter = new StringPrinter (output ::append ).toWriter ();
64
73
Writer errorWriter = new StringPrinter (error ::append ).toWriter ();) {
65
- gradleRunner ()
74
+ gradleRunner (configurationCache )
66
75
.withArguments (arguments )
67
76
.forwardStdOutput (outputWriter )
68
77
.forwardStdError (errorWriter )
@@ -72,37 +81,60 @@ private void runWith(String... arguments) throws IOException {
72
81
this .error = error .toString ();
73
82
}
74
83
75
- @ Test
76
- void dirty () throws IOException {
77
- runWith ("spotlessApply" , "--quiet" , "-PspotlessIdeHook=" + dirty .getAbsolutePath (), "-PspotlessIdeHookUseStdOut" );
84
+ protected GradleRunner gradleRunner (boolean configurationCache ) throws IOException {
85
+ if (configurationCache ) {
86
+ setFile ("gradle.properties" ).toContent ("org.gradle.unsafe.configuration-cache=true" );
87
+ setFile ("settings.gradle" ).toContent ("enableFeaturePreview(\" STABLE_CONFIGURATION_CACHE\" )" );
88
+ return super .gradleRunner ().withGradleVersion (GradleVersionSupport .STABLE_CONFIGURATION_CACHE .version );
89
+ } else {
90
+ File gradleProps = new File (rootFolder (), "gradle.properties" );
91
+ if (gradleProps .exists ()) {
92
+ gradleProps .delete ();
93
+ }
94
+ File settingsGradle = new File (rootFolder (), "settings.gradle" );
95
+ if (settingsGradle .exists ()) {
96
+ settingsGradle .delete ();
97
+ }
98
+ return super .gradleRunner ();
99
+ }
100
+ }
101
+
102
+ @ ParameterizedTest
103
+ @ MethodSource ("configurationCacheProvider" )
104
+ void dirty (boolean configurationCache ) throws IOException {
105
+ runWith (configurationCache , "spotlessApply" , "--quiet" , "-PspotlessIdeHook=" + dirty .getAbsolutePath (), "-PspotlessIdeHookUseStdOut" );
78
106
Assertions .assertThat (output ).isEqualTo ("abc" );
79
107
Assertions .assertThat (error ).startsWith ("IS DIRTY" );
80
108
}
81
109
82
- @ Test
83
- void clean () throws IOException {
84
- runWith ("spotlessApply" , "--quiet" , "-PspotlessIdeHook=" + clean .getAbsolutePath (), "-PspotlessIdeHookUseStdOut" );
110
+ @ ParameterizedTest
111
+ @ MethodSource ("configurationCacheProvider" )
112
+ void clean (boolean configurationCache ) throws IOException {
113
+ runWith (configurationCache , "spotlessApply" , "--quiet" , "-PspotlessIdeHook=" + clean .getAbsolutePath (), "-PspotlessIdeHookUseStdOut" );
85
114
Assertions .assertThat (output ).isEmpty ();
86
115
Assertions .assertThat (error ).startsWith ("IS CLEAN" );
87
116
}
88
117
89
- @ Test
90
- void diverge () throws IOException {
91
- runWith ("spotlessApply" , "--quiet" , "-PspotlessIdeHook=" + diverge .getAbsolutePath (), "-PspotlessIdeHookUseStdOut" );
118
+ @ ParameterizedTest
119
+ @ MethodSource ("configurationCacheProvider" )
120
+ void diverge (boolean configurationCache ) throws IOException {
121
+ runWith (configurationCache , "spotlessApply" , "--quiet" , "-PspotlessIdeHook=" + diverge .getAbsolutePath (), "-PspotlessIdeHookUseStdOut" );
92
122
Assertions .assertThat (output ).isEmpty ();
93
123
Assertions .assertThat (error ).startsWith ("DID NOT CONVERGE" );
94
124
}
95
125
96
- @ Test
97
- void outofbounds () throws IOException {
98
- runWith ("spotlessApply" , "--quiet" , "-PspotlessIdeHook=" + outofbounds .getAbsolutePath (), "-PspotlessIdeHookUseStdOut" );
126
+ @ ParameterizedTest
127
+ @ MethodSource ("configurationCacheProvider" )
128
+ void outofbounds (boolean configurationCache ) throws IOException {
129
+ runWith (configurationCache , "spotlessApply" , "--quiet" , "-PspotlessIdeHook=" + outofbounds .getAbsolutePath (), "-PspotlessIdeHookUseStdOut" );
99
130
Assertions .assertThat (output ).isEmpty ();
100
131
Assertions .assertThat (error ).isEmpty ();
101
132
}
102
133
103
- @ Test
104
- void notAbsolute () throws IOException {
105
- runWith ("spotlessApply" , "--quiet" , "-PspotlessIdeHook=build.gradle" , "-PspotlessIdeHookUseStdOut" );
134
+ @ ParameterizedTest
135
+ @ MethodSource ("configurationCacheProvider" )
136
+ void notAbsolute (boolean configurationCache ) throws IOException {
137
+ runWith (configurationCache , "spotlessApply" , "--quiet" , "-PspotlessIdeHook=build.gradle" , "-PspotlessIdeHookUseStdOut" );
106
138
Assertions .assertThat (output ).isEmpty ();
107
139
Assertions .assertThat (error ).contains ("Argument passed to spotlessIdeHook must be an absolute path" );
108
140
}
0 commit comments