From a993e13330622d6bb0377854ed065a2d9d841544 Mon Sep 17 00:00:00 2001 From: Alexis Couvreur Date: Sun, 1 Mar 2026 12:35:28 -0500 Subject: [PATCH] version-file takes precedence --- README.md | 2 +- __tests__/version.test.ts | 69 +++++++++++++++++++++------------------ action.yml | 3 +- src/context.ts | 2 +- src/version.ts | 16 +++------ 5 files changed, 45 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 8338667..fae7461 100644 --- a/README.md +++ b/README.md @@ -198,7 +198,7 @@ Gets the version of GoReleaser to use from a file. The path must be relative to the root of the project, or the `workdir` if defined. -This parameter supports `.tool-versions` files. +This parameter only supports `.tool-versions` files for now.
Example diff --git a/__tests__/version.test.ts b/__tests__/version.test.ts index 333fde5..65b6bb0 100644 --- a/__tests__/version.test.ts +++ b/__tests__/version.test.ts @@ -34,33 +34,7 @@ describe('getRequestedVersion', () => { expect(v).toBe('v1.2.3'); }); - it('warns when both version and version-file are provided and returns version', () => { - const inputs: Inputs = { - distribution: 'goreleaser', - version: 'v9.9.9', - versionFile: '.tool-versions', - args: '', - workdir: '', - installOnly: false - }; - - const v = getRequestedVersion(inputs); - expect(v).toBe('v9.9.9'); - }); - - it('throws when version is empty and version-file does not exist', () => { - const inputs: Inputs = { - distribution: 'goreleaser', - version: '', - versionFile: 'nonexistent-file', - args: '', - workdir: '', - installOnly: false - }; - - expect(() => getRequestedVersion(inputs)).toThrow(); - }); - + it('parses .tool-versions and returns v-prefixed version', () => { const toolVersionsPath = path.join(tmpDir, '.tool-versions'); fs.writeFileSync(toolVersionsPath, 'goreleaser 1.2.3\n'); @@ -78,17 +52,50 @@ describe('getRequestedVersion', () => { expect(v).toBe('v1.2.3'); }); - it('defaults to ~> v2 when nothing provided', () => { + it('when both version and version-file are provided, version-file takes precedence', () => { + const toolVersionsPath = path.join(tmpDir, '.tool-versions'); + fs.writeFileSync(toolVersionsPath, 'goreleaser 1.2.3\n'); + + const inputs: Inputs = { + distribution: 'goreleaser', + version: 'v9.9.9', + versionFile: '.tool-versions', + args: '', + workdir: tmpDir, + installOnly: false + }; + + const v = getRequestedVersion(inputs); + expect(v).toBe('v1.2.3'); + }); + + it('throws when version-file does not exist', () => { const inputs: Inputs = { distribution: 'goreleaser', version: '', - versionFile: '', + versionFile: 'nonexistent-file', args: '', workdir: '', installOnly: false }; - const v = getRequestedVersion(inputs); - expect(v).toBe('~> v2'); + expect(() => getRequestedVersion(inputs)).toThrow(); }); + + it('throws when version-file is an unsupported type', () => { + const toolVersionsPath = path.join(tmpDir, 'unsupported-file'); + fs.writeFileSync(toolVersionsPath, 'goreleaser 1.2.3\n'); + + const inputs: Inputs = { + distribution: 'goreleaser', + version: '', + versionFile: 'unsupported-file', + args: '', + workdir: tmpDir, + installOnly: false + }; + + expect(() => getRequestedVersion(inputs)).toThrow(); + }); + }); diff --git a/action.yml b/action.yml index 5d76cff..86aee77 100644 --- a/action.yml +++ b/action.yml @@ -13,13 +13,12 @@ inputs: required: false version: description: 'GoReleaser version' - required: false + default: '~> v2' version-file: description: | Gets the version of GoReleaser to use from a file. The path must be relative to the root of the project, or the `workdir` if defined. This parameter supports `.tool-versions` files. - Only works with `install-mode: binary` (the default). required: false args: description: 'Arguments to pass to GoReleaser' diff --git a/src/context.ts b/src/context.ts index df278f0..930de4e 100644 --- a/src/context.ts +++ b/src/context.ts @@ -16,7 +16,7 @@ export interface Inputs { export async function getInputs(): Promise { return { distribution: core.getInput('distribution') || 'goreleaser', - version: core.getInput('version'), + version: core.getInput('version') || '~> v2', versionFile: core.getInput('version-file'), args: core.getInput('args'), workdir: core.getInput('workdir') || '.', diff --git a/src/version.ts b/src/version.ts index d9bbf19..c4cc93a 100644 --- a/src/version.ts +++ b/src/version.ts @@ -7,13 +7,8 @@ export function getRequestedVersion(inputs: Inputs): string { let requestedVersion = inputs.version; let versionFilePath = inputs.versionFile; - if (requestedVersion && versionFilePath) { - core.warning( - `Both version (${requestedVersion}) and version-file (${versionFilePath}) inputs are specified, only version will be used` - ); - } - - if (requestedVersion == '' && versionFilePath) { + // If versionFile is provided, it takes precedence over version input. + if (versionFilePath) { const workingDirectory = inputs.workdir; if (workingDirectory) { versionFilePath = path.join(workingDirectory, versionFilePath); @@ -29,13 +24,10 @@ export function getRequestedVersion(inputs: Inputs): string { // asdf/mise file. const match = content.match(/^goreleaser\s+([^\n#]+)/m); requestedVersion = match ? 'v' + match[1].trim().replace(/^v/gi, '') : ''; + } else { + throw new Error(`Unsupported version file: ${versionFilePath}. Only .tool-versions files are supported.`); } } - if (!requestedVersion) { - // default to latest v2 release - requestedVersion = '~> v2'; - } - return requestedVersion; }