Added in globbing for loading javascript files

This commit is contained in:
stbutler11 2014-12-09 16:23:22 +00:00
parent 25be1aeb82
commit fbae33595b
7 changed files with 112 additions and 13 deletions

View File

@ -100,8 +100,8 @@ You can still use Firebug to debug when generating a specRunner HTML file (see b
* debug: use the built-in Rhino debugger (gives you the chance to set a breakpoint before firing the test suite)
* jsRootDir: the javascript install root dir. Jasmine and other should be installed here (see source)
* sourcesRootDir: your production JS files root dir.
* specs: one or more spec file to run. Default behavior: use java Class name (replaces Test with Spec, see example)
* sources: one or more JS production file which your spec needs (included before specs, d'uh)
* specs: one or more spec file to run. You may also use the [glob syntax described here](https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher%28java.lang.String%29).For example to load all javascript files in subdir use subdir/*.js. Default behavior: use java Class name (replaces Test with Spec, see example).
* sources: one or more JS production file which your spec needs (included before specs, d'uh). You can use the same glob syntax as the specs option.
* generateSpecRunner: (the HTML output, useful for firefox/firebug debugging etc)
* specRunnerSubDir: a subsidiary path to the default runner root directory where generated spec runners will be placed
* envJs: load EnvJS support (defaults to true)

View File

@ -62,8 +62,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<encoding>UTF-8</encoding>

View File

@ -1,6 +1,19 @@
package be.klak.rhino;
import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
@ -77,13 +90,62 @@ public class RhinoContext {
}
}
public void load(String path, String... jsFiles) {
for (String jsFile : jsFiles) {
load(path + jsFile);
try {
List<Path> files = getFiles(path, jsFiles);
for (Path file: files) {
load(file.toString());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void load(String fileName) {
private List<Path> getFiles(String root, String... globs) throws IOException {
// Use a priority queue as the order of the files may make a difference
final PriorityQueue<MatchResult> matchResults = new PriorityQueue<>();
if (Paths.get(root).toFile().exists()) {
final List<PathMatcher> pathMatchers = getPathMatchers(root, globs);
Files.walkFileTree(Paths.get(root), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
MatchResult matchResult = match(file, pathMatchers);
if (matchResult != null) {
matchResults.add(matchResult);
};
return super.visitFile(file, attrs);
}
});
}
List<Path> paths = new ArrayList<>();
for (MatchResult matchResult : matchResults) {
paths.add(matchResult.path);
}
return paths;
}
private List<PathMatcher> getPathMatchers(String root, String[] globs) {
List<PathMatcher> matchers = new ArrayList<>();
for (String glob : globs) {
matchers.add(FileSystems.getDefault().getPathMatcher("glob:" + root + glob));
}
return matchers;
}
private MatchResult match(Path file,
List<PathMatcher> pathMatchers) {
MatchResult matchResult = null;
for (int i = 0; i < pathMatchers.size() && matchResult == null; i++) {
if (pathMatchers.get(i).matches(file)) {
matchResult = new MatchResult(i, file);
}
}
return matchResult;
}
private void load(String fileName) {
evalJS("load('" + fileName + "')");
// Main.processFile(this.jsContext, this.jsScope, fileName);
}
@ -150,4 +212,21 @@ public class RhinoContext {
public void exit() {
Context.exit();
}
private static class MatchResult implements Comparable<MatchResult> {
private final int position;
private final Path path;
public MatchResult(int position, Path path) {
this.position = position;
this.path = path;
}
@Override
public int compareTo(MatchResult o) {
return position - o.position;
}
}
}

View File

@ -1,8 +1,10 @@
package be.klak.rhino;
import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.mozilla.javascript.EcmaError;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.ScriptableObject;
@ -81,6 +83,21 @@ public class RhinoContextTest {
assertThat(context.evalJS("loadedTwo")).isEqualTo(true);
}
@Test
public void loadGlob() {
RhinoContext context = new RhinoContext();
context.load("src/test/javascript/", "globLoadTest/g*.js");
assertThat(context.evalJS("glob1")).isEqualTo(1.0);
assertThat(context.evalJS("glob2")).isEqualTo(2.0);
try {
context.evalJS("notGlobbed");
fail("notGlobbed should not have been loaded");
} catch (EcmaError e) {
// TODO need a better way to detect that it was not loaded
}
}
// {{{ loadsJSFilesFromClasspath
@Test
public void loadsJSFilesFromClasspath() {

View File

@ -0,0 +1 @@
var glob1 = 1;

View File

@ -0,0 +1 @@
var glob2 = 2;

View File

@ -0,0 +1 @@
var notGlobbed = "Shouldn't be loaded";