Cui Pengfei 崔鹏飞
Cui Pengfei 崔鹏飞

Reputation: 8305

How to run a custom task(functional tests written in protractor) while the "run" task is up and running?

I am working on a web app that's built atop Play Framework.

The goal I want to achieve is to create a custom sbt task that works like this:

  1. start the play application
  2. run my custom task (functional tests written in javascript that depends on the running application)
  3. stop the application after my custom task is done

Now I am stuck on step two.

I have this sbt script that's working:

lazy val anotherTask = taskKey[Unit]("run this first")
lazy val myCustomTask = taskKey[Unit]("try to run shell in sbt")

anotherTask := {
  println("i am the first task")
}

myCustomTask := {
  println("try to run shell")
  import scala.sys.process._
  println("git status" !!)
  println("the shell command worked, yeah!")
}

myCustomTask <<= myCustomTask.dependsOn(anotherTask)

But if I try to make myCustomTask depend on the run task (which starts the play app) by modifying the script like this:

myCustomTask <<= myCustomTask.dependsOn(runTask _)

I get the following error:

error: type mismatch; found : (sbt.Configuration, String, Seq[String]) => sbt.Def.Initialize[sbt.Task[Unit]] required: sbt.Scoped.AnyInitTask (which expands to) sbt.Def.Initialize[sbt.Task[T]] forSome { type T }

How should I solve this problem?

At last, I ended up with a specs2 class like this:

  "my app" should {

    "pass the protractor tests" in {
      running(TestServer(9000)) {

        Await.result(WS.url("http://localhost:9000").get, 2 seconds).status === 200
        startProtractor(getProcessIO) === 0
      }
    }

  }


  private def startProtractor(processIO: ProcessIO): Int = {
    Process("protractor", Seq( """functional-test/config/buildspike.conf.js"""))
      .run(processIO)
      .exitValue()
  }

  private def getProcessIO: ProcessIO = {
    new ProcessIO(_ => (),
      stdout => fromInputStream(stdout).getLines().foreach(println),
      _ => ())
  }

Upvotes: 2

Views: 887

Answers (2)

lpiepiora
lpiepiora

Reputation: 13749

Run is an Input Task, if you want to use it in conjunction with a normal task, you have to convert it to a task first.

You can get a task from an input task by using toTask method as it is described in the documentation.

myCustomTask <<= myCustomTask.dependsOn((run in Compile).toTask(""))

Upvotes: 4

Jacek Laskowski
Jacek Laskowski

Reputation: 74709

I would use the following to depend on the run task:

myCustomTask <<= myCustomTask dependsOn run

While changing the build I would also use sbt.Process API to execute git status.

"git version".!

It's supposed to be working fine(r) with the changes.

Upvotes: 0

Related Questions