1
0
Fork 0
mirror of https://github.com/actions/setup-java synced 2024-11-09 23:22:40 +00:00

feat: finished implementation and added e2e tests

This commit is contained in:
AUGMELO 2023-01-15 16:25:06 +00:00
parent e1a751b5d3
commit d105eb1d73
6 changed files with 45157 additions and 4871 deletions

View file

@ -339,22 +339,22 @@ jobs:
shell: bash
setup-java-version-from-pom-spring-boot-specification:
name: ${{ matrix.distribution }} version from file 'openjdk64-11.0.2' - ${{ matrix.os }}
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica' ]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create .java-version file
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version="1.0" encoding="UTF-8"?>" > pom.xml
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>com.test</groupId>" >> pom.xml
echo " <artifactId>Test</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
@ -380,5 +380,179 @@ jobs:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}"
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-maven-compiler-source-specification:
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <properties>" >> pom.xml
echo " <maven.compiler.source>1.8</maven.compiler.source>" >> pom.xml
echo " <maven.compiler.target>1.8</maven.compiler.target>" >> pom.xml
echo " </properties>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-maven-compiler-release-specification:
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <properties>" >> pom.xml
echo " <maven.compiler.release>8</maven.compiler.release>" >> pom.xml
echo " </properties>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-maven-compiler-configuration-source-specification:
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " <configuration>" >> pom.xml
echo " <source>1.8</source>" >> pom.xml
echo " <target>1.8</target>" >> pom.xml
echo " </configuration>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-maven-compiler-configuration-release-specification:
name: ${{ matrix.distribution }} version from pom.xml 'openjdk64-11.0.2' - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " <configuration>" >> pom.xml
echo " <release>11</release>" >> pom.xml
echo " </configuration>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version-file: 'pom.xml'
- name: Verify Java
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
shell: bash

40004
dist/cleanup/index.js vendored

File diff suppressed because it is too large Load diff

4927
dist/setup/index.js vendored

File diff suppressed because it is too large Load diff

4789
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -33,7 +33,8 @@
"@actions/io": "^1.0.2",
"@actions/tool-cache": "^1.6.1",
"semver": "^7.3.4",
"xmlbuilder2": "^2.4.0"
"xmlbuilder2": "^2.4.0",
"xpath": "0.0.32"
},
"devDependencies": {
"@types/jest": "^27.0.2",

View file

@ -1,12 +1,15 @@
import os from 'os';
import os, { version } from 'os';
import path from 'path';
import * as fs from 'fs';
import * as semver from 'semver';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import { create as xmlCreate } from 'xmlbuilder2';
import { select } from 'xpath';
import * as tc from '@actions/tool-cache';
import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants';
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces';
export function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir();
@ -106,12 +109,16 @@ export function getVersionFromFileContent(
distributionName: string
): string | null {
let fileContent = null;
core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content)
} else if (fileName.includes('pom.xml')){
fileContent = parsePomXmlFile(content)
fileContent = parseJavaVersionFile(content);
} else if (fileName.includes('pom.xml')) {
fileContent = parsePomXmlFile(content);
} else {
throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`)
throw new Error(
`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`
);
}
if (!fileContent) {
@ -148,75 +155,59 @@ function parseJavaVersionFile(content: string): string | null {
return null;
}
return fileContent
return fileContent;
}
function parsePomXmlFile(content: string): string | null {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(content, "text/xml");
const versionDefinitionTypes = [
getByMavenCompilerSource,
getByMavenCompilerRelease,
getBySpringBootSpecification,
]
const versionFound = versionDefinitionTypes.filter(function(definitionType){
const version = definitionType(xmlDoc)
function parsePomXmlFile(xmlString: string): string | null {
const xmlDoc = xmlCreate(xmlString);
const versionDefinitionTypes = [getByMavenCompilerSpecification, getBySpringBootSpecification];
return version !== null
})
for (var definitionType of versionDefinitionTypes) {
var version = definitionType(xmlDoc);
return versionFound?.at(0)?.toString() ?? null;
}
function getByMavenCompilerSource(xmlDoc: Document): string | null {
const possibleTags = [
"maven.compiler.source",
"source"
]
const tagFound = possibleTags.filter(function(tag) {
const version = getVersionByTagName(xmlDoc, tag)
return version !== null
})
return tagFound?.at(0)?.toString() ?? null
}
function getByMavenCompilerRelease(xmlDoc: Document): string | null {
const possibleTags = [
"maven.compiler.release",
"release"
]
const tagFound = possibleTags.filter(function(tag) {
const version = getVersionByTagName(xmlDoc, tag)
return version !== null
})
return tagFound?.at(0)?.toString() ?? null
}
function getBySpringBootSpecification(xmlDoc: Document): string | null {
return getVersionByTagName(xmlDoc, "java.version")
}
function getVersionByTagName(xmlDoc: Document, tagName: string): string | null {
const element = xmlDoc.getElementsByTagName(tagName)
if (element.length < 1) {
return null
if (version !== null) {
return version;
}
}
return element[0].textContent
return null;
}
function getByMavenCompilerSpecification(xmlDoc: XMLBuilder): string | null {
const possibleTags = [
'//properties/maven.compiler.source',
'//configuration/source',
'//properties/maven.compiler.release',
'//configuration/release'
];
for (var tag of possibleTags) {
const version = getVersionByTagName(xmlDoc, tag);
if (version !== null) {
return version;
}
}
return null;
}
function getBySpringBootSpecification(xmlDoc: XMLBuilder): string | null {
return getVersionByTagName(xmlDoc, '//properties/java.version');
}
function getVersionByTagName(xmlDoc: XMLBuilder, xpathQuery: string): string | null {
const element = select(`string(${xpathQuery})`, xmlDoc.node as any);
core.debug(`Found: '${element}' using xpath query: '${xpathQuery}'`);
if (element == undefined || element.length == 0) {
return null;
}
return element.toString();
}
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation(content: string): string {
return content.startsWith('1.') ? content.substring(2) : content;
}