Jack
Jack

Reputation: 16718

Executing a task dependency for test failure in SBT

The following piece of code, which resides in my build.sbt, executes if my tests complete successfully. It plays a nice little tune to tell me that my tests completed successfully, freeing me from staring the terminal down like Dirty Harry.

test <<= (test in Test) map { result =>
  import java.io.File
  import javax.sound.sampled._
  val clip = AudioSystem.getClip();
  val soundfile = new File("success.wav")
  val inputStream = AudioSystem.getAudioInputStream(soundfile);
  clip.open(inputStream);
  clip.start();
  result
}

So I start my tests with ~test and off it goes. Every-time I hit save in the editor, the tests auto-magically re-runs and if they pass, you here the jingle.

Question is: how can I go about playing a sound if the tests fail? Currently it just keeps quiet on failure.

Upvotes: 1

Views: 483

Answers (1)

Mark Harrah
Mark Harrah

Reputation: 7019

See Handling Failure. For example,

... test in Test mapR {
  case Inc(inc: Incomplete) =>
     ... play failure sound ...
     throw inc
  case Value(v) =>
     ... play success sound ...
     v
}

Upvotes: 1

Related Questions