Skip to content

Commit

Permalink
Merge pull request #18 from hmrc/PLATUI-443-remove-snapshotter-dep
Browse files Browse the repository at this point in the history
Remove dependency on x-frontend-snapshotter, simplify build
  • Loading branch information
matthewmascord committed May 21, 2020
2 parents 8717d4e + 09c8433 commit 5d84e85
Show file tree
Hide file tree
Showing 239 changed files with 1,049 additions and 1,578 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ npm-debug.log
yarn-debug.log
yarn-error.log
.DS_Store


src/main/play-25/twirl/**/*.scala.html
40 changes: 13 additions & 27 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import GenerateFixtures.generateFixtures
import GeneratePlay25Twirl.generatePlay25Templates
import PlayCrossCompilation.{dependencies, playVersion}
import de.heikoseeberger.sbtheader.HeaderKey
import play.sbt.PlayImport.PlayKeys._
import uk.gov.hmrc.playcrosscompilation.PlayVersion.{Play25, Play26}

Expand All @@ -12,6 +12,8 @@ lazy val playDir =

lazy val IntegrationTest = config("it") extend Test

lazy val hmrcFrontendVersion = "1.12.0"

lazy val root = Project(libName, file("."))
.enablePlugins(PlayScala, SbtAutoBuildPlugin, SbtGitVersioning, SbtTwirl, SbtArtifactory)
.disablePlugins(PlayLayoutPlugin)
Expand All @@ -32,16 +34,12 @@ lazy val root = Project(libName, file("."))
TwirlKeys.templateImports := templateImports,
PlayCrossCompilation.playCrossCompilationSettings,
makePublicallyAvailableOnBintray := true,
unmanagedSourceDirectories in Compile += baseDirectory.value / "src/main/twirl",
unmanagedSourceDirectories in Test += baseDirectory.value / "src/test/twirl",
(sourceDirectories in (Compile, TwirlKeys.compileTemplates)) +=
baseDirectory.value / "src" / "main" / playDir / "twirl",
(sourceDirectories in (Test, TwirlKeys.compileTemplates)) +=
baseDirectory.value / "src" / "test" / playDir / "twirl",
(generatePlay25TemplatesTask in Compile) := {
(generatePlay25TemplatesTask) := {
val cachedFun: Set[File] => Set[File] =
FileFunction.cached(
cacheBaseDirectory = streams.value.cacheDirectory / "compile-generate-play-25-templates-task",
cacheBaseDirectory = streams.value.cacheDirectory / "generate-play-25-templates-task",
inStyle = FilesInfo.lastModified,
outStyle = FilesInfo.exists) { (in: Set[File]) =>
println("Generating Play 2.5 templates")
Expand All @@ -52,32 +50,19 @@ lazy val root = Project(libName, file("."))
val play26Templates: Set[File] = (play26TemplatesDir ** ("*.scala.html")).get.toSet
cachedFun(play26Templates).toSeq
},
// FIXME: refactor to remove task implementation duplication
(generatePlay25TemplatesTask in Test) := {
val cachedFun: Set[File] => Set[File] =
FileFunction.cached(
cacheBaseDirectory = streams.value.cacheDirectory / "test-generate-play-25-templates-task",
inStyle = FilesInfo.lastModified,
outStyle = FilesInfo.exists) { (in: Set[File]) =>
println("Generating Play 2.5 test templates")
generatePlay25Templates(in)
}

val play26TemplatesDir = baseDirectory.value / "src/test/play-26/twirl"
val play26Templates: Set[File] = (play26TemplatesDir ** ("*.scala.html")).get.toSet
cachedFun(play26Templates).toSeq
(generateUnitTestFixtures in Test) := {
generateFixtures(baseDirectory.value / "src/test/resources", hmrcFrontendVersion)
},
(TwirlKeys.compileTemplates in Compile) :=
((TwirlKeys.compileTemplates in Compile) dependsOn (generatePlay25TemplatesTask in Compile)).value,
((TwirlKeys.compileTemplates in Compile) dependsOn (generatePlay25TemplatesTask)).value,
(TwirlKeys.compileTemplates in Test) :=
((TwirlKeys.compileTemplates in Test) dependsOn (generatePlay25TemplatesTask in Test)).value,
((TwirlKeys.compileTemplates in Test) dependsOn (generatePlay25TemplatesTask)).value,
parallelExecution in sbt.Test := false,
playMonitoredFiles ++= (sourceDirectories in (Compile, TwirlKeys.compileTemplates)).value,
routesGenerator := {
if (playVersion == Play25) StaticRoutesGenerator
else InjectedRoutesGenerator
},
HeaderKey.excludes += "fixtures/*/*/*.html",
unmanagedResourceDirectories in Test ++= Seq(baseDirectory(_ / "target/web/public/test").value),
buildInfoKeys ++= Seq[BuildInfoKey](
"playVersion" -> PlayCrossCompilation.playVersion,
Expand All @@ -99,7 +84,7 @@ lazy val libDependencies: Seq[ModuleID] = dependencies(
"com.typesafe.play" %% "play" % playRevision,
"com.typesafe.play" %% "filters-helpers" % playRevision,
"org.joda" % "joda-convert" % "2.0.2",
"org.webjars.npm" % "hmrc-frontend" % "1.12.0"
"org.webjars.npm" % "hmrc-frontend" % hmrcFrontendVersion
)

val test = Seq(
Expand All @@ -119,7 +104,7 @@ lazy val libDependencies: Seq[ModuleID] = dependencies(
},
play25 = {
val compile = Seq(
"uk.gov.hmrc" %% "play-frontend-govuk" % "0.44.0-play-25"
"uk.gov.hmrc" %% "play-frontend-govuk" % "0.45.0-play-25"
)

val test = Seq(
Expand All @@ -130,7 +115,7 @@ lazy val libDependencies: Seq[ModuleID] = dependencies(
},
play26 = {
val compile = Seq(
"uk.gov.hmrc" %% "play-frontend-govuk" % "0.44.0-play-26"
"uk.gov.hmrc" %% "play-frontend-govuk" % "0.45.0-play-26"
)

val test = Seq(
Expand Down Expand Up @@ -178,3 +163,4 @@ lazy val templateImports: Seq[String] = {
}

lazy val generatePlay25TemplatesTask = taskKey[Seq[File]]("Generate Play 2.5 templates")
lazy val generateUnitTestFixtures = taskKey[Unit]("Generate unit test fixtures")
19 changes: 11 additions & 8 deletions docs/maintainers/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@

### Unit Tests

The suite of unit tests runs against a set of test fixtures with data extracted from [hmrc-frontend's yaml documentation](https://github.com/hmrc/hmrc-frontend/blob/master/src/components/page-heading/page-heading.yaml)
for each component. The yaml examples are used in `hmrc-frontend`'s own unit test suite.
The unit tests work against two sets of fixtures.

The test fixtures are generated from the release of `hmrc-frontend` [used in the library](/README.md#sbt).
A script [TODO: document script] generates the fixtures which are manually included in the resources directory under `src/test/resources/fixtures/`.
The unit tests will pick up the fixtures folder matching the version of `hmrc-frontend` in the dependencies.
The first set in `src/test/resources/fixtures/test-fixtures` are derived from data extracted
from [hmrc-frontend's yaml documentation](https://github.com/hmrc/hmrc-frontend)
for each component. The yaml examples are used in `hmrc-frontend`'s own unit test suite.

_Future work: Unit testing the library using a fixed set of test data provides only very basic coverage. if we can improve
the test coverage via generative testing described on the next section, we could discard the test fixtures completely._
An additional, manually created, set of fixtures in `src/test/resources/fixtures/additional-fixtures` captures test
cases that are not covered by the published examples. For example, the layout and template components
do not have published examples in `govuk-frontend` so they are placed in this directory.

To regenerate the test fixtures, you will need the template renderer running locally (see below).
Then run ```sbt generateUnitTestFixtures```. The template renderer does not need to be running while the unit
tests are executing.

### Generative Testing

Expand Down Expand Up @@ -168,7 +172,6 @@ feature, templates should not be written with backwards incompatible features on
[@if else if](https://github.com/playframework/twirl/issues/33).

## Useful Links
- [x-frontend-snapshotter](https://github.com/dorightdigital/x-frontend-snapshotter) - provides static test fixtures for `govuk-frontend` and `hmrc-frontend` components in unit tests
- [x-govuk-component-renderer](https://github.com/hmrc/x-govuk-component-renderer) - service that returns HTML for `govuk-frontend` and `hmrc-frontend` component input parameters in the form of JSON objects - useful for confirming Twirl HTML outputs in integration tests
- [hmrc-frontend](https://github.com/hmrc/hmrc-frontend/) - reusable Nunjucks HTML components for HMRC design patterns
- [HMRC Design Patterns](https://design.tax.service.gov.uk/hmrc-design-patterns/) - documentation for the use of `hmrc-frontend` components
Expand Down
6 changes: 2 additions & 4 deletions docs/maintainers/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
- Input `hmrc-frontend` and select the relevant version from the dropdown
- Hit `Deploy!`
2. Bump webjar [dependency](https://github.com/hmrc/hmrc-frontend/tags) for `hmrc-frontend` in `build.sbt`.
3. Generate and copy fixtures folder to test/resources/fixtures (this won't be necessary when we implement generative testing to increase test coverage).
- Follow `Usage` steps in [x-frontend-snapshotter](https://github.com/dorightdigital/x-frontend-snapshotter)
- Extract the contents of zip file `test-fixtures-[version-number].tar.gz` created in `target` and copy these to test/resources/fixtures/test-fixtures-[version-number] in this repo.
3. Generate fixtures folder `src/test/resources/fixtures/test-fixtures`.
- With the template renderer running locally (see below), execute `sbt generateUnitTestFixtures`
4. Run unit tests: `sbt clean test`.
5. Start integration test dependencies: .
6. Run integration tests: `sbt clean it:test`.
Expand Down Expand Up @@ -73,7 +72,6 @@ index 51d2d346..713db75d 100644
```

## Useful Links
- [x-frontend-snapshotter](https://github.com/dorightdigital/x-frontend-snapshotter) - provides static test fixtures for `govuk-frontend` and `hmrc-frontend` components in unit tests
- [x-govuk-component-renderer](https://github.com/hmrc/x-govuk-component-renderer) - service that returns HTML for `govuk-frontend` and `hmrc-frontend` component input parameters in the form of JSON objects - useful for confirming Twirl HTML outputs in integration tests
- [hmrc-frontend](https://github.com/hmrc/hmrc-frontend/) - reusable Nunjucks HTML components for HMRC design patterns
- [HMRC Design Patterns](https://design.tax.service.gov.uk/hmrc-design-patterns/) - documentation for the use of `hmrc-frontend` components
60 changes: 60 additions & 0 deletions project/GenerateFixtures.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.io.File

import play.api.libs.json.{JsObject, Json}
import sbt._
import scalaj.http.{Http, HttpOptions}
import scala.util.{Failure, Success, Try}

object GenerateFixtures {
def getExamples(frontendVersion: String): Seq[JsObject] = {

val rendererPort: String = "3000"
val endpoint: String = s"http://localhost:$rendererPort/snapshot/hmrc/$frontendVersion"

val attempt: Try[Seq[JsObject]] = Try {
val response = Http(endpoint)
.option(HttpOptions.connTimeout(2000))
.option(HttpOptions.readTimeout(5000))
.asString
.body

Json.parse(response).as[Seq[JsObject]]
}

attempt match {
case Success(snapshot) => snapshot
case Failure(e) =>
println(s"Failed to fetch snapshot from Template Service at $endpoint. Details: [${e.getLocalizedMessage}].")
Seq()
}
}

def generateFixtures(javaResourcesDir: File, govukFrontendVersion: String): Unit = {
val resourcesDir = javaResourcesDir
val fixturesDir = resourcesDir / "fixtures"
val testFixturesDir =
(fixturesDir / s"test-fixtures")

IO.delete(testFixturesDir)
IO.createDirectory(testFixturesDir)

IO.write((testFixturesDir / "VERSION.txt"), govukFrontendVersion)

for (example <- getExamples(govukFrontendVersion)) {
val componentName = (example \ "componentName").as[String]
val componentJson = Json.obj(
"name" -> componentName
)
val inputJson = (example \ "input").as[JsObject]
val exampleDir = testFixturesDir / (example \ "exampleId").as[String]

IO.createDirectory(exampleDir)
IO.write((exampleDir / "input.json"), Json.prettyPrint(inputJson))
IO.write(
(exampleDir / "component.json"),
Json.prettyPrint(componentJson)
)
IO.write((exampleDir / "output.txt"), (example \ "output").as[String])
}
}
}
3 changes: 3 additions & 0 deletions project/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
libraryDependencies ++= Seq(
"org.scalaj" %% "scalaj-http" % "2.4.2"
)

This file was deleted.

This file was deleted.

0 comments on commit 5d84e85

Please sign in to comment.