简体   繁体   中英

How can I get the build output directory inside tests run by sbt?

I need to create directories and files for some tests. My project uses sbt as the build tool, and common practice is to use File.createTempFile or similar APIs, but I abhor that practice. I want all files created by my tests to reside somewhere inside the output directory ( <module>/target/ ), so that they'll be removed when I run clean , but otherwise preserved if I have need of them to figure out test failures.

The test framework is not relevant: if your solution requires a particular framework, I'll happily adopt it or figure out how it does the trick and use that.

In short, I need the answer to one of these two questions:

  • How can I create a file inside the build output directory from a test run by sbt?
  • How can I find out what is the build output directory for the current project from a test run by sbt?

In ScalaTest, try passing target

settingKey[File]("Main directory for files generated by the build.")

to config map as -Dkey=value . For example, in build.sbt specify

Test / testOptions += Tests.Argument(s"-DtargetDir=${target.value}")

and then define test like so

import org.scalatest._

class ExampleSpec extends fixture.FlatSpec with fixture.ConfigMapFixture with Matchers {
  "The config map" should "contain target directory used by sbt" in { configMap =>
    configMap should contain key "targetDir"
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM