ITV / scala-pact

A Scala implementation of CDC using the Pact standard
Other
108 stars 54 forks source link

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

Open solarmosaic-kflorence opened 3 years ago

solarmosaic-kflorence commented 3 years ago

Currently ScalaPactEnv sets outputPath to None by default:

In various places, a default value of "target/pacts" is then assigned. For example: https://github.com/ITV/scala-pact/blob/7e1ef1244ad584e906b115330d5514000cf7f812/scalapact-shared/src/main/scala/com/itv/scalapact/shared/ScalaPactSettings.scala#L25

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.

solarmosaic-kflorence commented 3 years ago

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...

solarmosaic-kflorence commented 3 years ago

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:

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.

solarmosaic-kflorence commented 3 years ago

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 https://github.com/ITV/scala-pact/blob/c4554a40a2e2150e9ef0f703f9b3dba765bac97c/scalapact-scalatest/src/main/scala/com/itv/scalapact/ScalaPactContractWriter.scala#L35

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

solarmosaic-kflorence commented 3 years ago

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.