All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.searls.jasmine.runner.CreatesRunner Maven / Gradle / Ivy

Go to download

A JavaScript unit test plugin that processes JavaScript sources and Jasmine specs, prepares test runner HTML files, executes Jasmine specs headlessly with HtmlUnit, and produces JUnit XML reports

There is a newer version: 3.0-beta-02
Show newest version
package com.github.searls.jasmine.runner;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.logging.Log;

import com.github.searls.jasmine.config.JasmineConfiguration;
import com.github.searls.jasmine.io.scripts.BasicScriptResolver;
import com.github.searls.jasmine.io.scripts.ContextPathScriptResolver;
import com.github.searls.jasmine.io.scripts.ScriptResolver;

public class CreatesRunner {

  private final JasmineConfiguration config;

  private final Log log;
  private final String runnerFileName;
  private final ReporterType reporterType;

  public CreatesRunner(JasmineConfiguration config, Log log, String runnerFileName, ReporterType reporterType) {
    this.config = config;
    this.runnerFileName = runnerFileName;
    this.reporterType = reporterType;
    this.log = log;
  }

  public String getRunnerFile() {
    return this.runnerFileName;
  }

  public void create() throws IOException {
    File runnerDestination = new File(this.config.getJasmineTargetDir(),this.runnerFileName);
    ScriptResolver resolver = new BasicScriptResolver(
        config.getBasedir(),
        config.getSources(),
        config.getSpecs(),
        config.getPreloadSources());
    resolver = new ContextPathScriptResolver(
        resolver,
        config.getSrcDirectoryName(),
        config.getSpecDirectoryName());

    SpecRunnerHtmlGenerator generator = new SpecRunnerHtmlGeneratorFactory().create(this.reporterType, this.config, resolver);

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

  private String existingRunner(File destination) throws IOException {
    String existingRunner = null;
    try {
      if(destination.exists()) {
        existingRunner = FileUtils.readFileToString(destination);
      }
    } catch(IOException e) {
      this.log.warn("An error occurred while trying to open an existing manual spec runner. Continuing.");
    }
    return existingRunner;
  }

  private boolean newRunnerDiffersFromOldRunner(File runnerDestination, String newRunner) throws IOException {
    return !StringUtils.equals(newRunner, this.existingRunner(runnerDestination));
  }

  private void saveRunner(File runnerDestination, String newRunner) throws IOException {
    FileUtils.writeStringToFile(runnerDestination, newRunner, this.config.getSourceEncoding());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy