Skip to content

Commit

Permalink
Merge pull request #73 from mbland/replace-gradle-node-plugin
Browse files Browse the repository at this point in the history
Replace gradle-node-plugin with own tasks
  • Loading branch information
mbland committed Dec 23, 2023
2 parents 1e0bac9 + 8526cc7 commit c6636d6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ jobs:
- name: Setup Gradle
uses: gradle/gradle-build-action@v2

- name: Install Node packages
run: ./gradlew --warning-mode=fail pnpmInstall

- name: Build and test
run: ./gradlew --warning-mode=fail build

Expand Down
26 changes: 4 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,10 @@ application deployment).
### OS Compatibility

I run Arm64/aarch64 builds of [Ubuntu Linux][] and [Windows 11 Professional][]
under [Parallels Desktop for Mac][] on Apple Silicon. There's some work to allow
WebDriver to use [Chromium][] or [Firefox][] on Linux, as no aarch64 build of
[Google Chrome][] is available. The [node-gradle/gradle-node-plugin][] breaks on
Windows 11 when it tries to execute `uname`:

```text
PS C:\Users\msb\src\mbland\tomcat-servlet-testing-example> ./gradlew.bat
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be
reused, use --status for details
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\msb\src\mbland\tomcat-servlet-testing-example\strcalc\build.gradle.kts' line: 8
* What went wrong:
An exception occurred applying plugin request [id: 'com.github.node-gradle.node']
> Failed to apply plugin 'com.github.node-gradle.node'.
> A problem occurred starting process 'command 'uname''
```
under [Parallels Desktop for Mac][] on Apple Silicon. `vite.config.js` contains
a workaround to allow WebDriver to use [Chromium][] or [Firefox][] on Linux, as
no aarch64 build of [Google Chrome][] is available. (I hope to contribute
this workaround upstream, at which point I'll remove it from `vite.config.js`.)

Also, [it doesn't appear as though nested virtualzation will ever be supported by
the aarch 64 Windows 11 on an Apple M1][no-vm-nesting]. This means the
Expand Down Expand Up @@ -802,7 +786,6 @@ TODO(mbland): Document how the following are configured:
- [Selenium: Design patterns and development strategies][]
- [TestTomcat](./strcalc/src/test/java/com/mike_bland/training/testing/utils/TestTomcat.java)
(for medium tests)
- [node-gradle/gradle-node-plugin][]

## Implementing core logic using Test Driven Development and unit tests

Expand All @@ -821,7 +804,6 @@ Coming soon...
[headless Chrome]: https://developer.chrome.com/blog/headless-chrome/
[test doubles]: https://mike-bland.com/2023/09/06/test-doubles.html
[Apache Solr]: https://solr.apache.org/
[node-gradle/gradle-node-plugin]: https://github.com/node-gradle/gradle-node-plugin
[Ubuntu Linux]: https://ubuntu.com/desktop
[Windows 11 Professional]: https://kb.parallels.com/125375/
[Parallels Desktop for Mac]: https://www.parallels.com/products/desktop/
Expand Down
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0"
id("com.github.node-gradle.node") version "7.0.1" apply false
id("io.freefair.lombok") version "8.4" apply false
id("com.github.ben-manes.versions") version "0.50.0" apply false
}
Expand Down
44 changes: 25 additions & 19 deletions strcalc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
plugins {
war
jacoco
id("com.github.node-gradle.node")
id("io.freefair.lombok")
id("com.github.ben-manes.versions")
}
Expand Down Expand Up @@ -51,45 +50,52 @@ jacoco {
toolVersion = "0.8.11"
}

// The frontend sources are under src/main/frontend. The Node plugin generates
// the pnpm_build and pnpm_test-ci task definitions from the "scripts" field of
// package.json.
node {
nodeProjectDir = file("src/main/frontend")
}

// Set inputs and outputs for the pnpm_build and pnpn_test-ci frontend tasks.
// This enables Gradle to cache the results instead of executing these tasks
// unconditionally on every build.
// Set inputs and outputs for the frontend tasks. This enables Gradle to cache
// the results instead of executing these tasks unconditionally on every build.
//
// The vite.config.js file in src/main/frontend specifies:
//
// - The output directory, build/webapp
// - The default test results directory, build/test-results/test-frontend
// - The coverage directory, build/reports/frontend/coverage
//
// The vite.config.ci-browser.js file, used by pnpm_test-ci, also specifies
// The vite.config.ci-browser.js file, used by frontendTest, also specifies
// the build/test-results/test-frontend-browser directory.
val frontendDir = project.layout.projectDirectory.dir("src/main/frontend")
val frontendSources = fileTree(frontendDir) {
exclude("node_modules", ".*", "*.md")
}
val frontendOutputDir = project.layout.buildDirectory.dir("webapp").get()
fun frontendCmd(task: Exec, vararg commands: String) {
task.workingDir(frontendDir)
task.commandLine("pnpm", *commands)
}

val frontendBuild = tasks.named<Task>("pnpm_build") {
val frontendInstall = tasks.register<Exec>("frontendInstall") {
description = "Install src/main/frontend npm packages"
inputs.files(frontendDir.files("package.json", "pnpm-lock.yaml"))
outputs.upToDateWhen { true }
frontendCmd(this, "install")
}

val frontendBuild = tasks.register<Exec>("frontendBuild") {
description = "Build src/main/frontend JavaScript into build/webapp"
inputs.files(frontendSources)
dependsOn(frontendInstall)
inputs.files(frontendSources.filter { !it.name.endsWith(".test.js") })
outputs.dir(frontendOutputDir)
frontendCmd(this, "build")
}

val frontendTest = tasks.named<Task>("pnpm_test-ci") {
val frontendTest = tasks.register<Exec>("frontendTest") {
description = "Test frontend JavaScript in src/main/frontend"
dependsOn(frontendBuild)
inputs.files(frontendSources)
frontendCmd(this, "test-ci")

val resultsDir = java.testResultsDir.get()
outputs.dir(resultsDir.dir("test-frontend"))
outputs.dir(resultsDir.dir("test-frontend-browser"))
outputs.dirs(
resultsDir.dir("test-frontend"),
resultsDir.dir("test-frontend-browser")
)
}

// Configure the "war" task generated by the Gradle War plugin to depend upon
Expand Down Expand Up @@ -219,7 +225,7 @@ val relativeToRootDir = fun(absPath: java.nio.file.Path): java.nio.file.Path {
val mergeTestReports = fun(resultsDir: File) {
val taskName = resultsDir.name

// The `pnpm_test-ci` output is already merged. Trying to merge it again
// The `frontendTest` output is already merged. Trying to merge it again
// results in an empty file, so skip it.
if (taskName.startsWith("test-frontend")) return

Expand Down

0 comments on commit c6636d6

Please sign in to comment.