Skip to content

Commit 89332e2

Browse files
author
Dave Syer
committed
Speed up grab command test
The autoconfiguration transformations (and loads of grabs of spring-boot snapshots) were making the grab command tests run really slowly. Snapshots are particularly bad. Fixed by adding a --autoconfigure=false option to the compiler configuration and using it in that test.
1 parent fce48c0 commit 89332e2

File tree

11 files changed

+77
-26
lines changed

11 files changed

+77
-26
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CompilerOptionHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
* Groovy scripts
2626
*
2727
* @author Andy Wilkinson
28+
* @author Dave Syer
2829
*/
2930
public class CompilerOptionHandler extends OptionHandler {
3031

3132
private OptionSpec<Void> noGuessImportsOption;
3233

3334
private OptionSpec<Void> noGuessDependenciesOption;
3435

36+
private OptionSpec<Boolean> autoconfigureOption;
37+
3538
private OptionSpec<String> classpathOption;
3639

3740
@Override
@@ -40,6 +43,9 @@ protected final void options() {
4043
"Do not attempt to guess imports");
4144
this.noGuessDependenciesOption = option("no-guess-dependencies",
4245
"Do not attempt to guess dependencies");
46+
this.autoconfigureOption = option("autoconfigure",
47+
"Add autoconfigure compiler transformations").withOptionalArg()
48+
.ofType(Boolean.class).defaultsTo(true);
4349
this.classpathOption = option(asList("classpath", "cp"),
4450
"Additional classpath entries").withRequiredArg();
4551
doOptions();
@@ -60,4 +66,8 @@ public OptionSpec<String> getClasspathOption() {
6066
return this.classpathOption;
6167
}
6268

69+
public OptionSpec<Boolean> getAutoconfigureOption() {
70+
return this.autoconfigureOption;
71+
}
72+
6373
}

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/GrabCommand.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ protected void run(OptionSet options) throws Exception {
4949

5050
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
5151
.createDefaultRepositoryConfiguration();
52-
repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File(
53-
getM2HomeDirectory(), "repository").toURI(), true));
5452

5553
GroovyCompilerConfiguration configuration = new GroovyCompilerConfigurationAdapter(
5654
options, this, repositoryConfiguration);
@@ -59,16 +57,33 @@ protected void run(OptionSet options) throws Exception {
5957
System.setProperty("grape.root", ".");
6058
}
6159

60+
if (!new File(System.getProperty("grape.root")).equals(getM2HomeDirectory())) {
61+
GrabCommand.addDefaultCacheAsRespository(repositoryConfiguration);
62+
}
6263
GroovyCompiler groovyCompiler = new GroovyCompiler(configuration);
6364
groovyCompiler.compile(fileOptions.getFilesArray());
6465
}
6566

66-
private File getM2HomeDirectory() {
67-
String mavenRoot = System.getProperty("maven.home");
68-
if (StringUtils.hasLength(mavenRoot)) {
69-
return new File(mavenRoot);
70-
}
71-
return new File(System.getProperty("user.home"), ".m2");
67+
}
68+
69+
/**
70+
* Add the default local M2 cache directory as a remote repository. Only do this if
71+
* the local cache location has been changed from the default.
72+
*
73+
* @param repositoryConfiguration
74+
*/
75+
public static void addDefaultCacheAsRespository(
76+
List<RepositoryConfiguration> repositoryConfiguration) {
77+
repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File(
78+
getM2HomeDirectory(), "repository").toURI(), true));
79+
}
80+
81+
private static File getM2HomeDirectory() {
82+
String mavenRoot = System.getProperty("maven.home");
83+
if (StringUtils.hasLength(mavenRoot)) {
84+
return new File(mavenRoot);
7285
}
86+
return new File(System.getProperty("user.home"), ".m2");
7387
}
88+
7489
}

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/RunCommand.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.cli.command;
1818

1919
import java.awt.Desktop;
20-
import java.io.File;
2120
import java.util.List;
2221
import java.util.logging.Level;
2322

@@ -89,8 +88,6 @@ protected void run(OptionSet options) throws Exception {
8988

9089
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
9190
.createDefaultRepositoryConfiguration();
92-
repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File(
93-
"repository").toURI(), true));
9491

9592
SpringApplicationRunnerConfiguration configuration = new SpringApplicationRunnerConfigurationAdapter(
9693
options, this, repositoryConfiguration);

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ScriptCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ public boolean isGuessImports() {
264264
return true;
265265
}
266266

