Package org.pyant.tasks

Source Code of org.pyant.tasks.PythonTestTask

/*
* Created on May 7, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package org.pyant.tasks;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.FileSet;


/**
* @author Ron Smith
*
* Ant task which executes Python unit tests.
*
* <py-test pythonpathref="project.python.path" >
*   <fileset dir="testscripts2">
*     <include name="** /*Test.py"/>
*   </fileset>
* </py-test>
*
*/
public class PythonTestTask extends PythonInteractiveBaseTask
{
  /**
   * Filesets of the unit tests to execute.
   */
  protected ArrayList filesets = new ArrayList();
 
  /**
   * Working directory to execute tests from.
   */
  protected File dir = null;
 
  /**
   * Python class to be used to execute unit tests.  If not set, uses default unit test runner.
   */
  protected String testrunner = "";

  private final static String FAIL_MSG = "Test execution failed.  See output for details.";

  public void execute()
  {
    Project project = getProject();
    project.log("PythonTestTask.execute()", Project.MSG_DEBUG);
    Commandline cmdline = new Commandline();
    cmdline.setExecutable("python");

    makeScript();

        if(this.script != null)
        super.executeScript(project, this.dir, FAIL_MSG);

  }

  /**
   * Create the Python script that will execute the unit tests and set
   * into the script attribute.
   */
  private void makeScript()
  {
    StringBuffer script = new StringBuffer();
        String modulesArg = getModulesArg(getProject());
       
    script.append("import unittest\n");
        if(modulesArg != null)
        {
            getProject().log("Non-null modules arg. creating script");
            if (! testrunner.equals(""))
            {
                int lastDotIndex = testrunner.lastIndexOf('.');
                String testrunnerModule = lastDotIndex >= 0 ?
                        testrunner.substring(0, testrunner.lastIndexOf('.')) :
                            "";
                        script.append("import " + testrunnerModule + "\n");
            }
            script.append("unittest.main(module=None, defaultTest=None, argv=[" +
                    "'unittest.main'," + modulesArg + "]");
            if (! testrunner.equals(""))
            {
                script.append(", testRunner=" + testrunner + "()");
            }
            script.append(")\n");
            this.script = script.toString();
           
            getProject().log("Script:\n" + script);
        }
        else
        {
            getProject().log("Modules arg is null. not creating script");
            this.script = null;
        }
  }
 
  /**
   *
   * @param project
   */
  protected String getModulesArg(Project project) {
    StringBuffer modulesArg = new StringBuffer();
    boolean isFirst = true;
    Iterator fsIter = filesets.iterator();
    while(fsIter.hasNext())
    {
      FileSet fs = (FileSet)fsIter.next();
      DirectoryScanner ds = fs.getDirectoryScanner(project);
      String[] files = ds.getIncludedFiles();

      for(int i = 0; i < files.length; i++)
      {
        project.log(files[i]);
        if(files[i].endsWith(".py"))
        {
          String fileName = files[i].substring(0, files[i].length()-3);
          if (!this.packagedtests) {
            fileName = fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1);
          }
         
          if(isFirst)
          {
            modulesArg.append("'" + fileName + "'");
            isFirst = false;
          }
          else {
         
            modulesArg.append(",'" + fileName + "'");
          }
         }
      }
    }

    String retVal = modulesArg.toString();

        if("".equals(retVal.trim()))
            return null;
       
    retVal = retVal.replace('\\', '.');
    retVal = retVal.replace('/', '.');
    project.log("Modules: " + retVal);
    return (retVal);
  }

  protected boolean packagedtests = true;

  public void setPackagedtests(boolean t) {
    this.packagedtests = t;
  }

  public void addFileSet(FileSet fs)
  {
    this.filesets.add(fs);
  }

  public void setDir(File dir)
  {
    this.dir = dir;
  }

  public void setTestRunner(String s)
  {
    this.testrunner = s;
  }
}
TOP

Related Classes of org.pyant.tasks.PythonTestTask

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.