Minimize and centralize assumptions about build output
Closes gh-15471
This commit is contained in:
@@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Assume;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
@@ -42,6 +43,7 @@ import org.springframework.boot.cli.command.archive.JarCommand;
|
||||
import org.springframework.boot.cli.command.grab.GrabCommand;
|
||||
import org.springframework.boot.cli.command.run.RunCommand;
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
import org.springframework.boot.testsupport.BuildOutput;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -54,6 +56,10 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class CliTester implements TestRule {
|
||||
|
||||
private final TemporaryFolder temp = new TemporaryFolder();
|
||||
|
||||
private final BuildOutput buildOutput = new BuildOutput(getClass());
|
||||
|
||||
private final OutputCapture outputCapture = new OutputCapture();
|
||||
|
||||
private long timeout = TimeUnit.MINUTES.toMillis(6);
|
||||
@@ -62,6 +68,8 @@ public class CliTester implements TestRule {
|
||||
|
||||
private final String prefix;
|
||||
|
||||
private File serverPortFile;
|
||||
|
||||
public CliTester(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
@@ -75,14 +83,15 @@ public class CliTester implements TestRule {
|
||||
boolean classpathUpdated = false;
|
||||
for (String arg : args) {
|
||||
if (arg.startsWith("--classpath=")) {
|
||||
arg = arg + ":" + new File("target/test-classes").getAbsolutePath();
|
||||
arg = arg + ":"
|
||||
+ this.buildOutput.getTestClassesLocation().getAbsolutePath();
|
||||
classpathUpdated = true;
|
||||
}
|
||||
updatedArgs.add(arg);
|
||||
}
|
||||
if (!classpathUpdated) {
|
||||
updatedArgs.add(
|
||||
"--classpath=.:" + new File("target/test-classes").getAbsolutePath());
|
||||
updatedArgs.add("--classpath=.:"
|
||||
+ this.buildOutput.getTestClassesLocation().getAbsolutePath());
|
||||
}
|
||||
Future<RunCommand> future = submitCommand(new RunCommand(),
|
||||
StringUtils.toStringArray(updatedArgs));
|
||||
@@ -111,8 +120,8 @@ public class CliTester implements TestRule {
|
||||
System.setProperty("server.port", "0");
|
||||
System.setProperty("spring.application.class.name",
|
||||
"org.springframework.boot.cli.CliTesterSpringApplication");
|
||||
System.setProperty("portfile",
|
||||
new File("target/server.port").getAbsolutePath());
|
||||
this.serverPortFile = new File(this.temp.newFolder(), "server.port");
|
||||
System.setProperty("portfile", this.serverPortFile.getAbsolutePath());
|
||||
try {
|
||||
command.run(sources);
|
||||
return command;
|
||||
@@ -168,8 +177,9 @@ public class CliTester implements TestRule {
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
final Statement statement = CliTester.this.outputCapture
|
||||
.apply(new RunLauncherStatement(base), description);
|
||||
final Statement statement = this.temp.apply(
|
||||
this.outputCapture.apply(new RunLauncherStatement(base), description),
|
||||
description);
|
||||
return new Statement() {
|
||||
|
||||
@Override
|
||||
@@ -180,6 +190,7 @@ public class CliTester implements TestRule {
|
||||
.contains("integration"));
|
||||
statement.evaluate();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@@ -190,7 +201,7 @@ public class CliTester implements TestRule {
|
||||
public String getHttpOutput(String uri) {
|
||||
try {
|
||||
int port = Integer.parseInt(
|
||||
FileCopyUtils.copyToString(new FileReader("target/server.port")));
|
||||
FileCopyUtils.copyToString(new FileReader(this.serverPortFile)));
|
||||
InputStream stream = URI.create("http://localhost:" + port + uri).toURL()
|
||||
.openStream();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import org.springframework.boot.cli.command.grab.GrabCommand;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
@@ -37,13 +38,15 @@ import static org.junit.Assert.fail;
|
||||
*/
|
||||
public class GrabCommandIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temp = new TemporaryFolder();
|
||||
|
||||
@Rule
|
||||
public CliTester cli = new CliTester("src/test/resources/grab-samples/");
|
||||
|
||||
@Before
|
||||
@After
|
||||
public void deleteLocalRepository() {
|
||||
FileSystemUtils.deleteRecursively(new File("target/repository"));
|
||||
System.clearProperty("grape.root");
|
||||
System.clearProperty("groovy.grape.report.downloads");
|
||||
}
|
||||
@@ -51,13 +54,13 @@ public class GrabCommandIntegrationTests {
|
||||
@Test
|
||||
public void grab() throws Exception {
|
||||
|
||||
System.setProperty("grape.root", "target");
|
||||
System.setProperty("grape.root", this.temp.getRoot().getAbsolutePath());
|
||||
System.setProperty("groovy.grape.report.downloads", "true");
|
||||
|
||||
// Use --autoconfigure=false to limit the amount of downloaded dependencies
|
||||
String output = this.cli.grab("grab.groovy", "--autoconfigure=false");
|
||||
assertThat(new File("target/repository/joda-time/joda-time").isDirectory())
|
||||
.isTrue();
|
||||
assertThat(new File(this.temp.getRoot(), "repository/joda-time/joda-time"))
|
||||
.isDirectory();
|
||||
// Should be resolved from local repository cache
|
||||
assertThat(output.contains("Downloading: file:")).isTrue();
|
||||
}
|
||||
@@ -76,13 +79,12 @@ public class GrabCommandIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void customMetadata() throws Exception {
|
||||
System.setProperty("grape.root", "target");
|
||||
System.setProperty("grape.root", this.temp.getRoot().getAbsolutePath());
|
||||
File repository = new File(this.temp.getRoot().getAbsolutePath(), "repository");
|
||||
FileSystemUtils.copyRecursively(
|
||||
new File("src/test/resources/grab-samples/repository"),
|
||||
new File("target/repository"));
|
||||
new File("src/test/resources/grab-samples/repository"), repository);
|
||||
this.cli.grab("customDependencyManagement.groovy", "--autoconfigure=false");
|
||||
assertThat(new File("target/repository/javax/ejb/ejb-api/3.0").isDirectory())
|
||||
.isTrue();
|
||||
assertThat(new File(repository, "javax/ejb/ejb-api/3.0")).isDirectory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,8 +28,6 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -50,9 +48,7 @@ public class InstallerTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
System.setProperty("spring.home", "target");
|
||||
FileSystemUtils.deleteRecursively(new File("target/lib"));
|
||||
|
||||
System.setProperty("spring.home", this.tempFolder.getRoot().getAbsolutePath());
|
||||
this.installer = new Installer(this.resolver);
|
||||
}
|
||||
|
||||
@@ -119,7 +115,7 @@ public class InstallerTests {
|
||||
|
||||
private Set<String> getNamesOfFilesInLibExt() {
|
||||
Set<String> names = new HashSet<>();
|
||||
for (File file : new File("target/lib/ext").listFiles()) {
|
||||
for (File file : new File(this.tempFolder.getRoot(), "lib/ext").listFiles()) {
|
||||
names.add(file.getName());
|
||||
}
|
||||
return names;
|
||||
|
||||
Reference in New Issue
Block a user