267+
@Override
268+
public boolean isAutoconfigure() {
269+
return true;
270+
}
271+
267272
@Override
268273
public boolean isGuessDependencies() {
269274
return true;

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.lang.reflect.Field;
2525
import java.util.ArrayList;
2626
import java.util.Arrays;
27+
import java.util.Collections;
2728
import java.util.LinkedList;
2829
import java.util.List;
2930
import java.util.ServiceLoader;
@@ -77,7 +78,7 @@ public class GroovyCompiler {
7778

7879
private final ExtendedGroovyClassLoader loader;
7980

80-
private final ServiceLoader<CompilerAutoConfiguration> compilerAutoConfigurations;
81+
private final Iterable<CompilerAutoConfiguration> compilerAutoConfigurations;
8182

8283
private final List<ASTTransformation> transformations;
8384

@@ -96,8 +97,14 @@ public GroovyCompiler(final GroovyCompilerConfiguration configuration) {
9697

9798
this.loader.getConfiguration().addCompilationCustomizers(
9899
new CompilerAutoConfigureCustomizer());
99-
this.compilerAutoConfigurations = ServiceLoader.load(
100-
CompilerAutoConfiguration.class, GroovyCompiler.class.getClassLoader());
100+
if (configuration.isAutoconfigure()) {
101+
this.compilerAutoConfigurations = ServiceLoader.load(
102+
CompilerAutoConfiguration.class,
103+
GroovyCompiler.class.getClassLoader());
104+
}
105+
else {
106+
this.compilerAutoConfigurations = Collections.emptySet();
107+
}
101108

102109
this.transformations = new ArrayList<ASTTransformation>();
103110
this.transformations.add(new GrabResolversAutoConfigurationTransformation());

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public interface GroovyCompilerConfiguration {
4848
*/
4949
boolean isGuessDependencies();
5050

51+
/**
52+
* Returns true if autoconfiguration transformations should be applied.
53+
*/
54+
boolean isAutoconfigure();
55+
5156
/**
5257
* @return a path for local resources
5358
*/

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfigurationAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public boolean isGuessDependencies() {
7171
return !this.options.has(this.optionHandler.getNoGuessDependenciesOption());
7272
}
7373

74+
@Override
75+
public boolean isAutoconfigure() {
76+
return this.optionHandler.getAutoconfigureOption().value(this.options);
77+
}
78+
7479
@Override
7580
public String[] getClasspath() {
7681
OptionSpec<String> classpathOption = this.optionHandler.getClasspathOption();

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/**
2626
* @author Andy Wilkinson
27+
* @author Dave Syer
2728
*/
2829
public final class RepositoryConfigurationFactory {
2930

@@ -51,4 +52,5 @@ public static List<RepositoryConfiguration> createDefaultRepositoryConfiguration
5152

5253
return repositoryConfiguration;
5354
}
55+
5456
}

spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ protected String[] getSources(String... args) {
9898
final String[] sources = new String[args.length];
9999
for (int i = 0; i < args.length; i++) {
100100
String arg = args[i];
101-
sources[i] = this.prefix + arg;
101+
if (arg.startsWith("--")) {
102+
sources[i] = arg;
103+
}
104+
else {
105+
sources[i] = this.prefix + arg;
106+
}
102107
}
103108
return sources;
104109
}

spring-boot-cli/src/test/java/org/springframework/boot/cli/GrabCommandIntegrationTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Integration tests for {@link GrabCommand}
3232
*
3333
* @author Andy Wilkinson
34+
* @author Dave Syer
3435
*/
3536
public class GrabCommandIntegrationTests {
3637

@@ -41,22 +42,21 @@ public class GrabCommandIntegrationTests {
4142
@After
4243
public void deleteLocalRepository() {
4344
FileSystemUtils.deleteRecursively(new File("target/repository"));
45+
System.clearProperty("grape.root");
46+
System.clearProperty("groovy.grape.report.downloads");
4447
}
4548

4649
@Test
4750
public void grab() throws Exception {
51+
4852
System.setProperty("grape.root", "target");
4953
System.setProperty("groovy.grape.report.downloads", "true");
5054

51-
try {
52-
String output = this.cli.grab("grab.groovy");
53-
assertTrue(output.contains("Downloading: file:"));
54-
assertTrue(new File("target/repository/org/springframework/spring-jdbc")
55-
.isDirectory());
56-
}
57-
finally {
58-
System.clearProperty("grape.root");
59-
System.clearProperty("groovy.grape.report.downloads");
60-
}
55+
// Use --autoconfigure=false to limit the amount of downloaded dependencies
56+
String output = this.cli.grab("grab.groovy", "--autoconfigure=false");
57+
assertTrue(output.contains("Downloading: file:"));
58+
assertTrue(new File("target/repository/net/sf/jopt-simple/jopt-simple")
59+
.isDirectory());
60+
6161
}
6262
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@Grab('spring-jdbc')
1+
@Grab('jopt-simple')
22
class GrabTest {
33

44
}

0 commit comments

Comments
 (0)