Skip to content

Commit

Permalink
Initial support for require.js scriptloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ebaxt committed Nov 22, 2011
1 parent 2fded73 commit e370469
Show file tree
Hide file tree
Showing 52 changed files with 1,220 additions and 253 deletions.
12 changes: 12 additions & 0 deletions features/amd_support.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Support amd modules in javaScript

In order to support amd modules
I want the maven build to include specs using require.js format instead of script tags
So that tests can be loaded using the require.js scriptloader

Scenario: project with javascript using require.js

Given I am currently in the "jasmine-webapp-amd-support" project
When I run "mvn clean test"
Then the build should succeed
And I should see "Results: 2 specs, 0 failures"
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>jasmine-maven-plugin</name>
Expand Down
54 changes: 52 additions & 2 deletions src/main/java/com/github/searls/jasmine/AbstractJasmineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class AbstractJasmineMojo extends AbstractMojo {
* @parameter default-value="${project.basedir}${file.separator}src${file.separator}test${file.separator}javascript" expression="${jsTestSrcDir}"
*/
private File jsTestSrcDir;

/**
* Determines the browser and version profile to execute the headless specs against. Because the plugin
* executes specs using HtmlUnit, this maps 1-to-1 with the public static
Expand Down Expand Up @@ -180,6 +180,17 @@ public abstract class AbstractJasmineMojo extends AbstractMojo {
*/
protected int serverPort;

/**
* Determines the strategy to use when generation the JasmineSpecRunner. This feature allows for custom
* implementation of the runner generator. Typically this is used when using different script runners.
*
*
* Some valid examples: REQUIRE_JS
*
* @parameter default-value="DEFAULT" expression="${jasmine.specRunnerTemplate}"
*/
protected String specRunnerTemplate;

protected ScriptSearch sources;
protected ScriptSearch specs;

Expand All @@ -199,5 +210,44 @@ public final void execute() throws MojoExecutionException, MojoFailureException
}

public abstract void run() throws Exception;


public String getSourceEncoding() {
return sourceEncoding;
}

public File getCustomRunnerTemplate() {
return customRunnerTemplate;
}

public String getSpecRunnerTemplate() {
return specRunnerTemplate;
}

public File getJasmineTargetDir() {
return jasmineTargetDir;
}

public String getSrcDirectoryName() {
return srcDirectoryName;
}

public ScriptSearch getSources() {
return sources;
}

public ScriptSearch getSpecs() {
return specs;
}

public String getSpecDirectoryName() {
return specDirectoryName;
}

public List<String> getPreloadSources() {
return preloadSources;
}

public MavenProject getMavenProject() {
return mavenProject;
}
}
21 changes: 7 additions & 14 deletions src/main/java/com/github/searls/jasmine/CreatesManualRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@

import java.io.File;
import java.io.IOException;
import java.util.Set;

import com.github.searls.jasmine.io.scripts.ProjectDirScripResolver;
import com.github.searls.jasmine.runner.*;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.logging.Log;

import com.github.searls.jasmine.io.FileUtilsWrapper;
import com.github.searls.jasmine.io.scripts.RelativizesASetOfScripts;
import com.github.searls.jasmine.io.scripts.ResolvesCompleteListOfScriptLocations;
import com.github.searls.jasmine.runner.ReporterType;
import com.github.searls.jasmine.runner.SpecRunnerHtmlGenerator;

