Skip to content

Commit

Permalink
Reformat coverage and move export to command
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Jun 13, 2024
1 parent 2bc23bc commit 2f73ec3
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 20 deletions.
9 changes: 2 additions & 7 deletions src/main/java/com/askimed/nf/test/App.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.askimed.nf.test;

import com.askimed.nf.test.commands.CleanCommand;
import com.askimed.nf.test.commands.GenerateTestsCommand;
import com.askimed.nf.test.commands.InitCommand;
import com.askimed.nf.test.commands.ListTestsCommand;
import com.askimed.nf.test.commands.RunTestsCommand;
import com.askimed.nf.test.commands.UpdatePluginsCommand;
import com.askimed.nf.test.commands.VersionCommand;
import com.askimed.nf.test.commands.*;

import ch.qos.logback.classic.Level;
import picocli.CommandLine;
Expand Down Expand Up @@ -35,6 +29,7 @@ public int run(String[] args) {
commandLine.addSubcommand("clean", new CleanCommand());
commandLine.addSubcommand("init", new InitCommand());
commandLine.addSubcommand("test", new RunTestsCommand());
commandLine.addSubcommand("coverage", new CoverageCommand());
commandLine.addSubcommand("list", new ListTestsCommand());
commandLine.addSubcommand("ls", new ListTestsCommand());
commandLine.addSubcommand("generate", new GenerateTestsCommand());
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/com/askimed/nf/test/commands/CoverageCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.askimed.nf.test.commands;

import com.askimed.nf.test.config.Config;
import com.askimed.nf.test.lang.dependencies.Coverage;
import com.askimed.nf.test.lang.dependencies.DependencyResolver;
import com.askimed.nf.test.util.AnsiColors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine.Command;
import picocli.CommandLine.Help.Visibility;
import picocli.CommandLine.Option;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Command(name = "coverage")
public class CoverageCommand extends AbstractCommand {

private static final String SHARD_STRATEGY_ROUND_ROBIN = "round-robin";

@Option(names = {
"--csv" }, description = "Write coverage results in csv format", required = false, showDefaultValue = Visibility.ALWAYS)
private String csv = null;

@Option(names = { "--config",
"-c" }, description = "nf-test.config filename", required = false, showDefaultValue = Visibility.ALWAYS)
private String configFilename = Config.FILENAME;

private static Logger log = LoggerFactory.getLogger(CoverageCommand.class);

@Override
public Integer execute() throws Exception {

List<File> scripts = new ArrayList<File>();
Config config = null;

try {

File defaultConfigFile = null;
boolean defaultWithTrace = true;
try {
File configFile = new File(configFilename);
if (configFile.exists()) {
log.info("Load config from file {}...", configFile.getAbsolutePath());
config = Config.parse(configFile);
} else {
System.out.println(AnsiColors.yellow("Warning: This pipeline has no nf-test config file."));
log.warn("No nf-test config file found.");
}

} catch (Exception e) {

System.out.println(AnsiColors.red("Error: Syntax errors in nf-test config file: " + e));
log.error("Parsing config file failed", e);
return 2;

}

File baseDir = new File(new File("").getAbsolutePath());
DependencyResolver resolver = new DependencyResolver(baseDir);
resolver.setFollowingDependencies(true);


if (config != null) {
resolver.buildGraph(config.getIgnore(), config.getTriggers());
} else {
resolver.buildGraph();
}

Coverage coverage = new Coverage(resolver).getAll();
if (csv != null) {
coverage.exportAsCsv(csv);
} else {
coverage.printDetails();
}

return 0;

} catch (Throwable e) {

System.out.println(AnsiColors.red("Error: " + e));log.error("Running tests failed.", e);
return 1;

}

}

}
18 changes: 9 additions & 9 deletions src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ public Integer execute() throws Exception {
return 2;
}

List<PathMatcher> ignorePatterns = new Vector<PathMatcher>();
File baseDir = new File(new File("").getAbsolutePath());
DependencyResolver resolver = new DependencyResolver(baseDir);
resolver.setFollowingDependencies(followDependencies);
Expand Down Expand Up @@ -231,20 +230,13 @@ public Integer execute() throws Exception {

AnsiText.printBulletList(scripts);

if (coverage) {
new Coverage(resolver).getByFiles(testPaths).print();
}

} else {
if (config != null) {
resolver.buildGraph(config.getIgnore(), config.getTriggers());
} else {
resolver.buildGraph();
}
scripts = resolver.findTestsByFiles(testPaths);
if (coverage) {
new Coverage(resolver).getAll().print();
}
}

if (graph != null) {
Expand Down Expand Up @@ -304,7 +296,15 @@ public Integer execute() throws Exception {
System.out.println(AnsiColors.yellow("Dry run mode activated: tests are not executed, just listed."));
}

return engine.execute();
int exitStatus = engine.execute();

if (coverage && findRelatedTests) {
new Coverage(resolver).getByFiles(testPaths).print();
} else if (coverage) {
new Coverage(resolver).getAll().print();
}

return exitStatus;

} catch (Throwable e) {

Expand Down
80 changes: 76 additions & 4 deletions src/main/java/com/askimed/nf/test/lang/dependencies/Coverage.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.askimed.nf.test.lang.dependencies;

import com.askimed.nf.test.core.TestExecutionResult;
import com.askimed.nf.test.core.TestSuiteExecutionResult;
import com.askimed.nf.test.core.reports.CsvReportWriter;
import com.askimed.nf.test.util.AnsiColors;
import com.askimed.nf.test.util.AnsiText;
import com.opencsv.CSVWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Vector;
Expand All @@ -19,12 +27,15 @@ public class Coverage {

private static Logger log = LoggerFactory.getLogger(Coverage.class);

private File baseDir = null;

public Coverage(DependencyGraph graph) {
this.graph = graph;
}

public Coverage(DependencyResolver resolver) {
this.graph = resolver.getGraph();
baseDir = resolver.getBaseDir();
}

public void add(File file, boolean covered) {
Expand Down Expand Up @@ -57,6 +68,8 @@ public Coverage getAll(){

}

items.sort(new CoverageItemSorter());

long time1 = System.currentTimeMillis();

log.info("Calculated coverage for {} files in {} sec", graph.size(), (time1 - time0) / 1000.0);
Expand Down Expand Up @@ -87,6 +100,8 @@ public Coverage getByFiles(List<File> files){

}

items.sort(new CoverageItemSorter());

long time1 = System.currentTimeMillis();

log.info("Calculated coverage for {} files in {} sec", graph.size(), (time1 - time0) / 1000.0);
Expand All @@ -95,14 +110,71 @@ public Coverage getByFiles(List<File> files){
}

public void print() {
DecimalFormat decimalFormat = new DecimalFormat("#.##");
printLabel();
System.out.println();
}

public void printDetails() {
System.out.println();
System.out.print("Coverage: " + getCoveredItems() + "/" + getItems().size());
System.out.println(" (" + decimalFormat.format(getCoveredItems() / (float) getItems().size() * 100) + "%)");
System.out.println("Files:");
for (Coverage.CoverageItem item : getItems()) {
System.out.println(" - " + (item.isCovered() ? AnsiColors.green(item.getFile().getAbsolutePath()) : AnsiColors.red(item.getFile().getAbsolutePath())));
String label = item.getFile().getAbsolutePath();
if (baseDir != null) {
label = Paths.get(baseDir.getAbsolutePath()).relativize(item.getFile().toPath()).toString();
}
System.out.println(" \u2022 " + (item.isCovered() ? AnsiColors.green(label) : AnsiColors.red(label)));
}
System.out.println();
printLabel();
System.out.println();
}

private void printLabel() {
float coverage = getCoveredItems() / (float) getItems().size();
System.out.print(getColor("COVERAGE:", coverage) + " " + formatCoverage(coverage));
System.out.println( " [" + getCoveredItems() + " of " + getItems().size() + " files]");
}

private String getColor(String label, float value) {
if (value < 0.5) {
return AnsiColors.red(label);
} else if (value < 0.9) {
return AnsiColors.yellow(label);
} else {
return AnsiColors.green(label);
}
}

private String formatCoverage(float value) {
DecimalFormat decimalFormat = new DecimalFormat("#.##");
return decimalFormat.format(value * 100) + "%";
}

public void exportAsCsv(String filename) throws IOException {
String[] header = new String[]{
"filename",
"covered",
"type"
};

CSVWriter writer = new CSVWriter(new FileWriter(new File(filename)));
writer.writeNext(header);
for (Coverage.CoverageItem item : getItems()) {
String[] line = new String[]{
item.getFile().getAbsolutePath(),
item.isCovered() + "",
"unknown"
};

writer.writeNext(line);
}

writer.close();
System.out.println();
printLabel();
System.out.println();
System.out.println("Wrote coverage report to file " + filename + "\n");

}

public static class CoverageItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.askimed.nf.test.lang.dependencies;

public class CoverageItemSorter implements java.util.Comparator<Coverage.CoverageItem> {

@Override
public int compare(Coverage.CoverageItem o1, Coverage.CoverageItem o2) {
return o1.getFile().getAbsolutePath().compareTo(o2.getFile().getAbsolutePath());
}
}

0 comments on commit 2f73ec3

Please sign in to comment.