Add a test for merging existing toolchains.xml (#151)

This commit is contained in:
Philip Wedemann 2024-04-11 01:20:36 +02:00 committed by GitHub
parent 30a2ee13f2
commit 0f427bc07b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 61 additions and 5 deletions

View file

@ -35,6 +35,6 @@ jobs:
done
- run: |
echo "The 'dist' directory is auotmatically updated by the release process."
echo "The 'dist' directory is automatically updated by the release process."
echo "It should not be updated manually in a non-release branch or a pull request."
exit 1

View file

@ -9,7 +9,7 @@ import {CacheListener} from './cache-reporting'
import {saveCache, restoreCache, cacheDebug, isCacheDebuggingEnabled, tryDelete} from './cache-utils'
import {CacheConfig} from '../configuration'
import {GradleHomeEntryExtractor, ConfigurationCacheEntryExtractor} from './gradle-home-extry-extractor'
import {getPredefinedToolchains, readResourceFileAsString} from './gradle-user-home-utils'
import {getPredefinedToolchains, mergeToolchainContent, readResourceFileAsString} from './gradle-user-home-utils'
const RESTORED_CACHE_KEY_KEY = 'restored-cache-key'
@ -233,8 +233,7 @@ export class GradleUserHomeCache {
} else {
// Merge into an existing toolchains.xml file
const existingToolchainContent = fs.readFileSync(toolchainXmlTarget, 'utf8')
const appendedContent = preInstalledToolchains.split('<toolchains>').pop()!
const mergedContent = existingToolchainContent.replace('</toolchains>', appendedContent)
const mergedContent = mergeToolchainContent(existingToolchainContent, preInstalledToolchains)
fs.writeFileSync(toolchainXmlTarget, mergedContent)
core.info(`Merged default JDK locations into ${toolchainXmlTarget}`)

View file

@ -42,3 +42,8 @@ export function getPredefinedToolchains(): string | null {
toolchainsXml += `</toolchains>\n`
return toolchainsXml
}
export function mergeToolchainContent(existingToolchainContent: string, preInstalledToolchains: string): string {
const appendedContent = preInstalledToolchains.split('<toolchains>').pop()!
return existingToolchainContent.replace('</toolchains>', appendedContent)
}

View file

@ -1,4 +1,4 @@
import {getPredefinedToolchains} from "../../src/caching/gradle-user-home-utils";
import {getPredefinedToolchains, mergeToolchainContent} from "../../src/caching/gradle-user-home-utils";
describe('predefined-toolchains', () => {
const OLD_ENV = process.env
@ -62,4 +62,56 @@ describe('predefined-toolchains', () => {
`)
})
})
it("merges with existing toolchains", async () => {
jest.resetModules()
process.env = {
"JAVA_HOME_11_X64": "/jdks/foo_11",
}
// language=XML
const existingToolchains =
`<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>8</version>
</provides>
<configuration>
<jdkHome>\${env.JAVA_HOME_8_X64}</jdkHome>
</configuration>
</toolchain>
</toolchains>
`
const mergedContent = mergeToolchainContent(existingToolchains, getPredefinedToolchains()!)
expect(mergedContent).toBe(
// language=XML
`<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>8</version>
</provides>
<configuration>
<jdkHome>\${env.JAVA_HOME_8_X64}</jdkHome>
</configuration>
</toolchain>
<!-- JDK Toolchains installed by default on GitHub-hosted runners -->
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
</provides>
<configuration>
<jdkHome>\${env.JAVA_HOME_11_X64}</jdkHome>
</configuration>
</toolchain>
</toolchains>
`)
})
})