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.
This commit is contained in:
parent
fce48c00c7
commit
89332e230e
|
@ -25,6 +25,7 @@ import static java.util.Arrays.asList;
|
|||
* Groovy scripts
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class CompilerOptionHandler extends OptionHandler {
|
||||
|
||||
|
@ -32,6 +33,8 @@ public class CompilerOptionHandler extends OptionHandler {
|
|||
|
||||
private OptionSpec<Void> noGuessDependenciesOption;
|
||||
|
||||
private OptionSpec<Boolean> autoconfigureOption;
|
||||
|
||||
private OptionSpec<String> classpathOption;
|
||||
|
||||
@Override
|
||||
|
@ -40,6 +43,9 @@ public class CompilerOptionHandler extends OptionHandler {
|
|||
"Do not attempt to guess imports");
|
||||
this.noGuessDependenciesOption = option("no-guess-dependencies",
|
||||
"Do not attempt to guess dependencies");
|
||||
this.autoconfigureOption = option("autoconfigure",
|
||||
"Add autoconfigure compiler transformations").withOptionalArg()
|
||||
.ofType(Boolean.class).defaultsTo(true);
|
||||
this.classpathOption = option(asList("classpath", "cp"),
|
||||
"Additional classpath entries").withRequiredArg();
|
||||
doOptions();
|
||||
|
@ -60,4 +66,8 @@ public class CompilerOptionHandler extends OptionHandler {
|
|||
return this.classpathOption;
|
||||
}
|
||||
|
||||
public OptionSpec<Boolean> getAutoconfigureOption() {
|
||||
return this.autoconfigureOption;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -49,8 +49,6 @@ public class GrabCommand extends OptionParsingCommand {
|
|||
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File(
|
||||
getM2HomeDirectory(), "repository").toURI(), true));
|
||||
|
||||
GroovyCompilerConfiguration configuration = new GroovyCompilerConfigurationAdapter(
|
||||
options, this, repositoryConfiguration);
|
||||
|
@ -59,16 +57,33 @@ public class GrabCommand extends OptionParsingCommand {
|
|||
System.setProperty("grape.root", ".");
|
||||
}
|
||||
|
||||
if (!new File(System.getProperty("grape.root")).equals(getM2HomeDirectory())) {
|
||||
GrabCommand.addDefaultCacheAsRespository(repositoryConfiguration);
|
||||
}
|
||||
GroovyCompiler groovyCompiler = new GroovyCompiler(configuration);
|
||||
groovyCompiler.compile(fileOptions.getFilesArray());
|
||||
}
|
||||
|
||||
private File getM2HomeDirectory() {
|
||||
String mavenRoot = System.getProperty("maven.home");
|
||||
if (StringUtils.hasLength(mavenRoot)) {
|
||||
return new File(mavenRoot);
|
||||
}
|
||||
return new File(System.getProperty("user.home"), ".m2");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the default local M2 cache directory as a remote repository. Only do this if
|
||||
* the local cache location has been changed from the default.
|
||||
*
|
||||
* @param repositoryConfiguration
|
||||
*/
|
||||
public static void addDefaultCacheAsRespository(
|
||||
List<RepositoryConfiguration> repositoryConfiguration) {
|
||||
repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File(
|
||||
getM2HomeDirectory(), "repository").toURI(), true));
|
||||
}
|
||||
|
||||
private static File getM2HomeDirectory() {
|
||||
String mavenRoot = System.getProperty("maven.home");
|
||||
if (StringUtils.hasLength(mavenRoot)) {
|
||||
return new File(mavenRoot);
|
||||
}
|
||||
return new File(System.getProperty("user.home"), ".m2");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.boot.cli.command;
|
||||
|
||||
import java.awt.Desktop;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
|
@ -89,8 +88,6 @@ public class RunCommand extends OptionParsingCommand {
|
|||
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File(
|
||||
"repository").toURI(), true));
|
||||
|
||||
SpringApplicationRunnerConfiguration configuration = new SpringApplicationRunnerConfigurationAdapter(
|
||||
options, this, repositoryConfiguration);
|
||||
|
|
|
@ -264,6 +264,11 @@ public class ScriptCommand implements Command {
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoconfigure() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGuessDependencies() {
|
||||
return true;
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.io.IOException;
|
|||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ServiceLoader;
|
||||
|
@ -77,7 +78,7 @@ public class GroovyCompiler {
|
|||
|
||||
private final ExtendedGroovyClassLoader loader;
|
||||
|
||||
private final ServiceLoader<CompilerAutoConfiguration> compilerAutoConfigurations;
|
||||
private final Iterable<CompilerAutoConfiguration> compilerAutoConfigurations;
|
||||
|
||||
private final List<ASTTransformation> transformations;
|
||||
|
||||
|
@ -96,8 +97,14 @@ public class GroovyCompiler {
|
|||
|
||||
this.loader.getConfiguration().addCompilationCustomizers(
|
||||
new CompilerAutoConfigureCustomizer());
|
||||
this.compilerAutoConfigurations = ServiceLoader.load(
|
||||
CompilerAutoConfiguration.class, GroovyCompiler.class.getClassLoader());
|
||||
if (configuration.isAutoconfigure()) {
|
||||
this.compilerAutoConfigurations = ServiceLoader.load(
|
||||
CompilerAutoConfiguration.class,
|
||||
GroovyCompiler.class.getClassLoader());
|
||||
}
|
||||
else {
|
||||
this.compilerAutoConfigurations = Collections.emptySet();
|
||||
}
|
||||
|
||||
this.transformations = new ArrayList<ASTTransformation>();
|
||||
this.transformations.add(new GrabResolversAutoConfigurationTransformation());
|
||||
|
|
|
@ -48,6 +48,11 @@ public interface GroovyCompilerConfiguration {
|
|||
*/
|
||||
boolean isGuessDependencies();
|
||||
|
||||
/**
|
||||
* Returns true if autoconfiguration transformations should be applied.
|
||||
*/
|
||||
boolean isAutoconfigure();
|
||||
|
||||
/**
|
||||
* @return a path for local resources
|
||||
*/
|
||||
|
|
|
@ -71,6 +71,11 @@ public class GroovyCompilerConfigurationAdapter implements GroovyCompilerConfigu
|
|||
return !this.options.has(this.optionHandler.getNoGuessDependenciesOption());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoconfigure() {
|
||||
return this.optionHandler.getAutoconfigureOption().value(this.options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getClasspath() {
|
||||
OptionSpec<String> classpathOption = this.optionHandler.getClasspathOption();
|
||||
|
|
|
@ -24,6 +24,7 @@ import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
|
|||
|
||||
/**
|
||||
* @author Andy Wilkinson
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public final class RepositoryConfigurationFactory {
|
||||
|
||||
|
@ -51,4 +52,5 @@ public final class RepositoryConfigurationFactory {
|
|||
|
||||
return repositoryConfiguration;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -98,7 +98,12 @@ public class CliTester implements TestRule {
|
|||
final String[] sources = new String[args.length];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
String arg = args[i];
|
||||
sources[i] = this.prefix + arg;
|
||||
if (arg.startsWith("--")) {
|
||||
sources[i] = arg;
|
||||
}
|
||||
else {
|
||||
sources[i] = this.prefix + arg;
|
||||
}
|
||||
}
|
||||
return sources;
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ import static org.junit.Assert.assertTrue;
|
|||
* Integration tests for {@link GrabCommand}
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class GrabCommandIntegrationTests {
|
||||
|
||||
|
@ -41,22 +42,21 @@ public class GrabCommandIntegrationTests {
|
|||
@After
|
||||
public void deleteLocalRepository() {
|
||||
FileSystemUtils.deleteRecursively(new File("target/repository"));
|
||||
System.clearProperty("grape.root");
|
||||
System.clearProperty("groovy.grape.report.downloads");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void grab() throws Exception {
|
||||
|
||||
System.setProperty("grape.root", "target");
|
||||
System.setProperty("groovy.grape.report.downloads", "true");
|
||||
|
||||
try {
|
||||
String output = this.cli.grab("grab.groovy");
|
||||
assertTrue(output.contains("Downloading: file:"));
|
||||
assertTrue(new File("target/repository/org/springframework/spring-jdbc")
|
||||
.isDirectory());
|
||||
}
|
||||
finally {
|
||||
System.clearProperty("grape.root");
|
||||
System.clearProperty("groovy.grape.report.downloads");
|
||||
}
|
||||
// Use --autoconfigure=false to limit the amount of downloaded dependencies
|
||||
String output = this.cli.grab("grab.groovy", "--autoconfigure=false");
|
||||
assertTrue(output.contains("Downloading: file:"));
|
||||
assertTrue(new File("target/repository/net/sf/jopt-simple/jopt-simple")
|
||||
.isDirectory());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
@Grab('spring-jdbc')
|
||||
@Grab('jopt-simple')
|
||||
class GrabTest {
|
||||
|
||||
}
|
Loading…
Reference in New Issue