|
| 1 | +// Windows hosts cannot run the docker-compose based integration/auth suite natively, and the |
| 2 | +// remaining (untagged) tests would run against a Windows JDK/filesystem, which can drift from CI. |
| 3 | +// So on Windows we route the whole non-integration suite through a small Linux container instead. |
| 4 | +// |
| 5 | +// Linux and macOS are untouched: useTestContainer is false there and every task below is a no-op. |
| 6 | +// |
| 7 | +// ./gradlew test on Windows -> one container run, all three modules |
| 8 | +// ./gradlew test -PnoDocker run natively (this is how you run integration/auth locally) |
| 9 | + |
| 10 | +def testImage = 'extender-unit-test:1.0.0' |
| 11 | + |
| 12 | +// EXTENDER_IN_TEST_CONTAINER guards against recursion. os.name is already "Linux" inside the |
| 13 | +// container so this is belt and braces, but it keeps the intent explicit. |
| 14 | +ext.useTestContainer = System.getProperty('os.name').toLowerCase().startsWith('windows') && |
| 15 | + !project.hasProperty('noDocker') && |
| 16 | + System.getenv('EXTENDER_IN_TEST_CONTAINER') == null |
| 17 | + |
| 18 | +// Inside the container we build into build-container/ rather than build/. The two must not be |
| 19 | +// shared: the host Gradle daemon keeps its own build outputs open on Windows, and the container |
| 20 | +// then cannot overwrite them ("Unable to delete file .../extender-client-0.5.0.jar"). It also |
| 21 | +// keeps Linux and Windows compilation output from mixing. |
| 22 | +if (System.getenv('EXTENDER_IN_TEST_CONTAINER') != null) { |
| 23 | + allprojects { |
| 24 | + layout.buildDirectory = layout.projectDirectory.dir('build-container') |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +tasks.register('buildTestImage', Exec) { |
| 29 | + group = 'verification' |
| 30 | + description = 'Builds the container image used to run the tests on Windows.' |
| 31 | + // Docker layer caching makes this a no-op after the first run. |
| 32 | + commandLine 'docker', 'build', |
| 33 | + '-t', testImage, |
| 34 | + '-f', file('server/docker/Dockerfile.unit-test').absolutePath, |
| 35 | + file('server/docker').absolutePath |
| 36 | +} |
| 37 | + |
| 38 | +tasks.register('dockerTest', Exec) { |
| 39 | + group = 'verification' |
| 40 | + description = 'Runs the non-integration tests of all modules inside a Linux container.' |
| 41 | + dependsOn 'buildTestImage' |
| 42 | + |
| 43 | + // Docker wants forward slashes even on Windows. |
| 44 | + def mount = rootDir.absolutePath.replace('\\', '/') |
| 45 | + |
| 46 | + commandLine 'docker', 'run', '--rm', |
| 47 | + '-v', "${mount}:/workspace", |
| 48 | + // Named volume, so the dependency cache survives between runs. |
| 49 | + '-v', 'extender-gradle-cache:/gradle-home', |
| 50 | + '-e', 'EXTENDER_IN_TEST_CONTAINER=1', |
| 51 | + '-w', '/workspace', |
| 52 | + testImage, |
| 53 | + // --project-cache-dir keeps the container out of the host's .gradle/, so a Windows-side |
| 54 | + // Gradle daemon and the container never fight over the same lock files. Everything else |
| 55 | + // (build/, reports, jacoco) stays on the bind mount and is visible from the host. |
| 56 | + '--no-daemon', |
| 57 | + '--project-cache-dir', '/tmp/gradle-project-cache', |
| 58 | + '-PexcludeTags=integration', |
| 59 | + ':server:test', ':client:test', ':manifestmergetool:test' |
| 60 | +} |
| 61 | + |
| 62 | +if (rootProject.ext.useTestContainer) { |
| 63 | + subprojects { |
| 64 | + tasks.matching { it.name == 'test' }.configureEach { |
| 65 | + // All three depend on the single root task, so `./gradlew test` still produces |
| 66 | + // exactly one container run. |
| 67 | + dependsOn ':dockerTest' |
| 68 | + enabled = false |
| 69 | + } |
| 70 | + // The container already wrote its own report under build-container/; there is no |
| 71 | + // execution data on the host side to report on. |
| 72 | + tasks.matching { it.name == 'jacocoTestReport' }.configureEach { enabled = false } |
| 73 | + } |
| 74 | +} |
| 75 | +// On Linux/macOS nothing is wired up - `test` runs natively as before. `dockerTest` stays |
| 76 | +// invokable by hand there if you want to reproduce the Windows path. |
0 commit comments