<commit_before>package org.disco.easyb;

import groovy.lang.GroovyShell;

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

import org.apache.commons.cli.*;

import org.disco.easyb.core.listener.DefaultListener;
import org.disco.easyb.core.listener.SpecificationListener;
import org.disco.easyb.core.util.CamelCaseConverter;
import org.disco.easyb.core.util.ReportFormat;
import org.disco.easyb.core.util.ReportType;
import org.disco.easyb.core.report.Report;
import org.disco.easyb.core.report.ReportWriter;
import org.disco.easyb.core.report.TerseReportWriter;
import org.disco.easyb.core.report.TxtStoryReportWriter;
import org.disco.easyb.core.report.XmlBehaviorReportWriter;
import org.disco.easyb.core.result.Result;
import org.disco.easyb.core.exception.InvalidArgumentException;


/**
 * usage is:
 * <p/>
 * java SpecificationRunner my/path/to/spec/MyStory.groovy -txtstory ./reports/story-report.txt
 * <p/>
 * You don't need to pass in the file name for the report either-- if no
 * path is present, then the runner will create a report in the current directory
 * with a default filename following this convention: easyb-<type>-report.<format>
 * <p/>
 * Multiple specifications can be passed in on the command line
 * <p/>
 * java SpecificationRunner my/path/to/spec/MyStory.groovy my/path/to/spec/AnotherStory.groovy
 */
public class SpecificationRunner {


  List<Report> reports;

  public SpecificationRunner() {
    this(null);
  }

  public SpecificationRunner(List<Report> reports) {
    this.reports = addDefaultReports(reports);
  }

  /**
   * @param specs collection of files that contain the specifications
   * @throws Exception if unable to write report file
   * @return BehaviorListener has status about failures and successes
   */
  public List<SpecificationListener> runSpecification(Collection<File> specs) throws Exception {

    List<SpecificationListener> listeners = new ArrayList<SpecificationListener>();

    SpecificationListener listener = new DefaultListener();

    for (File file : specs) {
    	listener.setSpecificationName(file.getName());
        if (file.getName().contains("Story.groovy")) {
        	listener.gotResult(new Result(new CamelCaseConverter(file.getName().substring(0, file.getName().indexOf("Story.groovy"))).toPhrase(), SpecificationBinding.STORY, Result.SUCCEEDED));
          new GroovyShell(SpecificationBinding.getBinding(listener)).evaluate(file);
        } else {
        	if(!file.getName().contains("Behavior.groovy")){
        		System.out.println("You should consider ending your specification file (" +
        			file.getName() + ") with either Story or Behavior. " + 
        			"See easyb documentation for more details. ");
        	}
        	new GroovyShell(SpecificationBinding.getBinding(listener)).evaluate(file);
        } 
    }

    for(Report report : reports) {
      ReportWriter reportWriter;
      if(report.getFormat().concat(report.getType()).equals(Report.TXT_STORY)) {
        reportWriter = new TxtStoryReportWriter(report, listener);
      } else if(report.getFormat().concat(report.getType()).equals(Report.XML_BEHAVIOR)) {
        reportWriter = new XmlBehaviorReportWriter(report, listener);
      } else {
        reportWriter = new TerseReportWriter(report, listener);
      }

      reportWriter.writeReport();
    }

    listeners.add(listener);
    return listeners;
  }


  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    Options options = getOptionsForMain();


