mirror of
https://github.com/actions/setup-go.git
synced 2025-04-24 01:50:52 +00:00
add rudimentary .tool-versions parser
This commit is contained in:
parent
fac708d667
commit
fc952ad45a
5 changed files with 148 additions and 59 deletions
16
.github/workflows/versions.yml
vendored
16
.github/workflows/versions.yml
vendored
|
@ -134,6 +134,22 @@ jobs:
|
|||
run: __tests__/verify-go.sh 1.19
|
||||
shell: bash
|
||||
|
||||
tool-versions-file:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ ubuntu-latest, windows-latest, macos-latest ]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Setup Go and check latest
|
||||
uses: ./
|
||||
with:
|
||||
tool-versions-file: __tests__/data/.tool-versions
|
||||
- name: verify go
|
||||
run: __tests__/verify-go.sh 1.16.3
|
||||
shell: bash
|
||||
|
||||
setup-versions-from-manifest:
|
||||
name: Setup ${{ matrix.go }} ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
|
4
__tests__/data/.tool-versions
Normal file
4
__tests__/data/.tool-versions
Normal file
|
@ -0,0 +1,4 @@
|
|||
terraform 1.3.7
|
||||
golang 1.16.3
|
||||
python 3.10.11
|
||||
ruby 3.2.1
|
|
@ -523,7 +523,8 @@ describe('setup-go', () => {
|
|||
return '/Users/testuser/go';
|
||||
});
|
||||
|
||||
mkdirpSpy.mockImplementation(async () => {});
|
||||
mkdirpSpy.mockImplementation(async () => {
|
||||
});
|
||||
existsSpy.mockImplementation(() => {
|
||||
return false;
|
||||
});
|
||||
|
@ -957,4 +958,48 @@ use .
|
|||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe('tool-versions-file', () => {
|
||||
const toolVersionsFile = `terraform 1.3.7
|
||||
golang 1.16.3
|
||||
python 3.10.11
|
||||
ruby 3.2.1
|
||||
`;
|
||||
|
||||
it('reads version from .tool-versions when specified', async () => {
|
||||
inputs['tool-versions-file'] = '.special-tool-versions'
|
||||
existsSpy.mockImplementation(() => true);
|
||||
readFileSpy.mockImplementation(() => Buffer.from(toolVersionsFile))
|
||||
|
||||
await main.run();
|
||||
|
||||
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.16.3');
|
||||
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.16.3...');
|
||||
expect(logSpy).toHaveBeenCalledWith('matching 1.16.3...');
|
||||
});
|
||||
|
||||
it('is overwritten by go-version param', async () => {
|
||||
inputs['go-version'] = '1.18.2'
|
||||
inputs['tool-versions-file'] = '.special-tool-versions'
|
||||
existsSpy.mockImplementation(() => true);
|
||||
readFileSpy.mockImplementation(() => Buffer.from(toolVersionsFile))
|
||||
|
||||
await main.run();
|
||||
|
||||
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.18.2');
|
||||
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.18.2...');
|
||||
expect(logSpy).toHaveBeenCalledWith('matching 1.18.2...');
|
||||
});
|
||||
|
||||
it('uses .tool-versions as a default when no version specified', async () => {
|
||||
existsSpy.mockImplementation(() => true);
|
||||
readFileSpy.mockImplementation(() => Buffer.from(toolVersionsFile))
|
||||
|
||||
await main.run();
|
||||
|
||||
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.16.3');
|
||||
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.16.3...');
|
||||
expect(logSpy).toHaveBeenCalledWith('matching 1.16.3...');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -366,6 +366,13 @@ export function parseGoVersionFile(versionFilePath: string): string {
|
|||
return contents.trim();
|
||||
}
|
||||
|
||||
export function parseToolVersionsFile(toolVersionsPath: string): string {
|
||||
const contents = fs.readFileSync(toolVersionsPath).toString();
|
||||
|
||||
const match = contents.match(/^golang (\d+(\.\d+)*)/m);
|
||||
return match ? match[1] : '';
|
||||
}
|
||||
|
||||
async function resolveStableVersionDist(versionSpec: string, arch: string) {
|
||||
const archFilter = sys.getArch(arch);
|
||||
const platFilter = sys.getPlatform();
|
||||
|
|
27
src/main.ts
27
src/main.ts
|
@ -138,24 +138,41 @@ export function parseGoVersion(versionString: string): string {
|
|||
function resolveVersionInput(): string {
|
||||
let version = core.getInput('go-version');
|
||||
const versionFilePath = core.getInput('go-version-file');
|
||||
let toolVersionsPath = core.getInput('tool-versions-file');
|
||||
|
||||
if (version && versionFilePath) {
|
||||
if (version && (versionFilePath || toolVersionsPath)) {
|
||||
core.warning(
|
||||
'Both go-version and go-version-file inputs are specified, only go-version will be used'
|
||||
'Multiple version inputs are specified, only go-version will be used'
|
||||
);
|
||||
}
|
||||
|
||||
if (version) {
|
||||
return version;
|
||||
}
|
||||
|
||||
if (versionFilePath) {
|
||||
} else if (versionFilePath) {
|
||||
if (!fs.existsSync(versionFilePath)) {
|
||||
throw new Error(
|
||||
`The specified go version file at: ${versionFilePath} does not exist`
|
||||
);
|
||||
}
|
||||
version = installer.parseGoVersionFile(versionFilePath);
|
||||
} else {
|
||||
// in the case of no version specification, reach for .tool-versions
|
||||
if(toolVersionsPath && !fs.existsSync(toolVersionsPath)) {
|
||||
throw new Error(
|
||||
`The specified .tool-versions file at ${toolVersionsPath} does not exist`
|
||||
)
|
||||
}
|
||||
|
||||
if (!toolVersionsPath) {
|
||||
toolVersionsPath = '.tool-versions'
|
||||
if(!fs.existsSync(toolVersionsPath)) {
|
||||
throw new Error(
|
||||
`No .tool-versions file was found in the project path. Please specify using tool-versions-file`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
version = installer.parseToolVersionsFile(toolVersionsPath)
|
||||
}
|
||||
|
||||
return version;
|
||||
|
|
Loading…
Add table
Reference in a new issue