Running Ruby tests (Rspec and Cucumber) through Ant - ruby

I'm currently looking at TeamCity and how to get our Ruby tests running. I can run the tests just fine when using the Command line or Rake builders. The question I'm trying to solve right now is two fold:
In one of my previous jobs, we also relied on TeamCity to run our .NET tests. We used Nant for this and we had means of tracking the amount of queries that were ran during tests as well as the average execution time for these queries.
I'm trying to do the same right now with my Ruby project. So the first logical step I want to tackle is, How do I run for example RSpec or Cucumber tests using Ant?
I tried looking at Ant itself and grasp it a little bit, but all the examples I find are for jRuby, which we don't use. We rely on RVM and a normal Ruby installation.
The second part of the question is, How can I track the amount of queries ran and their execution time? I'm guessing there is probably a gem for it or some sort of global variable to track. Would love to output this information back to TeamCity somehow.
EDIT
Ok, so I managed to get things running with Ant for my TeamCity server.
This is the XML i'm using atm:
<?xml version="1.0"?>
<project name="rubycas" default="init">
<description>
This buildfile is used to build the RubyCAS project under TeamCity and run the required tasks to validated
whether the project is stable and fully functional.
</description>
<property name="test_type" value="cucumber" />
<target name="init">
<echo message="##teamcity[testStarted name='Rubycas']" />
<condition property="cucumberBool">
<equals arg1="${test_type}" arg2="cucumber" />
</condition>
<condition property="rspecBool">
<equals arg1="${test_type}" arg2="rspec" />
</condition>
</target>
<target name="rspec" if="rspecBool" depends="init">
<exec executable="rspec" outputproperty="result">
<arg value="--require teamcity/spec/runner/formatter/teamcity/formatter" /> <arg value="--format Spec::Runner::Formatter::TeamcityFormatter" />
</exec>
<echo message="${result}" />
</target>
<target name="cucumber" if="cucumberBool" depends="init">
<exec executable="cucumber" outputproperty="result">
<arg value="--format junit" />
<arg value="--out results" />
<arg value="features" />
</exec>
<echo message="${result}" />
</target>
</project>
Problem now is, I cannot get the output from RSpec into TeamCity to recognize the tests.

You can use ant's exec task to run arbitrary system calls, which in your case might be rspec:
https://ant.apache.org/manual/Tasks/exec.html
Something along the lines of
<target name="rspec">
<exec executable="rake">
<arg value="spec"/>
</exec>
</target>
I don't know if your tracking stuff will work with this though, because it's really executing the commands outside of ant.

Related

How to run Maven command in ant script?

I am trying to create an ant task to execute a maven command, but I am getting error while running the ant task
<target name="Junit">
<exec dir="./MServer/BuildServer/Workspc/CustMgmt" executable="cmd">
<arg value="/C"/>
<arg value="E:\EOM Setup\maven-3.3.9\bin\mvn.bat"/>
<arg value="test" />
</exec>
</target>
While am running this, am getting an error:
'E:\EOM' is not recognized as internal or external command, operable program or batch file
(I am running on Windows 7)
You can get around this issue by using Ant's property task with the location attribute (as opposed to the more common value attribute). This will store the value as a properly formatted path. In addition, you can use this to reference mvn.bat instead of typing out the entire path every time.
<target name="Junit">
<property name="mvn.executable" location="E:\EOM Setup\maven-3.3.9\bin\mvn.bat" />
<exec dir="./MServer/BuildServer/Workspc/CustMgmt" executable="cmd">
<arg value="/C"/>
<arg value="${mvn.executable}"/>
<arg value="test" />
</exec>
</target>

How to use skipTests option when using mvn-ant-tasks in ant

I am using mvn-ant-task in a build file to clean and install the Maven projects.
Below is the configuration I did
<project name="maven-project"
default="default"
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
and in the target, I have added something like this
<target name="CleanInstall">
<echo message="Building the project using maven and skipping tests if any in ${RepoFolder}" />
<artifact:mvn pom="${GIT_REPO_HOME}\${RepoFolder}\pom.xml">
<arg value="clean:clean" />
</artifact:mvn>
<artifact:mvn pom="${GIT_REPO_HOME}\${RepoFolder}\pom.xml">
<arg value="install:install -DskipTests=true" />
</artifact:mvn>
</target>
When I run this build, clean is processing properly, but I am unable to send the arguments skipTests in the install plugin.
Is there any other way to execute the Maven commands from Ant? I also tried
<exec command="mvn install -DskipTests=true" dir=""${GIT_REPO_HOME}\${RepoFolder}\pom.xml"/>
Its failing with error given below
C:\Users\sharath\Desktop\devsetup\build.xml:102: Execute failed:
java.io.IOException: Cannot run program "mvn" (in directory
"D:\sbhaskara\GIT\connectmodel"): CreateProcess error=2, The system
cannot find the file specified
You are not building the argument for the task correctly, it should be:
<target name="CleanInstall">
<echo message="Building the project using maven and skipping tests if any in ${RepoFolder}" />
<artifact:mvn pom="${GIT_REPO_HOME}\${RepoFolder}\pom.xml" >
<arg value="-Dmaven.test.skip=true" />
<arg value="clean" />
<arg value="install" />
</artifact:mvn>
</target>
i.e. each argument must be in its own <arg> tag. Note that I collapsed all the calls to Maven inside a single task.

