1

What is the correct way of passing in an environment variable into SBT so that it can be accessed using Specs2? (And then retrieving the value in Specs2.) The environment variable will contain an API key to use for testing.

It needs to be an environment variable to work with Travis CI's encrypted environment variable functionality[1]

My setup:

  1. SBT 0.13.0
  2. Specs2 2.3.4
  3. Travis CI

Edit: bonus points if somebody can link to an open-source repo that does this. There must be a few!

[1] Using secret api keys on travis-ci

Community
  • 1
  • 1
Alex Dean
  • 15,575
  • 13
  • 63
  • 74

2 Answers2

1

I guess that you can encrypt your key with the travis api and get:

xxxEncryptedxxx

Then you can use the CommandLineArguments trait to pass arguments from the command-line in SBT to your specification.

In .travis.yml

sbt ++$TRAVIS_SCALA_VERSION testOnly *MySpec* -- key xxxEncryptedxxx

In MySpec.scala

class MySpec extends mutable.Specification with CommandLineArguments {
  "this is an API test" >> {
    arguments.commandLine.value("key").map { k =>
      callApi(k) must beOk
    }.getOrElse(ko("you need to pass a key on the command line"))
  }
}
Eric
  • 15,494
  • 38
  • 61
  • Hi Eric! Thanks for this. Have you seen an open-source repo that does it this way - or could you link to some Travis documentation showing this? I have re-checked and I can still only find Travis documentation which details encrypting **environment variables** (i.e. $KEY=blah). – Alex Dean Dec 25 '13 at 05:35
  • No sorry, it's more a speculation than an actual experiment, I've never tried to do that myself and don't know any project doing that. – Eric Dec 26 '13 at 00:50
0

From you questions, I presume you're looking to pass secure environment variables using Travis's built-in support for encryption?

If so the environment variable is set before SBT is run, so it should be available to all processes. I don't use Specs, but the standard JVM way to get environment variable is to use System.getenv(String). It's possible that sbt deletes the environment variables before running Specs; if that's true then fixing that has to be done in your build.sbt somehow, and isn't specific to Travis.

Christopher Currie
  • 3,025
  • 1
  • 29
  • 40