Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better log output to console #21

Merged
merged 11 commits into from
May 28, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,29 @@ private void findText(Run<?, ?> run, FilePath workspace, TaskListener listener)
boolean foundText = false;

if (alsoCheckConsoleOutput) {
logger.println("Checking console output");
// Do not mention the pattern we are looking for to avoid false positives
logger.println("Looking for a specific pattern in the console output");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for doing this. There's only one minor comment I have. The wording of the message "Looking for a specific pattern in the console output" seems potentially confusing to the end user. To me, this message raises the question: "What is that specific pattern?" Of course, we can't tell the user what the specific pattern is, because that would lead to a false positive, as your comment states. How about if we changed the logic such that instead of printing "Looking for a specific pattern in the console output", we printed "Searched for pattern X in the console output" after performing the search? That way, we'd avoid the false positive. We'd probably want to change the behavior for files to do the same thing, for consistency. Then we'd be able to print the pattern in the message in both cases. Let me know what you think of this idea. If you don't think it's worth the additional work, then I'm fine with this change as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time on thinking about this!
I also went through this a couple of times, I really wanted to show the pattern but felt it required some user feedback BEFORE starting looking.
I was thinking on saving the console log, print message and then do the search on the saved content but the size might be too big in memory in certain situations.
I will try to apply your suggestion, adding a message BEFORE the search so the user knows what is happening.

foundText |=
checkFile(
run.getLogFile(),
compilePattern(logger, regexp),
logger,
run.getCharset(),
true);
} else {
// printing this when checking console output will cause the plugin
// to find this line, which would be pointless.
// doing this only when fileSet!=null to avoid
logger.println("Checking " + regexp);
}

final RemoteOutputStream ros = new RemoteOutputStream(logger);

if (fileSet != null) {
logger.println(
"Looking for pattern "
+ "'"
+ regexp
+ "'"
+ " in the files at "
+ "'"
+ fileSet
+ "'");
foundText |= workspace.act(new FileChecker(ros, fileSet, regexp));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public void failureIfFoundInFileOnAgent() throws Exception {
agent.getNodeName(), agent.getNodeName())));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking foobar", build);
rule.assertLogContains(
"Looking for pattern " + "'" + UNIQUE_TEXT + "'" + " in the files at", build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.FAILURE, build);
}
Expand All @@ -79,7 +80,7 @@ public void failureIfFoundInConsoleOnAgent() throws Exception {
agent.getNodeName(), agent.getNodeName())));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.FAILURE, build);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void successIfFoundInConsole() throws Exception {
project.getPublishersList().add(textFinder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.SUCCESS, build);
}
Expand All @@ -66,7 +66,7 @@ public void failureIfFoundInConsole() throws Exception {
project.getPublishersList().add(textFinder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.FAILURE, build);
}
Expand All @@ -85,7 +85,7 @@ public void unstableIfFoundInConsole() throws Exception {
project.getPublishersList().add(textFinder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.UNSTABLE, build);
}
Expand All @@ -98,7 +98,7 @@ public void notFoundInConsole() throws Exception {
project.getPublishersList().add(textFinder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
rule.assertBuildStatus(Result.SUCCESS, build);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TextFinderPublisherPipelineTest {

private static final String UNIQUE_TEXT = "foobar";
private static final String ECHO_UNIQUE_TEXT = "echo " + UNIQUE_TEXT;
private static final String fileSet = "out.txt";

@Rule public JenkinsRule rule = new JenkinsRule();

Expand Down Expand Up @@ -54,12 +55,22 @@ public void successIfFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobar'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt', succeedIfFound: true}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: '"
+ UNIQUE_TEXT
+ "'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "', succeedIfFound: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking foobar", build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
rule.assertLogContains(
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.SUCCESS, build);
}

Expand All @@ -68,12 +79,22 @@ public void failureIfFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobar'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt'}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: '"
+ UNIQUE_TEXT
+ "'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "'}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking foobar", build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
rule.assertLogContains(
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.FAILURE, build);
}

Expand All @@ -82,12 +103,22 @@ public void unstableIfFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobar'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt', unstableIfFound: true}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: '"
+ UNIQUE_TEXT
+ "'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "', unstableIfFound: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking foobar", build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
rule.assertLogContains(
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.UNSTABLE, build);
}

Expand All @@ -96,12 +127,22 @@ public void notBuiltIfFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobar'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt', notBuiltIfFound: true}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: '"
+ UNIQUE_TEXT
+ "'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "', notBuiltIfFound: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking foobar", build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
rule.assertLogContains(
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.NOT_BUILT, build);
}

Expand All @@ -110,11 +151,19 @@ public void notFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobaz'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt'}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: 'foobaz'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "'}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking foobar", build);
rule.assertLogContains(
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
rule.assertBuildStatus(Result.SUCCESS, build);
}

Expand All @@ -123,11 +172,17 @@ public void successIfFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n"
+ "node {findText regexp: 'foobar', succeedIfFound: true, alsoCheckConsoleOutput: true}\n"));
"node {isUnix() ? sh('"
+ ECHO_UNIQUE_TEXT
+ "') : bat(\"prompt \\$G\\r\\n"
+ ECHO_UNIQUE_TEXT
+ "\")}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', succeedIfFound: true, alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.SUCCESS, build);
}
Expand All @@ -137,11 +192,17 @@ public void failureIfFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n"
+ "node {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n"));
"node {isUnix() ? sh('"
+ ECHO_UNIQUE_TEXT
+ "') : bat(\"prompt \\$G\\r\\n"
+ ECHO_UNIQUE_TEXT
+ "\")}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.FAILURE, build);
}
Expand All @@ -151,11 +212,17 @@ public void unstableIfFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n"
+ "node {findText regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true}\n"));
"node {isUnix() ? sh('"
+ ECHO_UNIQUE_TEXT
+ "') : bat(\"prompt \\$G\\r\\n"
+ ECHO_UNIQUE_TEXT
+ "\")}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', unstableIfFound: true, alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.UNSTABLE, build);
}
Expand All @@ -165,11 +232,17 @@ public void notBuiltIfFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n"
+ "node {findText regexp: 'foobar', notBuiltIfFound: true, alsoCheckConsoleOutput: true}\n"));
"node {isUnix() ? sh('"
+ ECHO_UNIQUE_TEXT
+ "') : bat(\"prompt \\$G\\r\\n"
+ ECHO_UNIQUE_TEXT
+ "\")}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', notBuiltIfFound: true, alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.NOT_BUILT, build);
}
Expand All @@ -179,10 +252,12 @@ public void notFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n"));
"node {findText regexp: '"
+ UNIQUE_TEXT
+ "', alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Checking console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
rule.assertBuildStatus(Result.SUCCESS, build);
}
}