public class CreatesManualRunner {

private ResolvesCompleteListOfScriptLocations resolvesCompleteListOfScriptLocations = new ResolvesCompleteListOfScriptLocations();
private RelativizesASetOfScripts relativizesASetOfScripts = new RelativizesASetOfScripts();

private FileUtilsWrapper fileUtilsWrapper = new FileUtilsWrapper();
private AbstractJasmineMojo config;
Expand All @@ -30,19 +24,18 @@ public CreatesManualRunner(AbstractJasmineMojo config) {

public void create() throws IOException {
File runnerDestination = new File(config.jasmineTargetDir,config.manualSpecRunnerHtmlFileName);

String newRunnerHtml = new SpecRunnerHtmlGenerator(scriptsForRunner(), config.sourceEncoding).generate(ReporterType.TrivialReporter, config.customRunnerTemplate);

ProjectDirScripResolver projectDirScripResolver = new ProjectDirScripResolver(config.getMavenProject().getBasedir(), config.getSources(), config.getSpecs(), config.getPreloadSources());
SpecRunnerHtmlGenerator generator = new SpecRunnerHtmlGeneratorFactory().create(ReporterType.TrivialReporter, config, projectDirScripResolver);

String newRunnerHtml = generator.generateWitRelativePaths();
if(newRunnerDiffersFromOldRunner(runnerDestination, newRunnerHtml)) {
saveRunner(runnerDestination, newRunnerHtml);
} else {
log.info("Skipping spec runner generation, because an identical spec runner already exists.");
}
}

private Set<String> scriptsForRunner() throws IOException {
return relativizesASetOfScripts.relativize(config.jasmineTargetDir, resolvesCompleteListOfScriptLocations.resolve(config.sources, config.specs, config.preloadSources));
}

private String existingRunner(File destination) throws IOException {
String existingRunner = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class GenerateManualRunnerMojo extends AbstractJasmineMojo {
public void run() throws IOException {
if(writingAManualSpecRunnerIsNecessary()) {
getLog().info("Generating runner '"+manualSpecRunnerHtmlFileName+"' in the Jasmine plugin's target directory to open in a browser to facilitate faster feedback.");
getLog().info("SpecRunnerTemplate" + " " + this.getSpecRunnerTemplate());
new CreatesManualRunner(this).create();
} else {
getLog().warn("Skipping manual spec runner generation. Check to make sure that both JavaScript directories `"+sources.getDirectory().getAbsolutePath()+"` and `"+specs.getDirectory().getAbsolutePath()+"` exist.");
Expand Down
38 changes: 14 additions & 24 deletions src/main/java/com/github/searls/jasmine/TestMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Set;

import com.github.searls.jasmine.io.scripts.TargetDirScriptResolver;
import com.github.searls.jasmine.runner.*;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoFailureException;

import com.github.searls.jasmine.format.JasmineResultLogger;
import com.github.searls.jasmine.io.scripts.ResolvesCompleteListOfScriptLocations;
import com.github.searls.jasmine.model.JasmineResult;
import com.github.searls.jasmine.model.ScriptSearch;
import com.github.searls.jasmine.runner.ReporterType;
import com.github.searls.jasmine.runner.SpecRunnerExecutor;
import com.github.searls.jasmine.runner.SpecRunnerHtmlGenerator;


/**
Expand All @@ -24,8 +20,7 @@
* @execute lifecycle="jasmine-lifecycle" phase="process-test-resources"
*/
public class TestMojo extends AbstractJasmineMojo {
private ResolvesCompleteListOfScriptLocations resolvesCompleteListOfScriptLocations = new ResolvesCompleteListOfScriptLocations();


public void run() throws Exception {
if(!skipTests) {
getLog().info("Executing Jasmine Specs");
Expand All @@ -39,25 +34,23 @@ public void run() throws Exception {
}

private File writeSpecRunnerToOutputDirectory() throws IOException {
Set<String> scripts = resolvesCompleteListOfScriptLocations.resolve(
searchForDir(new File(jasmineTargetDir,srcDirectoryName),sources),
searchForDir(new File(jasmineTargetDir,specDirectoryName),specs),
preloadSources);
SpecRunnerHtmlGenerator htmlGenerator = new SpecRunnerHtmlGenerator(scripts, sourceEncoding);
String html = htmlGenerator.generate(ReporterType.JsApiReporter, customRunnerTemplate);


SpecRunnerHtmlGenerator generator = new SpecRunnerHtmlGeneratorFactory().create(ReporterType.JsApiReporter, this, new TargetDirScriptResolver(this));

String html = generator.generate();

getLog().debug("Writing out Spec Runner HTML " + html + " to directory " + jasmineTargetDir);
File runnerFile = new File(jasmineTargetDir,specRunnerHtmlFileName);
FileUtils.writeStringToFile(runnerFile, html);
return runnerFile;
}

private JasmineResult executeSpecs(File runnerFile) throws MalformedURLException {
JasmineResult result = new SpecRunnerExecutor().execute(
runnerFile.toURI().toURL(),
new File(jasmineTargetDir,junitXmlReportFileName),
browserVersion,
timeout, debug, getLog(), format);
runnerFile.toURI().toURL(),
new File(jasmineTargetDir, junitXmlReportFileName),
browserVersion,
timeout, debug, getLog(), format);
return result;
}

Expand All @@ -71,10 +64,7 @@ private void throwAnySpecFailures(JasmineResult result) throws MojoFailureExcept
if(haltOnFailure && !result.didPass()) {
throw new MojoFailureException("There were Jasmine spec failures.");
}
}

private ScriptSearch searchForDir(File dir, ScriptSearch search) {
return new ScriptSearch(dir,search.getIncludes(),search.getExcludes());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.github.searls.jasmine.io.scripts;

import com.github.searls.jasmine.io.RelativizesFilePaths;
import com.github.searls.jasmine.model.ScriptSearch;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public abstract class AbstractScriptResolver implements ScriptResolver {
private Set<String> sources;
private Set<String> specs;
protected File baseDir;
protected ScriptSearch scriptSearchSources;
protected ScriptSearch scriptSearchSpecs;
protected List<String> preloads;
protected RelativizesASetOfScripts relativizer = new RelativizesASetOfScripts();
protected RelativizesFilePaths relativizesFilePaths = new RelativizesFilePaths();

public void resolveScripts() throws IOException {
ResolvesLocationOfPreloadSources resolvesLocationOfPreloadSources = new ResolvesLocationOfPreloadSources();
FindsScriptLocationsInDirectory findsScriptLocationsInDirectory = new FindsScriptLocationsInDirectory();

setScriptsToPreload(new LinkedHashSet<String>(resolvesLocationOfPreloadSources.resolve(preloads, scriptSearchSources.getDirectory(), scriptSearchSpecs.getDirectory())));
setSources(new LinkedHashSet<String>(findsScriptLocationsInDirectory.find(scriptSearchSources)));
setSpecs(new LinkedHashSet<String>(findsScriptLocationsInDirectory.find(scriptSearchSpecs)));

}

public Set<String> getPreloads() {
return this.scriptsToPreload;
}

public Set<String> getSources() {
return this.sources;
}

public Set<String> getSpecs() {
return this.specs;
}

public Set<String> getAllScripts() {
return addAllScripts(scriptsToPreload, sources, specs);
}

public String getSourceDirectory() {
return scriptSearchSources.getDirectory().getAbsolutePath();
}

public String getSpecDirectoryPath() {
return scriptSearchSpecs.getDirectory().getAbsolutePath();
}

public Set<String> getSourcesRelativePath() throws IOException {
return relativizer.relativize(baseDir, this.sources);
}

public Set<String> getSpecsRelativePath() throws IOException {
return relativizer.relativize(baseDir, this.specs);
}

public Set<String> getPreloadsRelativePath() throws IOException {
return relativizer.relativize(baseDir, this.scriptsToPreload);
}

public Set<String> getAllScriptsRelativePath() throws IOException {
return addAllScripts(getPreloadsRelativePath(), getSourcesRelativePath(), getSpecsRelativePath());
}

public String getSourceDirectoryRelativePath() throws IOException {
return relativizesFilePaths.relativize(baseDir, scriptSearchSources.getDirectory());
}

public String getSpecDirectoryRelativePath() throws IOException {
return relativizesFilePaths.relativize(baseDir, scriptSearchSpecs.getDirectory());
}

private Set<String> addAllScripts(Set<String> preloadedSources, Set<String> sources, Set<String> specs) {
LinkedHashSet<String> allScripts = new LinkedHashSet<String>();
allScripts.addAll(preloadedSources);
allScripts.addAll(sources);
allScripts.addAll(specs);
return allScripts;
}

private Set<String> scriptsToPreload;

public void setScriptsToPreload(Set<String> scriptsToPreload) {
this.scriptsToPreload = scriptsToPreload;
}

public void setSources(Set<String> sources) {
this.sources = sources;
}

public void setSpecs(Set<String> specs) {
this.specs = specs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.searls.jasmine.io.scripts;

import com.github.searls.jasmine.model.ScriptSearch;

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

public class ProjectDirScripResolver extends AbstractScriptResolver {

public ProjectDirScripResolver(File projectBaseDir, ScriptSearch sources, ScriptSearch specs, List<String> preloads) throws IOException {
this.preloads = preloads;
this.scriptSearchSpecs = specs;
this.scriptSearchSources = sources;
this.baseDir = projectBaseDir;
}
}
Loading

0 comments on commit e370469

Please sign in to comment.