Skip to content

Commit e429f9f

Browse files
authored
Merge branch 'master' into feature/fix-padded-cell
2 parents 60eadd9 + a523231 commit e429f9f

File tree

10 files changed

+76
-54
lines changed

10 files changed

+76
-54
lines changed

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ before_install:
99
install: true
1010
script:
1111
- ./.ci/ci.sh
12-
# lines commented out below are a temporary workaround for https://github.com/diffplug/spotless/issues/429
1312
before_cache:
14-
# - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
15-
# - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
13+
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
14+
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
1615
- rm -fr plugin-maven/build/localMavenRepository/com/diffplug/spotless/
1716
cache:
1817
directories:
19-
# - $HOME/.gradle/caches/
20-
# - $HOME/.gradle/wrapper/
18+
- $HOME/.gradle/caches/
19+
- $HOME/.gradle/wrapper/
2120
- $HOME/.m2/
2221
- plugin-maven/build/localMavenRepository/

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ You might be looking for:
1717
- this led to "check" says all good, but then "apply" still causes format (https://github.com/diffplug/spotless/issues/453)
1818
- combined with up-to-date checking, could lead to even more confusing results (https://github.com/diffplug/spotless/issues/338)
1919
- only affects the gradle plugin, since that was the only plugin to use this feature
20+
* Minor change to `TestProvisioner`, which should fix the cache-breaking issues, allowing us to speed-up the CI builds a bit.
2021

2122
### Version 1.24.1 - August 12th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.24.1/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.24.1/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))
2223

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name=spotless
22
description=Spotless - keep your code spotless with Gradle
33
org=diffplug
44

5+
org.gradle.parallel=false
6+
57
group=com.diffplug.spotless
68

79
versionLib=1.25.0-SNAPSHOT

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import org.assertj.core.api.Assertions;
2828
import org.gradle.api.GradleException;
2929
import org.gradle.api.Project;
30-
import org.gradle.testfixtures.ProjectBuilder;
3130
import org.junit.Test;
3231

3332
import com.diffplug.common.base.StringPrinter;
3433
import com.diffplug.spotless.FormatterStep;
3534
import com.diffplug.spotless.LineEnding;
3635
import com.diffplug.spotless.ResourceHarness;
36+
import com.diffplug.spotless.TestProvisioner;
3737
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
3838

3939
public class DiffMessageFormatterTest extends ResourceHarness {
@@ -42,7 +42,7 @@ private SpotlessTask create(File... files) throws IOException {
4242
}
4343

4444
private SpotlessTask create(List<File> files) throws IOException {
45-
Project project = ProjectBuilder.builder().withProjectDir(rootFolder()).build();
45+
Project project = TestProvisioner.gradleProject(rootFolder());
4646
SpotlessTask task = project.getTasks().create("underTest", SpotlessTask.class);
4747
task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
4848
task.setTarget(files);

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FileTreeTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,18 @@
2323
import org.assertj.core.api.Assertions;
2424
import org.gradle.api.Project;
2525
import org.gradle.api.file.ConfigurableFileTree;
26-
import org.gradle.testfixtures.ProjectBuilder;
2726
import org.junit.Before;
2827
import org.junit.Test;
2928

3029
import com.diffplug.spotless.ResourceHarness;
30+
import com.diffplug.spotless.TestProvisioner;
3131

3232
public class FileTreeTest extends ResourceHarness {
3333
private ConfigurableFileTree fileTree;
3434

3535
@Before
3636
public void fileTree() throws IOException {
37-
Project project = ProjectBuilder.builder()
38-
.withProjectDir(rootFolder())
39-
.build();
37+
Project project = TestProvisioner.gradleProject(rootFolder());
4038
fileTree = project.fileTree(rootFolder());
4139
fileTree.exclude("userHome"); // somehow we're getting userHome\native\19\windows-amd64\native-platform.dll
4240
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424
import org.assertj.core.api.Assertions;
2525
import org.gradle.api.GradleException;
2626
import org.gradle.api.Project;
27-
import org.gradle.testfixtures.ProjectBuilder;
2827
import org.junit.Before;
2928
import org.junit.Test;
3029

3130
import com.diffplug.spotless.FormatterStep;
3231
import com.diffplug.spotless.LineEnding;
3332
import com.diffplug.spotless.ResourceHarness;
33+
import com.diffplug.spotless.TestProvisioner;
3434

3535
public class FormatTaskTest extends ResourceHarness {
3636
private SpotlessTask checkTask;
3737
private SpotlessTask applyTask;
3838

3939
@Before
40-
public void createTask() {
41-
Project project = ProjectBuilder.builder().build();
40+
public void createTask() throws IOException {
41+
Project project = TestProvisioner.gradleProject(rootFolder());
4242
checkTask = project.getTasks().create("checkTaskUnderTest", SpotlessTask.class);
4343
checkTask.setCheck();
4444
applyTask = project.getTasks().create("applyTaskUnderTest", SpotlessTask.class);

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import org.assertj.core.api.Assertions;
2727
import org.gradle.api.Project;
28-
import org.gradle.testfixtures.ProjectBuilder;
2928
import org.junit.Assert;
3029
import org.junit.Test;
3130

@@ -35,6 +34,7 @@
3534
import com.diffplug.spotless.FormatterStep;
3635
import com.diffplug.spotless.LineEnding;
3736
import com.diffplug.spotless.ResourceHarness;
37+
import com.diffplug.spotless.TestProvisioner;
3838

3939
public class PaddedCellTaskTest extends ResourceHarness {
4040
private static final boolean IS_WIN = StandardSystemProperty.OS_NAME.value().toLowerCase(Locale.US).contains("win");
@@ -44,7 +44,7 @@ private static String slashify(String input) {
4444
}
4545

4646
private class Bundle {
47-
Project project = ProjectBuilder.builder().withProjectDir(rootFolder()).build();
47+
Project project = TestProvisioner.gradleProject(rootFolder());
4848
File file;
4949
SpotlessTask check;
5050
SpotlessTask apply;

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SelfTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
import org.gradle.api.Project;
2222
import org.gradle.api.tasks.incremental.IncrementalTaskInputs;
23-
import org.gradle.testfixtures.ProjectBuilder;
2423
import org.gradle.testkit.runner.GradleRunner;
2524
import org.junit.Ignore;
2625
import org.junit.Test;
2726

2827
import com.diffplug.common.base.Errors;
2928
import com.diffplug.common.base.StandardSystemProperty;
29+
import com.diffplug.spotless.TestProvisioner;
3030

3131
/**
3232
* If you'd like to step through the full spotless plugin,
@@ -117,7 +117,7 @@ private static void runTasksManually(Type type) throws Exception {
117117

118118
/** Creates a Project which has had the SpotlessExtension setup. */
119119
private static Project createProject(Consumer<SpotlessExtension> test) throws Exception {
120-
Project project = ProjectBuilder.builder().withProjectDir(new File("").getAbsoluteFile()).build();
120+
Project project = TestProvisioner.gradleProject(new File("").getAbsoluteFile());
121121
// create the spotless plugin
122122
SpotlessPlugin plugin = project.getPlugins().apply(SpotlessPlugin.class);
123123
// setup the plugin

testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.junit.rules.TestWatcher;
4040
import org.junit.runner.Description;
4141

42+
import com.diffplug.common.base.Errors;
4243
import com.diffplug.common.io.Resources;
4344

4445
public class ResourceHarness {
@@ -77,8 +78,8 @@ private void logFailure(String message, Description description) {
7778
};
7879

7980
/** Returns the root folder (canonicalized to fix OS X issue) */
80-
protected File rootFolder() throws IOException {
81-
return folderDontUseDirectly.getRoot().getCanonicalFile();
81+
protected File rootFolder() {
82+
return Errors.rethrow().get(() -> folderDontUseDirectly.getRoot().getCanonicalFile());
8283
}
8384

8485
/** Returns a new child of the root folder. */

testlib/src/main/java/com/diffplug/spotless/TestProvisioner.java

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.IOException;
2020
import java.io.ObjectInputStream;
2121
import java.io.ObjectOutputStream;
22+
import java.nio.file.Path;
23+
import java.util.Comparator;
2224
import java.util.HashMap;
2325
import java.util.Map;
2426
import java.util.function.Consumer;
@@ -31,14 +33,21 @@
3133
import org.gradle.api.artifacts.dsl.RepositoryHandler;
3234
import org.gradle.testfixtures.ProjectBuilder;
3335

34-
import com.diffplug.common.base.Box;
3536
import com.diffplug.common.base.Errors;
3637
import com.diffplug.common.base.StandardSystemProperty;
3738
import com.diffplug.common.base.Suppliers;
3839
import com.diffplug.common.collect.ImmutableSet;
3940
import com.diffplug.common.io.Files;
4041

4142
public class TestProvisioner {
43+
public static Project gradleProject(File dir) {
44+
File userHome = new File(StandardSystemProperty.USER_HOME.value());
45+
return ProjectBuilder.builder()
46+
.withGradleUserHomeDir(new File(userHome, ".gradle"))
47+
.withProjectDir(dir)
48+
.build();
49+
}
50+
4251
/**
4352
* Creates a Provisioner for the given repositories.
4453
*
@@ -47,26 +56,35 @@ public class TestProvisioner {
4756
*
4857
* Every call to resolve will take about 1 second, even when all artifacts are resolved.
4958
*/
50-
private static Supplier<Provisioner> createLazyWithRepositories(Consumer<RepositoryHandler> repoConfig) {
59+
private static Provisioner createWithRepositories(Consumer<RepositoryHandler> repoConfig) {
5160
// Running this takes ~3 seconds the first time it is called. Probably because of classloading.
52-
return Suppliers.memoize(() -> {
53-
Project project = ProjectBuilder.builder().build();
54-
repoConfig.accept(project.getRepositories());
55-
return (withTransitives, mavenCoords) -> {
56-
Dependency[] deps = mavenCoords.stream()
57-
.map(project.getDependencies()::create)
58-
.toArray(Dependency[]::new);
59-
Configuration config = project.getConfigurations().detachedConfiguration(deps);
60-
config.setTransitive(withTransitives);
61-
config.setDescription(mavenCoords.toString());
61+
File tempDir = Files.createTempDir();
62+
Project project = TestProvisioner.gradleProject(tempDir);
63+
repoConfig.accept(project.getRepositories());
64+
return (withTransitives, mavenCoords) -> {
65+
Dependency[] deps = mavenCoords.stream()
66+
.map(project.getDependencies()::create)
67+
.toArray(Dependency[]::new);
68+
Configuration config = project.getConfigurations().detachedConfiguration(deps);
69+
config.setTransitive(withTransitives);
70+
config.setDescription(mavenCoords.toString());
71+
try {
72+
return config.resolve();
73+
} catch (ResolveException e) {
74+
/* Provide Maven coordinates in exception message instead of static string 'detachedConfiguration' */
75+
throw new ResolveException(config.getDescription(), e);
76+
} finally {
77+
// delete the temp dir
6278
try {
63-
return config.resolve();
64-
} catch (ResolveException e) {
65-
/* Provide Maven coordinates in exception message instead of static string 'detachedConfiguration' */
66-
throw new ResolveException(config.getDescription(), e);
79+
java.nio.file.Files.walk(tempDir.toPath())
80+
.sorted(Comparator.reverseOrder())
81+
.map(Path::toFile)
82+
.forEach(File::delete);
83+
} catch (IOException e) {
84+
throw Errors.asRuntime(e);
6785
}
68-
};
69-
});
86+
}
87+
};
7088
}
7189

7290
/** Creates a Provisioner which will cache the result of previous calls. */
@@ -86,20 +104,23 @@ private static Provisioner caching(String name, Supplier<Provisioner> input) {
86104
} else {
87105
cached = new HashMap<>();
88106
}
89-
return (withTransitives, mavenCoords) -> {
90-
Box<Boolean> wasChanged = Box.of(false);
91-
ImmutableSet<File> result = cached.computeIfAbsent(ImmutableSet.copyOf(mavenCoords), coords -> {
92-
wasChanged.set(true);
93-
return ImmutableSet.copyOf(input.get().provisionWithTransitives(withTransitives, coords));
94-
});
95-
if (wasChanged.get()) {
96-
try (ObjectOutputStream outputStream = new ObjectOutputStream(Files.asByteSink(cacheFile).openBufferedStream())) {
97-
outputStream.writeObject(cached);
98-
} catch (IOException e) {
99-
throw Errors.asRuntime(e);
107+
return (withTransitives, mavenCoordsRaw) -> {
108+
ImmutableSet<String> mavenCoords = ImmutableSet.copyOf(mavenCoordsRaw);
109+
synchronized (TestProvisioner.class) {
110+
ImmutableSet<File> result = cached.get(mavenCoords);
111+
// double-check that depcache pruning hasn't removed them since our cache cached them
112+
boolean needsToBeSet = result == null || !result.stream().allMatch(file -> file.exists() && file.isFile() && file.length() > 0);
113+
if (needsToBeSet) {
114+
result = ImmutableSet.copyOf(input.get().provisionWithTransitives(withTransitives, mavenCoords));
115+
cached.put(mavenCoords, result);
116+
try (ObjectOutputStream outputStream = new ObjectOutputStream(Files.asByteSink(cacheFile).openBufferedStream())) {
117+
outputStream.writeObject(cached);
118+
} catch (IOException e) {
119+
throw Errors.asRuntime(e);
120+
}
100121
}
122+
return result;
101123
}
102-
return result;
103124
};
104125
}
105126

@@ -109,7 +130,7 @@ public static Provisioner jcenter() {
109130
}
110131

111132
private static final Supplier<Provisioner> jcenter = Suppliers.memoize(() -> {
112-
return caching("jcenter", createLazyWithRepositories(repo -> repo.jcenter()));
133+
return caching("jcenter", () -> createWithRepositories(repo -> repo.jcenter()));
113134
});
114135

115136
/** Creates a Provisioner for the mavenCentral repo. */
@@ -118,22 +139,22 @@ public static Provisioner mavenCentral() {
118139
}
119140

120141
private static final Supplier<Provisioner> mavenCentral = Suppliers.memoize(() -> {
121-
return caching("mavenCentral", createLazyWithRepositories(repo -> repo.mavenCentral()));
142+
return caching("mavenCentral", () -> createWithRepositories(repo -> repo.mavenCentral()));
122143
});
123144

124145
/** Creates a Provisioner for the local maven repo for development purpose. */
125146
public static Provisioner mavenLocal() {
126147
return mavenLocal.get();
127148
}
128149

129-
private static final Supplier<Provisioner> mavenLocal = createLazyWithRepositories(repo -> repo.mavenLocal());
150+
private static final Supplier<Provisioner> mavenLocal = () -> createWithRepositories(repo -> repo.mavenLocal());
130151

131152
/** Creates a Provisioner for the Sonatype snapshots maven repo for development purpose. */
132153
public static Provisioner snapshots() {
133154
return snapshots.get();
134155
}
135156

136-
private static final Supplier<Provisioner> snapshots = createLazyWithRepositories(repo -> {
157+
private static final Supplier<Provisioner> snapshots = () -> createWithRepositories(repo -> {
137158
repo.maven(setup -> {
138159
setup.setUrl("https://oss.sonatype.org/content/repositories/snapshots");
139160
});

0 commit comments

Comments
 (0)