Skip to content

Commit

Permalink
Test project run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tsegismont committed Mar 8, 2019
1 parent bea8e46 commit 366ff8a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/templates/README.adoc.ftl
Expand Up @@ -33,7 +33,7 @@ To package your application:
To run your application:
<#if buildTool == "maven">
```
./mvnw clean exec:java
./mvnw clean compile exec:java
```
</#if>
<#if buildTool == 'gradle'>
Expand Down
Expand Up @@ -14,7 +14,7 @@ public class MainVerticle extends AbstractVerticle {
}).listen(8888, http -> {
if (http.succeeded()) {
startFuture.complete();
System.out.println("HTTP server started on http://localhost:8888");
System.out.println("HTTP server started on port 8888");
} else {
startFuture.fail(http.cause());
}
Expand Down
41 changes: 40 additions & 1 deletion src/test/java/io/vertx/starter/service/GeneratorTest.java
Expand Up @@ -28,6 +28,7 @@
import io.vertx.core.impl.NoStackTraceThrowable;
import io.vertx.core.impl.Utils;
import io.vertx.core.json.JsonObject;
import io.vertx.core.parsetools.RecordParser;
import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
Expand All @@ -40,6 +41,7 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -49,6 +51,7 @@
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -70,6 +73,7 @@ class GeneratorTest {

private MessageProducer<JsonObject> producer;
private Path workdir;
private List<Runnable> cleanupTasks = new ArrayList<>();

@BeforeEach
void beforeEach(Vertx vertx, VertxTestContext testContext) throws IOException {
Expand All @@ -78,6 +82,11 @@ void beforeEach(Vertx vertx, VertxTestContext testContext) throws IOException {
vertx.deployVerticle(new GeneratorVerticle(), testContext.succeeding(id -> testContext.completeNow()));
}

@AfterEach
void afterEach() {
cleanupTasks.forEach(Runnable::run);
}

private VertxProject defaultProject() {
return new VertxProject()
.setId("demo")
Expand Down Expand Up @@ -146,7 +155,7 @@ private void testProject(Vertx vertx, VertxTestContext testContext, VertxProject
testContext.failNow(new NoStackTraceThrowable(unsupported(buildTool)));
}

testContext.completeNow();
runDevMode(vertx, buildTool, testContext.succeeding(devModeRan -> testContext.completeNow()));
});
}));
}
Expand Down Expand Up @@ -214,6 +223,7 @@ private void buildProject(Vertx vertx, BuildTool buildTool, Handler<AsyncResult<
return;
}
Process process = Process.create(vertx, command, args, processOptions);
cleanupTasks.add(() -> process.kill(true));
Buffer buffer = Buffer.buffer();
process.stdout().handler(buffer::appendBuffer);
process.stderr().handler(buffer::appendBuffer);
Expand All @@ -227,6 +237,35 @@ private void buildProject(Vertx vertx, BuildTool buildTool, Handler<AsyncResult<
process.start();
}

private void runDevMode(Vertx vertx, BuildTool buildTool, Handler<AsyncResult<Void>> handler) {
ProcessOptions processOptions = new ProcessOptions().setCwd(workdir.toString());
String command;
List<String> args;
if (buildTool == MAVEN) {
command = "./mvnw";
args = Arrays.asList("clean", "compile", "exec:java");
} else if (buildTool == GRADLE) {
command = "./gradlew";
args = Arrays.asList("clean", "run");
} else {
handler.handle(Future.failedFuture(unsupported(buildTool)));
return;
}
Process process = Process.create(vertx, command, args, processOptions);
cleanupTasks.add(() -> process.kill(true));
Future<Void> future = Future.<Void>future().setHandler(handler);
RecordParser parser = RecordParser.newDelimited("\n")
.exceptionHandler(future::fail)
.handler(buffer -> {
String line = buffer.toString().trim();
if (line.contains("HTTP server started on port 8888")) {
future.complete();
}
});
process.stdout().exceptionHandler(future::fail).handler(parser);
process.start();
}

private void unpack(Vertx vertx, VertxTestContext testContext, Path workdir, Buffer buffer, Handler<AsyncResult<Void>> handler) {
vertx.executeBlocking(fut -> {
try (ByteBufInputStream bbis = new ByteBufInputStream(buffer.getByteBuf());
Expand Down

0 comments on commit 366ff8a

Please sign in to comment.