11/*
2- * Copyright 2016-2021 DiffPlug
2+ * Copyright 2016-2024 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1919import java .io .IOException ;
2020import java .io .Writer ;
2121import java .nio .charset .StandardCharsets ;
22+ import java .util .stream .Stream ;
2223
2324import org .assertj .core .api .Assertions ;
25+ import org .gradle .testkit .runner .GradleRunner ;
2426import 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 ;
2630
2731import com .diffplug .common .base .StringPrinter ;
2832import com .diffplug .common .io .Files ;
2933
34+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
3035class IdeHookTest extends GradleIntegrationHarness {
3136 private String output , error ;
3237 private File dirty , clean , diverge , outofbounds ;
@@ -40,11 +45,11 @@ void before() throws IOException {
4045 "spotless {" ,
4146 " format 'misc', {" ,
4247 " target 'DIRTY.md', 'CLEAN.md'" ,
43- " custom 'lowercase', { str -> str.toLowerCase(Locale.ROOT) } " ,
48+ " addStep com.diffplug.spotless.TestingOnly.lowercase() " ,
4449 " }" ,
4550 " format 'diverge', {" ,
4651 " target 'DIVERGE.md'" ,
47- " custom ' diverge', { str -> str + ' ' } " ,
52+ " addStep com.diffplug.spotless.TestingOnly. diverge() " ,
4853 " }" ,
4954 "}" );
5055 dirty = new File (rootFolder (), "DIRTY.md" );
@@ -57,12 +62,16 @@ void before() throws IOException {
5762 Files .write ("ABC" .getBytes (StandardCharsets .UTF_8 ), outofbounds );
5863 }
5964
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 {
6170 StringBuilder output = new StringBuilder ();
6271 StringBuilder error = new StringBuilder ();
6372 try (Writer outputWriter = new StringPrinter (output ::append ).toWriter ();
6473 Writer errorWriter = new StringPrinter (error ::append ).toWriter ();) {
65- gradleRunner ()
74+ gradleRunner (configurationCache )
6675 .withArguments (arguments )
6776 .forwardStdOutput (outputWriter )
6877 .forwardStdError (errorWriter )
@@ -72,37 +81,60 @@ private void runWith(String... arguments) throws IOException {
7281 this .error = error .toString ();
7382 }
7483
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" );
78106 Assertions .assertThat (output ).isEqualTo ("abc" );
79107 Assertions .assertThat (error ).startsWith ("IS DIRTY" );
80108 }
81109
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" );
85114 Assertions .assertThat (output ).isEmpty ();
86115 Assertions .assertThat (error ).startsWith ("IS CLEAN" );
87116 }
88117
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" );
92122 Assertions .assertThat (output ).isEmpty ();
93123 Assertions .assertThat (error ).startsWith ("DID NOT CONVERGE" );
94124 }
95125
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" );
99130 Assertions .assertThat (output ).isEmpty ();
100131 Assertions .assertThat (error ).isEmpty ();
101132 }
102133
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" );
106138 Assertions .assertThat (output ).isEmpty ();
107139 Assertions .assertThat (error ).contains ("Argument passed to spotlessIdeHook must be an absolute path" );
108140 }
0 commit comments