Skip to content

Replace the info.app tokens in the test phase resources - #17306

Open
matrei wants to merge 4 commits into
7.1.xfrom
fix/test-phase-application-yml
Open

matrei wants to merge 4 commits into
7.1.xfrom
fix/test-phase-application-yml

Conversation

@matrei

@matrei matrei commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

The fix of #17305 for 7.1.x, where the code moved: TestPhasesGradlePlugin adds grails-app/conf to the resources of every test phase. Merging #17305 up would fix the integration tests here too, since IntegrationTestGradlePlugin creates that phase before GrailsGradlePlugin configures the resources, but not the other test phases, such as a functionalTest phase declared in build.gradle. This replaces the tokens in the resources of every test phase.

processResources replaces the @info.app.name@, @info.app.version@ and @info.app.grailsVersion@ tokens of application.yml in the main resources, but grails-app/conf is also a resource directory of each test phase, where it is copied unprocessed. That copy comes before the processed one on the classpath of the tests, and Metadata reads the first application.yml on the classpath. So in the tests, the name, version and Grails version of the application are the tokens, and DefaultGrailsPluginManager logs for every plugin, whenever the application starts, that it may not be compatible with the Grails version @info.app.grailsVersion@ (see #17305).

This replaces the tokens in the resources of every test phase as processResources does in the main resources, with the same tokens. grails-app/conf stays a resource directory of the test phases, so they see the same files as before, including the spring/resources.groovy of a plugin, which processResources of a plugin leaves out. Only the files that contain a token are filtered, so the others are copied unchanged, whatever their encoding. The check reads the file with the filteringCharset of the task, the charset that ReplaceTokens reads it with. A file that an earlier filter changes, such as an expand of a convention of the build, is filtered without the check: getFile() fails for it, and reading it with open() would run that filter once more. That filter decodes and encodes the file anyway, so a file without tokens is written as it leaves it.

The values of the tokens are an input of the resources task of each test phase, such as processIntegrationTestResources, and now also of processResources, which had the same gap: without it, processResources stays up to date when the version changes, and leaves the old version in application.yml.

8.0.x removed grails-app/conf from the resources of the test phases instead, in 289519a ("Prevent duplicate resources on the classpath", part of #15541), which needed 75e7336 for resources.groovy. This keeps it, so that nothing else changes in a patch release.

Merging: merging 7.0.x up after #17305 conflicts in GrailsGradlePlugin, IntegrationTestGradlePluginSpec and the build.gradle of its test project: keep the versions of this PR, which cover every test phase. On 8.0.x, which no longer copies grails-app/conf, keep its version: configureTestPhaseResources isn't needed there.

Results that change

In the tests of every test phase, info.app.name, info.app.version and info.app.grailsVersion, and so grailsApplication.metadata, have the values of the running application instead of the tokens. The same tokens are replaced in the other *.groovy, *.yml and *.xml files of their resources that contain them, as in the main resources. processResources and the resources tasks of the test phases run again when the name or version of the project or the Grails version changes. I don't see a use for the old results, so this adds no setting.

Testing

IntegrationTestGradlePluginSpec checks, for the integration tests and a functionalTest phase declared in build.gradle:

  • that they read application.yml with the tokens replaced, which fails without the change;
  • that the main resources and the resources of the phase have the new version after it changed in build.gradle, which fails without the input of either task;
  • that a file in ISO-8859-1 without tokens is copied unchanged, which fails when every file is filtered;
  • that the tokens are replaced in a file in UTF-16 when filteringCharset is UTF-16, which fails when the check reads the file as ISO-8859-1;
  • that the tokens are replaced after a filter that numbers the lines, which a convention in settings.gradle adds before the Grails plugin is applied, and that a file without tokens is numbered once, which fails with UnsupportedOperationException when the check calls getFile() without handling it, and with 2: a: 1 when it reads the file with open().

It also checks that the integration tests of a plugin still get its spring/resources.groovy, which fails without the copy of grails-app/conf.

./gradlew build codeStyle passes in grails-gradle, and so do the integration tests of the issue11005, cache and micronaut examples.

@matrei matrei added the run ci label Dec 3, 2026
@matrei
matrei marked this pull request as ready for review December 3, 2026 12:57
@jdaugherty

Copy link
Copy Markdown
Contributor

The same concern I posted here: Isn't this a breaking change? we've always told people that grails/conf is where you configure environments, and by not copying that config, you're now expecting people to define their config with the test resources directory instead. I'm not against this change, but this feels breaking and thus should go on a major release.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A plugin integration test loses the Spring beans that were left out of the packaged resources.

registerSourceDir(phaseSourceSet, srcDir)
acceptedSourceDirs.add(srcDir)
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A plugin leaves spring/resources.groovy out of the packaged resources, and an integration test needs the bean it defines. The test should see that file. It does not, because this no longer copies the configuration the package excludes. Keep that copy, and still use the processed application.yml.

The test phases, such as the integration tests, have grails-app/conf as
a resource directory of their own, which comes before the main
resources on their classpath. The processResources tasks of the test
phases copied its application.yml without replacing the info.app
tokens, as processResources does in the main resources, so the tests
read the tokens instead of the values, and every plugin logged that it
may not be compatible with the Grails version
'@info.app.grailsVersion@'.

Replace the tokens in the resources of the test phases too.
@matrei
matrei force-pushed the fix/test-phase-application-yml branch from 02aae28 to 08677da Compare December 3, 2026 20:52
@matrei matrei changed the title Read the processed application.yml in test phases Replace the info.app tokens in the test phase resources Dec 3, 2026
@matrei
matrei requested a review from jdaugherty December 3, 2026 21:51

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The earlier missing Spring beans are addressed. Requested changes. A changed version can leave the old value in the processed file, and a file with no token can be rewritten.

testPhases.configureEach { TestPhase phase ->
SourceSet phaseSourceSet = SourceSets.findSourceSet(project, phase.name)
project.tasks.named(phaseSourceSet.processResourcesTaskName, ProcessResources).configure { ProcessResources task ->
Map<String, String> replaceTokens = resolveReplaceTokens(project, grailsVersion)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version changes between two runs, and a file contains that token. The second run should write the new version. It can keep the old one, because this does not record the token values as inputs. Record them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 86862a8: the token values are now an input of the resources task of every test phase, and of processResources, which had the same gap. IntegrationTestGradlePluginSpec changes the version between two runs and checks both outputs, for integrationTest and functionalTest.

project.tasks.named(phaseSourceSet.processResourcesTaskName, ProcessResources).configure { ProcessResources task ->
Map<String, String> replaceTokens = resolveReplaceTokens(project, grailsVersion)
task.filesMatching(['**/*.groovy', '**/*.yml', '**/*.xml']) { FileCopyDetails details ->
details.filter([tokens: replaceTokens], ReplaceTokens)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A file has no token and is not UTF-8. It should be copied unchanged. A character is corrupted, because this filters every file. Leave a file with no token alone.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 86862a8: only the files that contain one of the tokens are filtered now, so the others are copied byte for byte, as before. IntegrationTestGradlePluginSpec checks it with an ISO-8859-1 file in both phases.

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback as on #17305, applied to every test phase here: please declare the token values as inputs of each phase's resources task (a small Gradle 8.14 build shows the stale version), and consider filtering only files that contain @info.app. so non-UTF-8 XML in the phase resource dirs isn't rewritten. The configureEach ordering with TestPhasesGradlePlugin looks fine, and the functionalTest case covers a phase declared in build.gradle.

One small correction to the description: merging #17305 up would fix integrationTest on 7.1.x, since that phase exists before GrailsGradlePlugin configures resources. It just wouldn't cover custom phases, which is why this version is better. Please keep the two PRs in sync when you make the input fix.

- Record the values of the tokens as an input of processResources and
  of the resources tasks of the test phases, so that they run again
  when the version changes, instead of leaving the old version in the
  processed application.yml
- Filter only the resources of the test phases that contain a token,
  so that the others are copied unchanged, whatever their encoding
@matrei

matrei commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@jdaugherty As on #17305: this PR now keeps grails-app/conf in the resources of the test phases and only replaces the info.app tokens in it, so the tests see the same files as before, and only the tokens differ.

@matrei

matrei commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@sbglasius Thanks! Done here too in 86862a8, for the resources task of every test phase and processResources, in sync with #17305. You're right about merging #17305 up: it would fix integrationTest, just not the other phases. I've corrected the description.

@matrei
matrei requested a review from sbglasius December 4, 2026 10:56

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An earlier copy filter can now fail resource processing, including for a file that has no token.

Map<String, String> replaceTokens = resolveReplaceTokens(project, grailsVersion)
task.inputs.property('replaceTokens', replaceTokens)
task.filesMatching(['**/*.groovy', '**/*.yml', '**/*.xml']) { FileCopyDetails details ->
if (containsReplaceToken(details.file, replaceTokens)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a convention plugin adds an earlier filter or expand action to a matching XML file, Gradle throws UnsupportedOperationException on details.file. Resource processing fails even when the file contains no Grails token. Read the file without depending on another filter having left it unread.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5e02ba8, as in #17305: the check reads the file with details.open(), which applies the earlier filters, instead of details.file. IntegrationTestGradlePluginSpec adds an expand from a convention, configured in settings.gradle before the Grails plugin is applied, and checks a file with a token and one without, for integrationTest and functionalTest. Without the fix, it fails with this UnsupportedOperationException.

@jdaugherty jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 86862a8. Keeping grails-app/conf addresses my earlier compatibility concern. The configureEach ordering follows the creation of each phase source set, and the integrationTest and functionalTest cases cover the intended scope. The input map and unchanged-encoding tests address the earlier findings.

The remaining filter-chain failure is shared with #17305. On Gradle 8.14.8, a minimal Copy task with an earlier filter succeeds until this filesMatching action reads details.file; it then throws UnsupportedOperationException from DefaultFileCopyDetails.getFile, including for XML with no Grails tokens. Please fix this in both branches and add a regression covering an earlier convention filter or expand action.

Existing thread: #17306 (comment)

FileCopyDetails.getFile() fails once a file has a filter, so a filter or
expand that a convention of the build added to the resources task of a
test phase before failed the task, even for a file without tokens. Read
the file with open() instead, which applies those filters.
@matrei

matrei commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

@jdaugherty Fixed here too in 5e02ba8, in sync with #17305, with the regression test for both phases. The byte-for-byte test for files without tokens is unchanged.

@matrei
matrei requested a review from jdaugherty December 5, 2026 09:53

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An earlier filter can still break resource processing, and a UTF-16 resource keeps an unresolved token.

* ASCII, so they are found in the bytes of a file in UTF-8, ISO-8859-1 or another encoding that extends ASCII.
*/
protected static boolean containsReplaceToken(FileCopyDetails details, Map<String, String> replaceTokens) {
String content = details.open().withStream { InputStream input ->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening the file to look for tokens runs an earlier filter, then the copy runs it again. A filter that numbers lines writes 2 where it should write 1, even when the file has no Grails token. A filter that consumes its input fails on the second run. Do not run the earlier filters to decide whether a token is present.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 45a6ddb, as in #17305: the check no longer runs the earlier filters. A file that already has a filter, for which getFile() fails, is filtered without the check: that filter decodes and encodes the file anyway, so a file without tokens is written as it leaves it. The convention in IntegrationTestGradlePluginSpec now numbers the lines, and the file without a token gets 1: a: 1 in both phases, as on the base.

*/
protected static boolean containsReplaceToken(FileCopyDetails details, Map<String, String> replaceTokens) {
String content = details.open().withStream { InputStream input ->
new String(input.bytes, StandardCharsets.ISO_8859_1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A UTF-16LE XML file that contains @info.app.name@, with filteringCharset set to UTF-16LE, is scanned as ISO-8859-1. The token is not seen, so the resource keeps the placeholder. Scan with the charset the copy uses.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 45a6ddb: the check reads the file with the filteringCharset of the task, the charset that ReplaceTokens reads it with. IntegrationTestGradlePluginSpec checks a UTF-16 file with filteringCharset = 'UTF-16' in both phases.

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 5e02ba8.

My two earlier points are fixed in 86862a8, for both phases, and 5e02ba8 fixes the getFile() failure after a convention's filter. IntegrationTestGradlePluginSpec passes locally (9 tests).

The point about the earlier filters running twice reproduces here, and it changes the output of a file without a token. A convention that numbers lines, int n = 0; filesMatching('**/counted.yml') { filter { line -> "${++n}: ${line}" } }, on a counted.yml of a: 1 writes 1: a: 1 on the base 73d42a0 and 2: a: 1 at this head, in processIntegrationTestResources and processFunctionalTestResources. The description says files without a token are copied unchanged, so the check should look for the tokens without running the earlier filters, for example in the source file before any filter is applied.

For comparison with the base, the other two points from today: an expand with a missing property on a file without a token fails on the base as well, and only newly fails when a later action excludes the file; a UTF-16 file with a token kept the token on the base too.

The same containsReplaceToken is in #17305, so the fix belongs in both.

Reading a file with open() to look for the tokens ran the filters that
a convention of the build added before once more, so a filter that
numbers the lines started at 2. Filter a file that already has a filter
without looking for the tokens, since that filter decodes and encodes it
anyway, and read the other files with the filtering charset of the
task, so that the tokens of a file in UTF-16 are found.
@matrei

matrei commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

@sbglasius Thanks for the reproduction! Fixed in 45a6ddb, in sync with #17305. Gradle doesn't give the source file of a file that already has a filter, so such a file is filtered without the check instead: the earlier filter decodes and encodes it anyway, and ReplaceTokens leaves a file without tokens as it is. Your numbering convention is the regression test, for both phases.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An earlier filter no longer fails the task by itself. A UTF-16 resource added on a child copy still keeps an unresolved token.

} catch (UnsupportedOperationException ignored) {
return true
}
String content = new String(file.bytes, charset)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A UTF-16 XML file added with from(...) { filteringCharset = 'UTF-16' }, under a task whose charset is UTF-8, is scanned with the task charset. The token is not seen, so the resource keeps @info.app.name@. Scan with the charset of the copy that owns the file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in #17305, I'd leave this one as it is, because the charset of the copy that owns a file isn't available to a copy action: FileCopyDetails has no getter for it, and it is only in the internal CopySpecResolver of the copy. The bytes can't tell it either: a UTF-16 file in a child copy with filteringCharset = 'UTF-16' has the same bytes as a UTF-16 fixture in a UTF-8 copy, which should be copied unchanged. Looking for the tokens in every charset would garble that fixture, and reading the charset of the copy would need reflection on Gradle internals.

It isn't a regression: the base doesn't replace the tokens in the resources of the test phases at all. processResources doesn't replace them in a child copy either, whatever its charset, since its filters are on its own copies of the source set's resources. The test phases already replace them in a child copy that has the charset of the task.

@testlens-app

testlens-app Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ CI / Hibernate5 Functional Tests (Java 25, indy=false)

🏷️ Commit: 45a6ddb
▶️ Tests: 63271 executed
⚪️ Checks: 62/62 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

@matrei
matrei requested a review from sbglasius December 5, 2026 19:33

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants