Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

Archive for April, 2010

Gradle Cobertura Integration Revisited

Posted by Piotr Gabryanczyk on April 17, 2010

Problem

I’d like to run cobertura coverage report using gradle in a non intrusive way similar to maven.
I just want to do this:

gradle cobertura


and in my script:

apply from: 'cobertura.gradle'

Current solution

There is a great solution to this problem here. It works, but is not as unintrusive and not as easy to use as I would like it to be.

My solution

This works for Gradle version 0.9-preview-1.

cobertura.gradle:

logger.info "Configuring Cobertura Plugin"

configurations{
  coberturaRuntime {extendsFrom testRuntime}
}

dependencies {
  coberturaRuntime 'net.sourceforge.cobertura:cobertura:1.9.3'
}

def serFile="${project.buildDir}/cobertura.ser"
def classes="${project.sourceSets.main.classesDir}"
def classesCopy="${classes}-copy"


task cobertura(type: Test){
  dependencies {
    testRuntime 'net.sourceforge.cobertura:cobertura:1.9.3'
  }  

  systemProperties["net.sourceforge.cobertura.datafile"] = serFile
}

cobertura.doFirst  {
  logger.quiet "Instrumenting classes for Cobertura"
  ant {
    delete(file:serFile, failonerror:false)
    delete(dir: classesCopy, failonerror:false)    
    copy(todir: classesCopy) { fileset(dir: classes) }

    taskdef(resource:'tasks.properties', classpath: configurations.coberturaRuntime.asPath)
    'cobertura-instrument'(datafile: serFile) {
      fileset(dir: classes,
              includes:"**/*.class",
              excludes:"**/*Test.class")
    }
  }
}

cobertura.doLast{
  if (new File(classesCopy).exists()) {
    ant.'cobertura-report'(destdir:"${project.reportsDir}/cobertura",
            format:'html', srcdir:"src/main/java", datafile: serFile)

    ant.delete(file: classes)
    ant.move(file: classesCopy, tofile: classes)
  }
}


And then your build.gradle could look like that:

apply plugin: 'java'
apply from: 'cobertura.gradle'
...

Posted in Uncategorized | Tagged: , , | 4 Comments »

How to run a system command in Erlang?

Posted by Piotr Gabryanczyk on April 2, 2010

Problem

I would like to be able to run ie. ls -al / from erlang and process the results using erlang. ie.

Files = cmd:run("ls -al /").

Solution

-module(cmd).
-export([run/1, run/2, test/0]).

run(Cmd) -> 
	run(Cmd, 5000).
	
run(Cmd, Timeout) ->
	Port = erlang:open_port({spawn, Cmd},[exit_status]),
	loop(Port,[], Timeout).
	
loop(Port, Data, Timeout) ->
	receive
		{Port, {data, NewData}} -> loop(Port, Data++NewData, Timeout);
		{Port, {exit_status, 0}} -> Data;
		{Port, {exit_status, S}} -> throw({commandfailed, S})
	after Timeout ->
		throw(timeout)
	end.

test() -> 
	shouldReturnCommandResult(),
	shouldThrowAfterTimeout(),
	shouldThrowIfCmdFailed(),
	{ok, "Tests PASSED"}.

shouldReturnCommandResult() ->
	"Hello\n" = run("echo Hello").

shouldThrowAfterTimeout()->
	timeout = (catch run("sleep 10", 20)).
	
shouldThrowIfCmdFailed()->
		{commandfailed, _} = (catch run("wrongcommand")),
		{commandfailed, _} = (catch run("ls nonexistingfile")).

Posted in Uncategorized | Tagged: , , | 8 Comments »