Skip to content

Commit 082a670

Browse files
cli: background command output extract by regexp
1 parent 215b03a commit 082a670

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

webtau-cli-groovy/src/test/groovy/org/testingisdocumenting/webtau/cli/CliBackgroundCommandOutputTest.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.testingisdocumenting.webtau.console.ConsoleOutput
2222
import org.testingisdocumenting.webtau.console.ConsoleOutputs
2323
import org.testingisdocumenting.webtau.console.ansi.IgnoreAnsiString
2424

25+
import static org.testingisdocumenting.webtau.Matchers.contain
2526
import static org.testingisdocumenting.webtau.cli.Cli.cli
2627
import static org.testingisdocumenting.webtau.cli.CliTestUtils.supportedPlatformOnly
2728

@@ -67,6 +68,19 @@ class CliBackgroundCommandOutputTest implements ConsoleOutput {
6768
}
6869
}
6970

71+
@Test
72+
void "extract text by regexp should be part of steps"() {
73+
supportedPlatformOnly {
74+
def backgroundCmd = cli.runInBackground("scripts/hello")
75+
backgroundCmd.output.waitTo contain("more text")
76+
77+
def text = backgroundCmd.output.extractByRegexp("more (.*xt)")
78+
text.should == "text"
79+
80+
output.should contain("extracted text by regexp more (.*xt) from scripts/hello process output : text")
81+
}
82+
}
83+
7084
private static String normalizeOutput(String output) {
7185
return output.replaceAll(/\(\d+ms\)/, "(time)")
7286
.replace('. background cli command : scripts/long-sleep finished with exit code 143 (time)\n', '')

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/CliBackgroundProcess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public CliBackgroundProcess(Process process,
5151
this.errorGobbler = errorGobbler;
5252
this.consumeErrorThread = consumeErrorThread;
5353
this.consumeOutThread = consumeOutThread;
54-
this.output = new CliOutput("process output", outputGobbler);
55-
this.error = new CliOutput("process error output", errorGobbler);
54+
this.output = new CliOutput(command, "process output", outputGobbler);
55+
this.error = new CliOutput(command, "process error output", errorGobbler);
5656
this.isActive = new AtomicBoolean(true);
5757
}
5858

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/CliOutput.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@
1919

2020
import org.testingisdocumenting.webtau.cli.expectation.CliResultExpectations;
2121
import org.testingisdocumenting.webtau.expectation.ActualPath;
22+
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
23+
import org.testingisdocumenting.webtau.reporter.WebTauStep;
24+
import org.testingisdocumenting.webtau.utils.RegexpUtils;
2225

2326
import java.io.IOException;
2427
import java.util.*;
28+
import java.util.regex.Pattern;
2529
import java.util.stream.Collectors;
2630

31+
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.*;
32+
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.stringValue;
33+
import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.tokenizedMessage;
34+
2735
public class CliOutput implements CliResultExpectations {
2836
private final String id;
2937
private final StreamGobbler streamGobbler;
3038

3139
private final Set<Integer> matchedLinesIdx;
40+
private final String command;
3241

3342
private int lastClearNextLineIdxMarker;
3443

35-
public CliOutput(String id, StreamGobbler streamGobbler) {
44+
public CliOutput(String command, String id, StreamGobbler streamGobbler) {
45+
this.command = command;
3646
this.id = id;
3747
this.streamGobbler = streamGobbler;
3848

@@ -44,6 +54,21 @@ public ActualPath actualPath() {
4454
return new ActualPath(id);
4555
}
4656

57+
public String extractByRegexp(String regexp) {
58+
return extractByRegexp(Pattern.compile(regexp));
59+
}
60+
61+
public String extractByRegexp(Pattern regexp) {
62+
WebTauStep step = WebTauStep.createStep(null,
63+
tokenizedMessage(action("extracting text"), classifier("by regexp"), stringValue(regexp),
64+
FROM, urlValue(command), classifier(id)),
65+
(r) -> tokenizedMessage(action("extracted text"), classifier("by regexp"), stringValue(regexp),
66+
FROM, urlValue(command), classifier(id), COLON, stringValue(r)),
67+
() -> RegexpUtils.extractByRegexp(get(), regexp));
68+
69+
return step.execute(StepReportOptions.SKIP_START);
70+
}
71+
4772
public String get() {
4873
return streamGobbler.joinLinesStartingAt(lastClearNextLineIdxMarker);
4974
}

0 commit comments

Comments
 (0)