From 7d7fa63c26bd2b7ebf1f42c6ba09c49f52e58b2a Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 20 Aug 2004 10:55:20 +0000 Subject: [PATCH] git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276793 13f79535-47bb-0310-9956-ffa450edef68 --- proposal/sandbox/dotnet/README | 2 - proposal/sandbox/dotnet/docs/index.html | 4 + proposal/sandbox/dotnet/docs/nunit.html | 177 +++++++++ .../dotnet/src/etc/testcases/nunit.xml | 81 +++++ .../dotnet/src/etc/testcases/src/fail.cs | 30 ++ .../dotnet/src/etc/testcases/src/pass.cs | 30 ++ .../taskdefs/optional/dotnet/NUnitTask.java | 335 ++++++++++++++++++ .../ant/taskdefs/optional/dotnet/antlib.xml | 4 + .../optional/dotnet/NUnitTaskTest.java | 79 +++++ 9 files changed, 740 insertions(+), 2 deletions(-) create mode 100644 proposal/sandbox/dotnet/docs/nunit.html create mode 100644 proposal/sandbox/dotnet/src/etc/testcases/nunit.xml create mode 100644 proposal/sandbox/dotnet/src/etc/testcases/src/fail.cs create mode 100644 proposal/sandbox/dotnet/src/etc/testcases/src/pass.cs create mode 100644 proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTask.java create mode 100644 proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTaskTest.java diff --git a/proposal/sandbox/dotnet/README b/proposal/sandbox/dotnet/README index f026d4bc2..70bc73d2d 100644 --- a/proposal/sandbox/dotnet/README +++ b/proposal/sandbox/dotnet/README @@ -32,8 +32,6 @@ Short term goals are: * A task. -Mid term goals: - * A task. Those tasks should end up in an antlib of their own in order to be diff --git a/proposal/sandbox/dotnet/docs/index.html b/proposal/sandbox/dotnet/docs/index.html index 882be4cea..d5fee8b4c 100644 --- a/proposal/sandbox/dotnet/docs/index.html +++ b/proposal/sandbox/dotnet/docs/index.html @@ -112,6 +112,10 @@ tool, untested.
  • wix - execute the WiX toolset, untested.
  • + +
  • nunit - execute the + nunit-console.exe NUnit + test runner.

  • diff --git a/proposal/sandbox/dotnet/docs/nunit.html b/proposal/sandbox/dotnet/docs/nunit.html new file mode 100644 index 000000000..e7ddd11f2 --- /dev/null +++ b/proposal/sandbox/dotnet/docs/nunit.html @@ -0,0 +1,177 @@ + + + + NUnit Task + + + +

    NUnit

    + +

    Description

    + +

    Runs the NUnit console + test runner.

    + +

    Parameters

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AttributeDescriptionRequired
    vmSame as dotnetexec's vm attribute. + Specify the framework to use.No.
    configConfig file to useNo.
    outputWhere test output should go.No.
    errorWhere test error output should go.No.
    xmloutWhere NUnit's XML output should go.No.
    transformThe transformation to apply.No.
    threadCauses a separate thread to be created for + running the tests - see the NUnit documentation for + details.No - defaults to false.
    noshadowDisables shadow copying of the assembly in + order to provide improved performance..No - defaults to false.
    fixtureTest fixture to run.No.
    labelsCauses an identifying label to be displayed + at the start of each test case.No - defaults to false.
    failOnErrorStops the build if NUnit returns with a code + indicating an error or failure.No - defaults to false.
    + +

    Parameters specified as nested elements

    +

    testassembly

    + +

    testassembly has a single required attribute name - + specifies an assembly or a project file holding tests.

    + +

    include/exclude

    + +

    in/exclude have a single required attribute name - + specifies a category to include or exclude from the tests.

    + +

    Note that in current versions of NUnit (up to 2.2) you + may choose to either include or exclude categories in a run, but + not both. This task does not enforce this restriction + since future versions of NUnit may change behavior.

    + +

    redirector

    + +

    A nested I/O + Redirector can be specified. Any file mapping is done using a + null sourcefile; therefore not all Mapper + types will return results.

    + +

    env

    It is possible to specify environment variables to + pass to the system command via nested <env> + elements. They support the same attributes as the nested env + element of the <exec> task.

    + +

    Examples

    + +

    Specify an assembly or project:

    + +
    +      <nunit>
    +        <testassembly name="nunit.tests.dll"/>
    +      </nunit>
    +    
    + +

    or

    + +
    +      <nunit>
    +        <testassembly name="nunit.tests.csproj"/>
    +      </nunit>
    +    
    + +

    Specifying an Assembly and a Fixture

    + +
    +      <nunit fixture="NUnit.Tests.AssertionTests">
    +        <testassembly name="nunit.tests.dll"/>
    +      </nunit>
    +    
    + +

    Specifying Test Categories to Include

    + +
    +      <nunit>
    +        <testassembly name="nunit.tests.dll"/>
    +        <include name="Baseline"/>
    +      </nunit>
    +    
    + +

    Specifying the XML file name

    + +
    +      <nunit xmlout="console-test.xml">
    +        <testassembly name="nunit.tests.dll"/>
    +      </nunit>
    +    
    + +

    changes the name of the output file to "console-test.xml" - + note that this task will resolve file names relative to the + project's base directory, not the current working directory.

    + +

    Specifying Multiple Assemblies

    + +
    +      <nunit>
    +        <testassembly name="assembly1.dll"/>
    +        <testassembly name="assembly2.dll"/>
    +        <testassembly name="assembly3.dll"/>
    +      </nunit>
    +    
    + +
    +

    Copyright © 2004 The Apache Software Foundation. All rights Reserved.

    + + \ No newline at end of file diff --git a/proposal/sandbox/dotnet/src/etc/testcases/nunit.xml b/proposal/sandbox/dotnet/src/etc/testcases/nunit.xml new file mode 100644 index 000000000..ded9b586a --- /dev/null +++ b/proposal/sandbox/dotnet/src/etc/testcases/nunit.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/proposal/sandbox/dotnet/src/etc/testcases/src/fail.cs b/proposal/sandbox/dotnet/src/etc/testcases/src/fail.cs new file mode 100644 index 000000000..44c1205b4 --- /dev/null +++ b/proposal/sandbox/dotnet/src/etc/testcases/src/fail.cs @@ -0,0 +1,30 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +using System; +using NUnit.Framework; + +[TestFixture] +public class FailingTest +{ + + [Test] + public void Fail() + { + Assert.IsTrue(false); + } +} diff --git a/proposal/sandbox/dotnet/src/etc/testcases/src/pass.cs b/proposal/sandbox/dotnet/src/etc/testcases/src/pass.cs new file mode 100644 index 000000000..51fabf40e --- /dev/null +++ b/proposal/sandbox/dotnet/src/etc/testcases/src/pass.cs @@ -0,0 +1,30 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +using System; +using NUnit.Framework; + +[TestFixture] +public class PassingTest +{ + + [Test] + public void Pass() + { + Assert.IsTrue(true); + } +} diff --git a/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTask.java b/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTask.java new file mode 100644 index 000000000..97cf39588 --- /dev/null +++ b/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTask.java @@ -0,0 +1,335 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.taskdefs.optional.dotnet; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.Environment; +import org.apache.tools.ant.types.RedirectorElement; + +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * Task to run the NUnit Console test runner. + * + * @see http://www.nunit.org/ + */ +public class NUnitTask extends Task { + + /** + * The vm attribute - if given. + */ + private String vm; + + /** + * Test assemblies. + */ + private ArrayList testAssemblies = new ArrayList(); + + /** + * The /config argument. + */ + private File configFile; + + /** + * The /output argument. + */ + private File out; + + /** + * The /err argument. + */ + private File err; + + /** + * The /xml argument. + */ + private File xmlOut; + + /** + * The /transform argument. + */ + private File transform; + + /** + * The /thread argument. + */ + private boolean thread = false; + + /** + * The /fixture argument. + */ + private String fixture; + + /** + * Categories to include. + */ + private ArrayList includes = new ArrayList(); + + /** + * Categories to exclude. + */ + private ArrayList excludes = new ArrayList(); + + /** + * The /noshadow argument. + */ + private boolean noshadow = false; + + /** + * The /labels argument. + */ + private boolean labels = false; + + /** + * Redirects everything that NUnit wants to send to the console. + */ + private RedirectorElement redirectorElement; + + /** + * Whether a failure should stop the build. + */ + private boolean failOnError = false; + + /** + * Support for nested environment variables. + */ + private Environment env = new Environment(); + + public NUnitTask() { + super(); + } + + /** + * Set the name of the executable for the virtual machine. + * + * @param value the name of the executable for the virtual machine + */ + public void setVm(String value) { + this.vm = value; + } + + /** + * Sets the name of the config file. + */ + public void setConfig(File c) { + configFile = c; + } + + /** + * The /output argument. + */ + public void setOut(File out) { + this.out = out; + } + + /** + * The /err argument. + */ + public void setError(File err) { + this.err = err; + } + + /** + * The /xml argument. + */ + public void setXmlOut(File out) { + this.xmlOut = out; + } + + /** + * The /transform argument. + */ + public void setTransform(File transform) { + this.transform = transform; + } + + /** + * The /thread argument. + */ + public void setThread(boolean thread) { + this.thread = thread; + } + + /** + * The /fixture argument. + */ + public void setFixture(String fixture) { + this.fixture = fixture; + } + + /** + * The /noshadow argument. + */ + public void setNoshadow(boolean noshadow) { + this.noshadow = noshadow; + } + + /** + * The /labels argument. + */ + public void setLabels(boolean labels) { + this.labels = labels; + } + + /** + * Whether a failure should stop the build. + */ + public void setFailOnError(boolean b) { + failOnError = b; + } + + /** + * Adds a test assembly by name. + */ + public void addTestAssembly(NamedElement a) { + testAssemblies.add(a); + } + + /** + * Adds a category to the include list. + */ + public void addInclude(NamedElement a) { + includes.add(a); + } + + /** + * Adds a category to the exclude list. + */ + public void addExclude(NamedElement a) { + excludes.add(a); + } + + /** + * Add an environment variable to the launched process. + * + * @param var new environment variable + */ + public void addEnv(Environment.Variable var) { + env.addVariable(var); + } + + /** + * Add a RedirectorElement to this task. + * + *

    This does not use the out and + * error attributes, it only captures NUnits output + * that has not been redirected by those attributes.

    + */ + public void addConfiguredRedirector(RedirectorElement redirectorElement) { + if (this.redirectorElement != null) { + throw new BuildException("cannot have > 1 nested s"); + } else { + this.redirectorElement = redirectorElement; + } + } + + public void execute() { + if (testAssemblies.size() == 0) { + throw new BuildException("You must specify at least one test " + + "assembly."); + } + + DotNetExecTask exec = DotNetExecTask.getTask(this, vm, + "nunit-console.exe", + env); + Iterator iter = testAssemblies.iterator(); + while (iter.hasNext()) { + NamedElement a = (NamedElement) iter.next(); + exec.createArg().setValue(a.getName()); + } + if (configFile != null) { + exec.createArg().setValue("/config=" + + configFile.getAbsolutePath()); + } + exec.createArg().setValue("/nologo"); + + if (out != null) { + exec.createArg().setValue("/output=" + out.getAbsolutePath()); + } + if (err != null) { + exec.createArg().setValue("/err=" + err.getAbsolutePath()); + } + if (xmlOut != null) { + exec.createArg().setValue("/xml=" + xmlOut.getAbsolutePath()); + } + if (transform != null) { + exec.createArg().setValue("/transform=" + + transform.getAbsolutePath()); + } + + if (thread) { + exec.createArg().setValue("/thread"); + } + if (noshadow) { + exec.createArg().setValue("/noshadow"); + } + if (labels) { + exec.createArg().setValue("/labels"); + } + if (fixture != null) { + exec.createArg().setValue("/fixture=" + fixture); + } + + if (includes.size() > 0) { + StringBuffer sb = new StringBuffer("/include="); + iter = includes.iterator(); + boolean first = false; + while (iter.hasNext()) { + if (first) { + first = false; + } else { + sb.append(","); + } + NamedElement a = (NamedElement) iter.next(); + sb.append(a.getName()); + } + exec.createArg().setValue(sb.toString()); + } + if (excludes.size() > 0) { + StringBuffer sb = new StringBuffer("/exclude="); + iter = excludes.iterator(); + boolean first = false; + while (iter.hasNext()) { + if (first) { + first = false; + } else { + sb.append(","); + } + NamedElement a = (NamedElement) iter.next(); + sb.append(a.getName()); + } + exec.createArg().setValue(sb.toString()); + } + + if (redirectorElement != null) { + exec.addConfiguredRedirector(redirectorElement); + } + exec.setFailonerror(failOnError); + + exec.execute(); + } + + public static class NamedElement { + private String name; + public String getName() {return name;} + public void setName(String s) {name = s;} + } +} \ No newline at end of file diff --git a/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/antlib.xml b/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/antlib.xml index cf5bd0c93..ee05e2386 100644 --- a/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/antlib.xml +++ b/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/antlib.xml @@ -31,4 +31,8 @@ name="wix" classname="org.apache.tools.ant.taskdefs.optional.dotnet.WixTask" /> + diff --git a/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTaskTest.java b/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTaskTest.java new file mode 100644 index 000000000..3ccefb4ed --- /dev/null +++ b/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTaskTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant.taskdefs.optional.dotnet; + +import org.apache.tools.ant.BuildFileTest; + +/** + * Tests the NUnitTask task. + */ +public class NUnitTaskTest extends BuildFileTest { + + /** + * Description of the Field + */ + private final static String TASKDEFS_DIR = "src/etc/testcases/"; + + /** + * Constructor + * + * @param name testname + */ + public NUnitTaskTest(String name) { + super(name); + } + + /** + * The JUnit setup method + */ + public void setUp() { + configureProject(TASKDEFS_DIR + "nunit.xml"); + } + + /** + * The teardown method for JUnit + */ + public void tearDown() { + executeTarget("teardown"); + } + + public void testNoAssembly() { + expectSpecificBuildException("no-assembly", "no assembly", + "You must specify at least one test assembly."); + } + + public void testPass() { + if (getProject().getProperty("nunit.found") != null) { + expectLogContaining("passing-test", + "Tests run: 1, Failures: 0, Not run: 0"); + } + } + + public void testFail() { + if (getProject().getProperty("nunit.found") != null) { + expectLogContaining("failing-test", + "Tests run: 1, Failures: 1, Not run: 0"); + } + } + + public void testFailOnFail() { + if (getProject().getProperty("nunit.found") != null) { + expectBuildException("failing-test-with-fail", "test should fail"); + } + } + +}