Calling a rakefile from NAnt

I am dealing with a relatively large project, and up until this point building has been done in a NAnt build script. Recently a small portion of development was done in ruby, and is built using a rake file. I would like to create a NAnt target that will call the rakefile. Current set-up in the NAnt build: (Note that the rakefile is in a different directory from the NAnt script)
<property overwrite="false" name="project.rootdirectory" value="${project::get-base-directory()}" />
<property overwrite="false" name="rake.exe" value="rake.bat" />
<target name="callrake">
<exec program="${rake.exe}" verbose="true">
<arg value="build:foo" />
<arg line="-f ${project.rootdirectory}/../pathtorakefile" />
</exec>
</target>
The error that I recieve when I run nant callrake is:
[exec] ruby.exe: No such file or directory -- c:/pathtoNAntscript/rake (LoadError)
c:/pathtoNAntscript/NAntscript.build
External Program Failed: rake.bat (return code was 1)
Ruby and rake are installed, and the Ruby bin is in the path variable. Not sure why NAnt cant seem to call rake.
This issue is solved by calling cmd.exe and passing it the rake command as text:
<exec program="cmd.exe" verbose="true">
<arg line="/c ${rake.exe} build:foo" />
<arg line="-f ${project.rootdirectory}/../pathtorakefile/Rakefile.rb" />
</exec>

PHP Mess Detector integration with Teamcity

I have Teamcity 7.0.3 installed with different projects. I want to use PHP Mess Detector. I want to have limited set of rules from PHP Mess Detector. And different set of rules for every project. So my questions are
1: How can I modify default rules of PHP Mess Detector?
2: How can I have different set of rules defined against each project in Teamcity?
My current build file content is :
<?xml version="1.0" encoding="UTF-8"?>
<project name="bnnpoa" default="analyse" basedir=".">
<property name="project-name" value="${ant.project.name}" />
<property name="work-dir" value="%system.teamcity.build.workingDir%" />
<property name="folder-to-check" value="${work-dir}\sites" />
<target name="analyse">
<exec executable="C:\php\PEAR\scripts\phpmd.bat">
<arg value="${folder-to-check}"/>
<arg value="html"/>
<arg value="C:\php\PEAR\resources\rulesets\naming.xml,C:\php\PEAR\resources\rulesets\codesize.xml,C:\php\PEAR\resources\rulesets\controversial.xml"/>
<arg value=">"/>
<arg value="${project-name}_analysis.htm"/>
</exec>
</target>
</project>
Add a build step and use same code for every project.
To define your own rules you can modify XML files or copy them , modify rules as per needs, place files in same path and other option is to create new XML and import required rules from files.

Ant 1.8.0 low performance

New Ant 1.8.0 (release Feb 1) introduces some cool features, so I tried my build/deployment scripts with new Ant.
I was surprised that execution time becomes in 10-30 times slower for some targets! Below simple example with Exec task, although I got performance problems with other task types too.
<target name="create_backup_impl" if="db.make_backup" >
<echo message="Backup is starting.." />
<exec executable="${db.dump_executable}"
output="${db.backup_file}"
failonerror="true">
<arg value="-h${db.host}" />
<arg value="-u${db.userid}" />
<arg value="-p${db.password}" />
<arg value="${db.backup_options}" />
<arg value="${db.name}" />
</exec>
<echo message="Backup completed!" />
</target>
It is a target for backuping database (backup size ~100 Mb). Ant 1.7.1 works about 30 s, Ant 1.8.0 - 15 min. I tried several times, effect is stable. Processor loading is very low for Ant 1.8, and near 50% for old one. Looks like problem with priority of process or slow IO operations. Any ideas?
I'm seeing similar performance degradation when using
<apply executable="...">
...
</apply>
Looks like this performance degradation was caused by a 9 year old bug fix in ANT 1.8.0 found here: https://issues.apache.org/bugzilla/show_bug.cgi?id=5003 (See comment #29).
Performance slightly improves in ANT 1.8.1 but is still worse than ANT 1.7.1: https://issues.apache.org/bugzilla/show_bug.cgi?id=48734 (See comment #2).
I would suggest filing a bug report directly with Ant team. Their response time is usually very good.
http://ant.apache.org/bugs.html

Resources