Skip to content
Permalink
Browse files

initial cli testing frame (#341)

  • Loading branch information...
MykolaGolubyev committed May 28, 2019
1 parent 1d66e2f commit 5d9b660bc19029722f991b3b324b46ff4d9201bd
Showing with 912 additions and 22 deletions.
  1. +2 −0 pom.xml
  2. +7 −0 webtau-cache/pom.xml
  3. +28 −0 webtau-cli-groovy/pom.xml
  4. +22 −0 webtau-cli-groovy/src/main/groovy/com/twosigma/webtau/cli/CliExtension.groovy
  5. +52 −0 webtau-cli/pom.xml
  6. +2 −0 webtau-cli/scripts/hello
  7. +93 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/Cli.java
  8. +61 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/CliOutputListener.java
  9. +35 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/ProcessEnv.java
  10. +61 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/ProcessRunResult.java
  11. +61 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/ProcessUtils.java
  12. +67 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/StreamGobbler.java
  13. +37 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliExitCode.java
  14. +47 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliExitCodeCompareToHandler.java
  15. +47 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliOutput.java
  16. +33 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliOutputCompareToHandler.java
  17. +38 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliOutputContainHandler.java
  18. +51 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliResultExpectations.java
  19. +23 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliSetupAndValidationFullHandler.java
  20. +21 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliValidationExitCodeOutputHandler.java
  21. +21 −0 webtau-cli/src/main/java/com/twosigma/webtau/cli/expectation/CliValidationOutputOnlylHandler.java
  22. +1 −0 ...u-cli/src/main/resources/META-INF/services/com.twosigma.webtau.expectation.contain.ContainHandler
  23. +2 −0 ...li/src/main/resources/META-INF/services/com.twosigma.webtau.expectation.equality.CompareToHandler
  24. +56 −0 webtau-cli/src/test/java/com/twosigma/webtau/cli/CliJavaTest.java
  25. +29 −0 webtau-cli/src/test/java/com/twosigma/webtau/cli/CliTestUtils.java
  26. +7 −6 webtau-core/pom.xml
  27. +0 −6 webtau-http-groovy/pom.xml
  28. +0 −1 webtau-junit5-examples/pom.xml
  29. +8 −8 webtau-junit5-examples/src/test/groovy/com/example/tests/junit5/DynamicTestsGroovyTest.groovy
  30. +0 −1 webtau-junit5-groovy/pom.xml
@@ -106,6 +106,8 @@
<module>webtau-data</module>
<module>webtau-cache</module>
<module>webtau-test-server</module>
<module>webtau-cli</module>
<module>webtau-cli-groovy</module>
<module>webtau-groovy-ast</module>
<module>webtau-groovy</module>
<module>webtau-reactjs</module>
@@ -50,6 +50,13 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<scope>test</scope>
<type>pom</type>
</dependency>
</dependencies>

<build>
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.twosigma.webtau</groupId>
<artifactId>webtau-parent</artifactId>
<version>1.9-SNAPSHOT</version>
</parent>

<artifactId>webtau-cli-groovy</artifactId>

<dependencies>
<dependency>
<groupId>com.twosigma.webtau</groupId>
<artifactId>webtau-cli</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<type>pom</type>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,22 @@
/*
*
* * Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package com.twosigma.webtau.cli

class CliExtension {
}
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.twosigma.webtau</groupId>
<artifactId>webtau-parent</artifactId>
<version>1.9-SNAPSHOT</version>
</parent>

<artifactId>webtau-cli</artifactId>

<dependencies>
<dependency>
<groupId>com.twosigma.webtau</groupId>
<artifactId>webtau-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<scope>test</scope>
<type>pom</type>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>addTestSources</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,2 @@
#!/bin/bash
echo "hello world" $NAME
@@ -0,0 +1,93 @@
/*
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.twosigma.webtau.cli;

import com.twosigma.webtau.cli.expectation.CliExitCode;
import com.twosigma.webtau.cli.expectation.CliOutput;
import com.twosigma.webtau.cli.expectation.CliValidationExitCodeOutputHandler;
import com.twosigma.webtau.cli.expectation.CliValidationOutputOnlylHandler;
import com.twosigma.webtau.reporter.TestStep;
import com.twosigma.webtau.utils.CollectionUtils;

import java.util.Map;
import java.util.function.Consumer;

import static com.twosigma.webtau.reporter.IntegrationTestsMessageBuilder.action;
import static com.twosigma.webtau.reporter.IntegrationTestsMessageBuilder.stringValue;
import static com.twosigma.webtau.reporter.TokenizedMessage.tokenizedMessage;

public class Cli {
public static final Cli cli = new Cli();

private Cli() {
}

public ProcessEnv env(Map<String, String> env) {
return new ProcessEnv(env);
}

public ProcessEnv env(String... keyValue) {
return new ProcessEnv(CollectionUtils.aMapOf(keyValue));
}

public void run(String command, CliValidationOutputOnlylHandler handler) {
run(command, ProcessEnv.EMPTY, handler);
}

public void run(String command, ProcessEnv env, CliValidationOutputOnlylHandler handler) {
cliStep(command, env, (runResult) -> handler.handle(cliOutput(runResult), cliError(runResult)));
}

public void run(String command, CliValidationExitCodeOutputHandler handler) {
run(command, ProcessEnv.EMPTY, handler);
}

public void run(String command, ProcessEnv env, CliValidationExitCodeOutputHandler handler) {
cliStep(command, env,
(runResult) -> handler.handle(exitCode(runResult), cliOutput(runResult), cliError(runResult)));
}

private void cliStep(String command, ProcessEnv env, Consumer<ProcessRunResult> validationCode) {
TestStep.createAndExecuteStep(null,
tokenizedMessage(action("running cli command "), stringValue(command)),
() -> tokenizedMessage(action("ran cli command"), stringValue(command)),
() -> {
ProcessRunResult runResult = ProcessUtils.run(command, env.getEnv());
if (runResult.getErrorReadingException() != null) {
throw new RuntimeException(runResult.getErrorReadingException());
}

if (runResult.getOutputReadingException() != null) {
throw new RuntimeException(runResult.getOutputReadingException());
}

validationCode.accept(runResult);
});
}

private CliExitCode exitCode(ProcessRunResult runResult) {
return new CliExitCode(runResult.getExitCode());
}

private static CliOutput cliOutput(ProcessRunResult runResult) {
return new CliOutput("process output", runResult.getOutput());
}

private static CliOutput cliError(ProcessRunResult runResult) {
return new CliOutput("process error output", runResult.getError());
}
}
@@ -0,0 +1,61 @@
/*
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.twosigma.webtau.cli;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class CliOutputListener {
private List<PredicateWithHandler> lineHandlers;

public CliOutputListener() {
this.lineHandlers = new ArrayList<>();
}

public void on(String partialLine, Runnable handler) {
lineHandlers.add(new PredicateWithHandler(new PartialLineMatch(partialLine), handler));
}

public Stream<PredicateWithHandler> lineHandlersStream() {
return lineHandlers.stream();
}

private static class PredicateWithHandler {
Predicate<String> predicate;
Runnable handler;

public PredicateWithHandler(Predicate<String> predicate, Runnable handler) {
this.predicate = predicate;
this.handler = handler;
}
}

private static class PartialLineMatch implements Predicate<String> {
private final String partialExpectation;

public PartialLineMatch(String partialExpectation) {
this.partialExpectation = partialExpectation;
}

@Override
public boolean test(String line) {
return line.contains(partialExpectation);
}
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.twosigma.webtau.cli;

import java.util.Collections;
import java.util.Map;

public class ProcessEnv {
public static final ProcessEnv EMPTY = new ProcessEnv(Collections.emptyMap());

private Map<String, String> env;

public ProcessEnv(Map<String, String> env) {
this.env = env;
}

public Map<String, String> getEnv() {
return env;
}
}
@@ -0,0 +1,61 @@
/*
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.twosigma.webtau.cli;

import java.io.IOException;
import java.util.List;

public class ProcessRunResult {
private int exitCode;
private List<String> output;
private List<String> error;

private IOException outputReadingException;
private IOException errorReadingException;

public ProcessRunResult(int exitCode,
List<String> output,
List<String> error,
IOException outputReadingException,
IOException errorReadingException) {
this.exitCode = exitCode;
this.output = output;
this.error = error;
this.outputReadingException = outputReadingException;
this.errorReadingException = errorReadingException;
}

public IOException getOutputReadingException() {
return outputReadingException;
}

public IOException getErrorReadingException() {
return errorReadingException;
}

public int getExitCode() {
return exitCode;
}

public List<String> getOutput() {
return output;
}

public List<String> getError() {
return error;
}
}

0 comments on commit 5d9b660

Please sign in to comment.
You can’t perform that action at this time.