    try {
        CommandLine commandLine = getCommandLineForMain(args, options);
        validateArguments(commandLine);

        SpecificationRunner runner = new SpecificationRunner(getConfiguredReports(commandLine));

        List<SpecificationListener> listeners = runner.runSpecification(getFileCollection(commandLine.getArgs()));

        notifyAndExitOnSpecificationFailures(listeners);

    } catch (InvalidArgumentException iae) {
      System.out.println(iae.getMessage());
      handleHelpForMain(options);
    } catch (ParseException pe) {
      System.out.println(pe.getMessage());
      handleHelpForMain(options);
    } catch (Exception e) {
      System.err.println("There was an error running the script");
      e.printStackTrace(System.err);
      System.exit(-6);
    }
  }

  private static void validateArguments(CommandLine commandLine) throws InvalidArgumentException {
    if(commandLine.getArgs().length == 0) {
      throw new InvalidArgumentException("Required Arguments not passed in.");
    }
  }

  private static void notifyAndExitOnSpecificationFailures(List<SpecificationListener> listeners) {
    int totalSpecificationFailures = 0;
    for(SpecificationListener listener : listeners) {
      if(listener.hasBehaviorFailures()) {
        totalSpecificationFailures++;
      }
    }
    if(totalSpecificationFailures > 0) {<commit_msg>moved to jdk standard IllegalArgumentException.. still catching it as if a checked ex in order to print out the help<commit_after>package org.disco.easyb;

import groovy.lang.GroovyShell;

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

import org.apache.commons.cli.*;

import org.disco.easyb.core.listener.DefaultListener;
import org.disco.easyb.core.listener.SpecificationListener;
import org.disco.easyb.core.util.CamelCaseConverter;
import org.disco.easyb.core.util.ReportFormat;
import org.disco.easyb.core.util.ReportType;
import org.disco.easyb.core.report.Report;
import org.disco.easyb.core.report.ReportWriter;
import org.disco.easyb.core.report.TerseReportWriter;
import org.disco.easyb.core.report.TxtStoryReportWriter;
import org.disco.easyb.core.report.XmlBehaviorReportWriter;
import org.disco.easyb.core.result.Result;


/**
 * usage is:
 * <p/>
 * java SpecificationRunner my/path/to/spec/MyStory.groovy -txtstory ./reports/story-report.txt
 * <p/>
 * You don't need to pass in the file name for the report either-- if no
 * path is present, then the runner will create a report in the current directory
 * with a default filename following this convention: easyb-<type>-report.<format>
 * <p/>
 * Multiple specifications can be passed in on the command line
 * <p/>
 * java SpecificationRunner my/path/to/spec/MyStory.groovy my/path/to/spec/AnotherStory.groovy
 */
public class SpecificationRunner {


  List<Report> reports;

  public SpecificationRunner() {
    this(null);
  }

  public SpecificationRunner(List<Report> reports) {
    this.reports = addDefaultReports(reports);
  }

  /**
   * @param specs collection of files that contain the specifications
   * @throws Exception if unable to write report file
   * @return BehaviorListener has status about failures and successes
   */
  public List<SpecificationListener> runSpecification(Collection<File> specs) throws Exception {

    List<SpecificationListener> listeners = new ArrayList<SpecificationListener>();

    SpecificationListener listener = new DefaultListener();

    for (File file : specs) {
    	listener.setSpecificationName(file.getName());
        if (file.getName().contains("Story.groovy")) {
        	listener.gotResult(new Result(new CamelCaseConverter(file.getName().substring(0, file.getName().indexOf("Story.groovy"))).toPhrase(), SpecificationBinding.STORY, Result.SUCCEEDED));
          new GroovyShell(SpecificationBinding.getBinding(listener)).evaluate(file);
        } else {
        	if(!file.getName().contains("Behavior.groovy")){
        		System.out.println("You should consider ending your specification file (" +
        			file.getName() + ") with either Story or Behavior. " + 
        			"See easyb documentation for more details. ");
        	}
        	new GroovyShell(SpecificationBinding.getBinding(listener)).evaluate(file);
        } 
    }

    for(Report report : reports) {
      ReportWriter reportWriter;
      if(report.getFormat().concat(report.getType()).equals(Report.TXT_STORY)) {
        reportWriter = new TxtStoryReportWriter(report, listener);
      } else if(report.getFormat().concat(report.getType()).equals(Report.XML_BEHAVIOR)) {
        reportWriter = new XmlBehaviorReportWriter(report, listener);
      } else {
        reportWriter = new TerseReportWriter(report, listener);
      }

      reportWriter.writeReport();
    }

    listeners.add(listener);
    return listeners;
  }


  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    Options options = getOptionsForMain();


    try {
        CommandLine commandLine = getCommandLineForMain(args, options);
        validateArguments(commandLine);

        SpecificationRunner runner = new SpecificationRunner(getConfiguredReports(commandLine));

        List<SpecificationListener> listeners = runner.runSpecification(getFileCollection(commandLine.getArgs()));

        notifyAndExitOnSpecificationFailures(listeners);

    } catch (IllegalArgumentException iae) {
      System.out.println(iae.getMessage());
      handleHelpForMain(options);
    } catch (ParseException pe) {
      System.out.println(pe.getMessage());
      handleHelpForMain(options);
    } catch (Exception e) {
      System.err.println("There was an error running the script");
      e.printStackTrace(System.err);
      System.exit(-6);
    }
  }

  private static void validateArguments(CommandLine commandLine) throws IllegalArgumentException {
    if(commandLine.getArgs().length == 0) {
      throw new IllegalArgumentException("Required Arguments not passed in.");
    }
  }

  private static void notifyAndExitOnSpecificationFailures(List<SpecificationListener> listeners) {
    int totalSpecificationFailures = 0;
    for(SpecificationListener listener : listeners) {
      if(listener.hasBehaviorFailures()) {
        totalSpecificationFailures++;
      }
    }
    if(totalSpecificationFailures > 0) {
{'fraction_non_alphanumeric': 0.10754204007652804, 'fraction_numerical': 0.001006947940791461, 'mean_word_length': 3.9290322580645163, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3864535', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2721, 'n_words': 609}