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

Adding support for single file instrumentation via stdio #100

Merged
merged 1 commit into from
Sep 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/main/java/jscover/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ You should also get your employer (if you work as a programmer) or your

import jscover.filesystem.ConfigurationForFS;
import jscover.filesystem.FileSystemInstrumenter;
import jscover.stdout.ConfigurationForStdOut;
import jscover.stdout.StdOutInstrumenter;
import jscover.server.ConfigurationForServer;
import jscover.server.WebDaemon;
import jscover.util.IoUtils;
Expand All @@ -368,6 +370,7 @@ public class Main {
public static final String VERSION_PREFIX2 = "--version";
public static final String SERVER_PREFIX = "-ws";
public static final String FILESYSTEM_PREFIX = "-fs";
public static final String STDOUT_PREFIX = "-io";
public static final Properties properties = new Properties();
public static String reportSrcSubDir = "original-src";

Expand All @@ -378,6 +381,7 @@ public class Main {
private MainHelper mainHelper = new MainHelper();
private WebDaemon webDaemon = new WebDaemon();
private FileSystemInstrumenter fileSystemInstrumenter = new FileSystemInstrumenter();
private StdOutInstrumenter stdOutInstrumenter = new StdOutInstrumenter();
private IoUtils ioUtils = IoUtils.getInstance();

void initialize() throws IOException {
Expand Down Expand Up @@ -405,6 +409,7 @@ private void checkDependantClasses() throws IOException {
private boolean printVersion;
private boolean isServer;
private boolean isFileSystem;
private boolean isStdOut;
private int exitStatus;

public static void main(String[] args) throws IOException {
Expand All @@ -420,6 +425,8 @@ public void runMain(String[] args) throws IOException {
runServer(args);
} else if (isFileSystem()) {
runFileSystem(args);
} else if (isStdOut()) {
runSingleFile(args);
} else if (showCharSets()) {
System.out.println("Valid encodings:");
SortedMap<String, Charset> charSet = Charset.availableCharsets();
Expand Down Expand Up @@ -472,6 +479,18 @@ private void runServer(String[] args) {
}
}

private void runSingleFile(String[] args) {
ConfigurationForStdOut configuration = ConfigurationForStdOut.parse(args);
configuration.setProperties(properties);
if (configuration.isInvalid())
exitStatus = 1;
if (configuration.showHelp()) {
System.out.println(configuration.getHelpText());
} else {
stdOutInstrumenter.run(configuration);
}
}

public Main parse(String[] args) {
for (String arg : args) {
if (arg.equals(HELP_PREFIX1) || arg.equals(HELP_PREFIX2)) {
Expand All @@ -482,6 +501,8 @@ public Main parse(String[] args) {
isServer = true;
} else if (arg.equals(FILESYSTEM_PREFIX)) {
isFileSystem = true;
} else if (arg.equals(STDOUT_PREFIX)) {
isStdOut = true;
} else if (arg.equals(CHARSET_PREFIX)) {
showCharsets = true;
}
Expand All @@ -494,10 +515,10 @@ public Main parse(String[] args) {
}

private boolean validOptions() {
if (isServer && isFileSystem) {
if ((isServer && (isFileSystem || isStdOut)) || (isFileSystem && isStdOut)) {
return false;
}
return isServer || isFileSystem || showHelp || printVersion || showCharsets;
return isServer || isFileSystem || isStdOut || showHelp || printVersion || showCharsets;
}

public Boolean printVersion() {
Expand All @@ -521,6 +542,10 @@ public boolean isFileSystem() {
return isFileSystem;
}

public boolean isStdOut() {
return isStdOut;
}

public int getExitStatus() {
return exitStatus;
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/jscover/instrument/BranchInstrumentor.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,13 @@ public int getLinePosition(AstNode node) {
}

protected String getJsLineInitialization() {
StringBuilder sb = new StringBuilder(format("if (! _$jscoverage['%s'].branchData) {\n", uri));
sb.append(format(" _$jscoverage['%s'].branchData = {};\n", uri));
String fileName = uri.replace("\\", "\\\\").replace("'", "\\'");
StringBuilder sb = new StringBuilder(format("if (! _$jscoverage['%s'].branchData) {\n", fileName));
sb.append(format(" _$jscoverage['%s'].branchData = {};\n", fileName));
for (Integer line : lineConditionMap.keySet()) {
sb.append(format(initBranchLine, uri, line));
for (Integer condition: lineConditionMap.get(line))
sb.append(format(initBranchCondition, uri, line, condition));
sb.append(format(initBranchLine, fileName, line));
for (Integer condition : lineConditionMap.get(line))
sb.append(format(initBranchCondition, fileName, line, condition));
}
sb.append("}\n");
return sb.toString();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/jscover/instrument/InstrumenterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,11 @@ public void instrumentJSForFileSystem(CompilerEnvirons compilerEnvirons, File sr
String jsInstrumented = sourceProcessor.processSourceForFileSystem(source);
ioUtils.copy(jsInstrumented, dest);
}

public void instrumentJSForStdOut(CompilerEnvirons compilerEnvirons, File srcFile, String uri, boolean includeBranch, boolean includeFunction) {
SourceProcessor sourceProcessor = new SourceProcessor(compilerEnvirons, srcFile.getAbsolutePath(), includeBranch, includeFunction);
String source = ioUtils.loadFromFileSystem(srcFile);
String jsInstrumented = sourceProcessor.processSourceForFileSystem(source);
System.out.print(jsInstrumented);
}
}
6 changes: 4 additions & 2 deletions src/main/java/jscover/instrument/SourceProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ protected String processSourceWithoutHeader(String sourceURI, String source) {
String instrumentedSource = instrumentSource(sourceURI, source);

String jsLineInitialization = getJsLineInitialization(uri, instrumenter.getValidLines());

if (includeFunctionCoverage)
jsLineInitialization += getJsFunctionInitialization(uri, instrumenter.getNumFunctions());

Expand All @@ -434,6 +434,7 @@ protected String instrumentSource(String sourceURI, String source) {
}

protected String getJsLineInitialization(String fileName, SortedSet<Integer> validLines) {
fileName = fileName.replace("\\", "\\\\").replace("'", "\\'");
StringBuilder sb = new StringBuilder(format("if (! _$jscoverage['%s']) {\n", fileName));
sb.append(format(" _$jscoverage['%s'] = {};\n", fileName));
sb.append(format(" _$jscoverage['%s'].lineData = [];\n", fileName));
Expand All @@ -443,9 +444,10 @@ protected String getJsLineInitialization(String fileName, SortedSet<Integer> val
sb.append("}\n");
return sb.toString();
}

// Function Coverage (HA-CA)
protected String getJsFunctionInitialization(String fileName, int numFunction) {
fileName = fileName.replace("\\", "\\\\").replace("'", "\\'");
StringBuilder sb = new StringBuilder(format("if (! _$jscoverage['%s'].functionData) {\n", fileName));
sb.append(format(" _$jscoverage['%s'].functionData = [];\n", fileName));
for ( int i = 0; i < numFunction; ++i) {
Expand Down
Loading