ITV/scala-pact

Pact file output folders differ between SBT and tests for sub-projects

Opened this issue · 4 comments

Currently ScalaPactEnv sets outputPath to None by default:

In various places, a default value of "target/pacts" is then assigned. For example:

val giveOutputPath: String = outputPath.getOrElse(Properties.envOrElse("pact.rootDir", "target/pacts"))

This results in the project root target/pacts folder being used by default, even for sub-projects. This means by default, pact commands will fail on sub-projects. I would suggest instead, we use a default of (target.value / "pacts").getPath as provided by SBT, for example:

scalaPactEnv := ScalaPactEnv.empty.withOutputPath((target.value / "pacts").getPath),

This way the default target path will work on sub-projects by default. This means we should probably also update the logic so that other places in the code will expect the outputPath to always be defined and used as a fallback to Properties.envOrElse("pact.rootDir") so that defining the system override will still take precedence.

As a side note, when running tests in a sub-project the files are already written to the correct place (sub-project target/pacts by default).

EDIT: actually, when I run the tests inside of intelliJ they seem to write to the root project target/pacts folder, but when I run them from the command line they write to the project target/pacts folder...

I guess I'm less concerned about the default path, as long as it is consistent for both tests and SBT tasks. To clarify I am currently seeing the following behavior:

  • when sbt test is run from command line, pact files are output to the sub-project/target/pacts directory
  • when the same tests are run from within IntelliJ, pact files are output to the root project target/pacts directory
  • when SBT tasks like sbt pactPack are run, they are currently always looking in the root project target/pacts directory

So it seems like the current oddball is the output location of sub-project tests. Strangely, when I debug the code, file.getAbsolutePath points to the root project target/pacts directory, but when file.createNewFile() is called, it creates the file in sub-project/target/pacts... must have something to do with the workspace Java is using.

Oddly enough, this works:

  val file1 = new File("target/pacts")
  println(file1.getAbsolutePath)
  val file2 = new File(file1.getAbsolutePath + "/" + UUID.randomUUID())
  file2.createNewFile()
  println(file2.getAbsolutePath)

I think updating

To use an absolute path instead of a relative path will fix the issue.

For now I can use this workaround with ScalaPactForgerDsl:

  implicit override val options: ScalaPactOptions = ScalaPactOptions(
    writePactFiles = true,
    outputPath = new File("target/pacts").getAbsolutePath
  )

Will open a PR for this tomorrow.