Skip to content

Commit

Permalink
allow users to override file names of report artifacts #520
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Oct 19, 2018
1 parent 839bdf3 commit 53e5d0a
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 39 deletions.
2 changes: 1 addition & 1 deletion karate-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you want to customize the start-up, you can use a batch-file:

Here a batch-file called `chrome` was created in the system `PATH` (and made executable) with the following contents:

```sh
```bash
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" $*
```

Expand Down
2 changes: 1 addition & 1 deletion karate-core/src/main/java/com/intuit/karate/IdeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void exec(String[] args) {
if (isIntellij) {
log(result);
}
Engine.saveResultHtml(Engine.getBuildDir() + File.separator + "surefire-reports", result);
Engine.saveResultHtml(Engine.getBuildDir() + File.separator + "surefire-reports", result, null);
}
}

Expand Down
6 changes: 3 additions & 3 deletions karate-core/src/main/java/com/intuit/karate/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public static Results parallel(String tagSelector, List<Resource> resources, Exe
unit.setNext(() -> {
FeatureResult result = execContext.result;
if (result.getScenarioCount() > 0) { // possible that zero scenarios matched tags
File file = Engine.saveResultJson(finalReportDir, result);
Engine.saveResultXml(finalReportDir, result);
File file = Engine.saveResultJson(finalReportDir, result, null);
Engine.saveResultXml(finalReportDir, result, null);
String status = result.isFailed() ? "fail" : "pass";
logger.info("<<{}>> feature {} of {}: {}", status, index, count, feature.getRelativePath());
result.printStats(feature.getRelativePath(), file.getPath());
Expand Down Expand Up @@ -142,7 +142,7 @@ public static Results parallel(String tagSelector, List<Resource> resources, Exe
}
results.setFeatureCount(executedFeatureCount);
results.printStats(threadCount);
Engine.saveTimelineHtml(reportDir, results);
Engine.saveTimelineHtml(reportDir, results, null);
results.setReportDir(reportDir);
return results;
}
Expand Down
28 changes: 20 additions & 8 deletions karate-core/src/main/java/com/intuit/karate/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ public static Result executeStep(Step step, Actions actions) {
}
}

public static File saveResultJson(String targetDir, FeatureResult result) {
public static File saveResultJson(String targetDir, FeatureResult result, String fileName) {
List<Map> single = Collections.singletonList(result.toMap());
String json = JsonUtils.toJson(single);
File file = new File(targetDir + File.separator + result.getPackageQualifiedName() + ".json");
if (fileName == null) {
fileName = File.separator + result.getPackageQualifiedName() + ".json";
}
File file = new File(targetDir + fileName);
FileUtils.writeToFile(file, json);
return file;
}
Expand Down Expand Up @@ -203,7 +206,7 @@ private static Throwable appendSteps(List<StepResult> steps, StringBuilder sb) {
return error;
}

public static File saveResultXml(String targetDir, FeatureResult result) {
public static File saveResultXml(String targetDir, FeatureResult result, String fileName) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
formatter.applyPattern("0.######");
Document doc = XmlUtils.newDocument();
Expand Down Expand Up @@ -250,7 +253,10 @@ public static File saveResultXml(String targetDir, FeatureResult result) {
root.setAttribute("failures", failureCount + "");
root.setAttribute("time", formatNanos(totalDuration, formatter));
String xml = XmlUtils.toString(doc, true);
File file = new File(targetDir + File.separator + baseName + ".xml");
if (fileName == null) {
fileName = baseName + ".xml";
}
File file = new File(targetDir + File.separator + fileName);
FileUtils.writeToFile(file, xml);
return file;
}
Expand Down Expand Up @@ -358,7 +364,7 @@ private static void stepHtml(Document doc, DecimalFormat formatter, StepResult s
}
}

public static File saveResultHtml(String targetDir, FeatureResult result) {
public static File saveResultHtml(String targetDir, FeatureResult result, String fileName) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
formatter.applyPattern("0.######");
String html = getClasspathResource("report-template.html");
Expand All @@ -381,7 +387,10 @@ public static File saveResultHtml(String targetDir, FeatureResult result) {
stepHtml(doc, formatter, stepResult, scenarioDiv);
}
}
File file = new File(targetDir + File.separator + baseName + ".html");
if (fileName == null) {
fileName = File.separator + baseName + ".html";
}
File file = new File(targetDir + fileName);
String xml = "<!DOCTYPE html>\n" + XmlUtils.toString(doc, false);
try {
FileUtils.writeToFile(file, xml);
Expand All @@ -399,7 +408,7 @@ private static long getElapsedTime(long startTime) {
return System.nanoTime() - startTime;
}

public static void saveTimelineHtml(String targetDir, Results results) {
public static void saveTimelineHtml(String targetDir, Results results, String fileName) {
Map<String, Integer> groupsMap = new LinkedHashMap();
List<ScenarioResult> scenarioResults = results.getScenarioResults();
List<Map> items = new ArrayList(scenarioResults.size());
Expand Down Expand Up @@ -438,7 +447,10 @@ public static void saveTimelineHtml(String targetDir, Results results) {
+ "timeline.setOptions({ groupOrder: 'content' });\n"
+ "timeline.setGroups(groups);\n"
+ "timeline.setItems(items);\n");
File htmlFile = new File(targetDir + File.separator + "timeline.html");
if (fileName == null) {
fileName = File.separator + "timeline.html";
}
File htmlFile = new File(targetDir + fileName);
String html = getClasspathResource("timeline-template.html");
html = html.replaceFirst("//timeline//", sb.toString());
FileUtils.writeToFile(htmlFile, html);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
* @author pthomas3
*/
public class AppSession {

public final File featureFile;

private Feature feature; // mutable, can be re-built
private String envString; // can be changed

public final File featureFile;
public final HeaderPanel headerPanel;
public final FeaturePanel featurePanel;
public final VarsPanel varsPanel;
Expand Down
36 changes: 23 additions & 13 deletions karate-core/src/main/java/com/intuit/karate/ui/ExamplesPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
* @author pthomas3
*/
public class ExamplesPanel extends TitledPane {
private final VBox content;

private final VBox content;
private final AppSession session;

private Scenario scenario;
private final List<StepPanel> stepPanels;

public ExamplesPanel(AppSession session, Scenario scenario) {
super();
content = new VBox(0);
Expand All @@ -53,25 +53,35 @@ public ExamplesPanel(AppSession session, Scenario scenario) {
this.session = session;
this.scenario = scenario;
stepPanels = new ArrayList(scenario.getSteps().size());
initTitleAndContent();
initTitleAndContent();
}

private Optional<StepPanel> previousStep = Optional.empty();

private void addStepPanel(Step step) {
StepPanel stepPanel = new StepPanel(session, step, previousStep);
content.getChildren().add(stepPanel);
stepPanels.add(stepPanel);
previousStep = Optional.of(stepPanel);
}

private void initTitleAndContent() {
setText(scenario.getName());
Optional<StepPanel> previousStep = Optional.empty();
setText(scenario.getName());
if (!scenario.isBackgroundDone()) {
for (Step step : scenario.getBackgroundSteps()) {
addStepPanel(step);
}
}
for (Step step : scenario.getSteps()) {
StepPanel stepPanel = new StepPanel(session, step, previousStep);
content.getChildren().add(stepPanel);
stepPanels.add(stepPanel);
previousStep = Optional.of(stepPanel);
}
addStepPanel(step);
}
}

public void refresh(AppAction action) {
scenario = session.refresh(scenario);
for (StepPanel panel : stepPanels) {
panel.action(action);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;

import static com.intuit.karate.ui.App.PADDING_INSET;
import javafx.geometry.Insets;

/**
*
Expand All @@ -51,6 +52,7 @@ public FeaturePanel(AppSession session) {
this.setPadding(PADDING_INSET);
this.scrollPane = new ScrollPane();
content = new VBox(5.0);
content.setPadding(new Insets(5, 0, 5, 0));
this.scrollPane.setContent(content);
this.scrollPane.setFitToWidth(true);
this.session = session;
Expand All @@ -63,6 +65,7 @@ public FeaturePanel(AppSession session) {
private void addSections() {
final Feature feature = session.getFeature();
TextFlow flow = new TextFlow();
flow.setPadding(new Insets(0, 5, 0, 5));
Text keyword = new Text("Feature: ");
Text name = new Text(feature.getName());
flow.getChildren().addAll(keyword, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private boolean contains(String reportPath, String textToFind) {
private static String resultXml(String name) {
Feature feature = FeatureParser.parse("classpath:com/intuit/karate/" + name);
FeatureResult result = Engine.executeFeatureSync(feature, null, null);
File file = Engine.saveResultXml("target", result);
File file = Engine.saveResultXml("target", result, null);
return FileUtils.toString(file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ public void testEngineForSimpleFeature() {
ScenarioResult sr = (ScenarioResult) result.getScenarioResults().get(0);
map = sr.toMap();
Match.equals(map.get("tags"), "[{ name: '@bar', line: 5 }]");
Engine.saveResultJson("target", result);
Engine.saveResultXml("target", result);
Engine.saveResultJson("target", result, null);
Engine.saveResultXml("target", result, null);
}

@Test
public void testEngineForSimpleFeatureWithBackground() {
FeatureResult result = execute("test-simple-background.feature");
assertEquals(1, result.getScenarioResults().size());
Engine.saveResultJson("target", result);
Engine.saveResultXml("target", result);
Engine.saveResultJson("target", result, null);
Engine.saveResultXml("target", result, null);
}

@Test
public void testEngineForError() {
FeatureResult result = execute("test-error.feature");
Engine.saveResultJson("target", result);
Engine.saveResultXml("target", result);
Engine.saveResultJson("target", result, null);
Engine.saveResultXml("target", result, null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static FeatureResult result(String name) {
}

private static String xml(FeatureResult result) {
File file = Engine.saveResultXml("target", result);
File file = Engine.saveResultXml("target", result, null);
return FileUtils.toString(file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class FeatureReuseTest {
private static String resultXml(String name) {
Feature feature = FeatureParser.parse("classpath:com/intuit/karate/core/" + name);
FeatureResult result = Engine.executeFeatureSync(feature, null, null);
File file = Engine.saveResultXml("target", result);
File file = Engine.saveResultXml("target", result, null);
return FileUtils.toString(file);
}

Expand Down
17 changes: 17 additions & 0 deletions karate-demo/src/test/java/demo/outline/ExamplesUiRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package demo.outline;

import com.intuit.karate.ui.App;
import org.junit.Test;

/**
*
* @author pthomas3
*/
public class ExamplesUiRunner {

@Test
public void testApp() {
App.run("src/test/java/demo/outline/examples.feature", "mock");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected void runChild(Feature feature, RunNotifier notifier) {
}
notifier.fireTestFinished(scenarioDescription);
}
Engine.saveResultHtml(Engine.getBuildDir() + File.separator + "surefire-reports", result);
Engine.saveResultHtml(Engine.getBuildDir() + File.separator + "surefire-reports", result, null);
}

@Override
Expand Down

0 comments on commit 53e5d0a

Please sign in to comment.