Compare commits

..

1 Commits
main ... v5.0.0

Author SHA1 Message Date
GitHub Action
15650b3ad7 build 2026-03-25 17:25:33 +00:00
12 changed files with 41778 additions and 1585 deletions

View File

@ -59,7 +59,7 @@ jobs:
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
uses: github/codeql-action/init@b1bff81932f5cdfc8695c7752dcee935dcd061c8 # v3.29.5
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
@ -86,6 +86,6 @@ jobs:
echo ' make release'
exit 1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
uses: github/codeql-action/analyze@b1bff81932f5cdfc8695c7752dcee935dcd061c8 # v3.29.5
with:
category: '/language:${{matrix.language}}'

1
.gitignore vendored
View File

@ -330,4 +330,3 @@ ASALocalRun/
node_modules
# Transpiled JS
lib/

18
jest.config.js Normal file
View File

@ -0,0 +1,18 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
coverageThreshold: {
global: {
branches: 0,
functions: 14,
lines: 27,
statements: 27
}
}
}

34286
lib/index.js Normal file

File diff suppressed because one or more lines are too long

8849
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,12 +3,10 @@
"version": "5.0.0",
"private": true,
"main": "lib/index.js",
"type": "module",
"scripts": {
"build": "tsc --noEmit && esbuild src/index.ts --bundle --platform=node --target=node20 --format=esm --outfile=lib/index.js --banner:js=\"import { createRequire } from 'module';const require = createRequire(import.meta.url);\"",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"test-coverage": "vitest run --coverage",
"build": "npm i ncc && npx ncc build src/index.ts -o lib",
"test": "jest",
"test-coverage": "jest --coverage",
"format": "prettier --write .",
"format-check": "prettier --check .",
"prepare": "husky"
@ -21,16 +19,19 @@
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/core": "^3.0.0",
"@actions/exec": "^3.0.0",
"@actions/tool-cache": "^4.0.0"
"@actions/core": "^2.0.2",
"@actions/exec": "^2.0.0",
"@actions/tool-cache": "^3.0.0",
"ncc": "^0.3.6"
},
"devDependencies": {
"@types/jest": "^30.0.0",
"@types/node": "^25.0.9",
"esbuild": "^0.27.4",
"@vercel/ncc": "^0.38.4",
"husky": "^9.1.7",
"jest": "^30.2.0",
"prettier": "3.8.0",
"typescript": "5.9.3",
"vitest": "^4.0.18"
"ts-jest": "^29.4.6",
"typescript": "5.9.3"
}
}

View File

@ -42,7 +42,7 @@ export async function getLatestPatchVersion(
}
return latestPatch
} catch (error) {
core.debug(String(error))
core.debug(error)
core.warning('GetLatestPatchVersionFailed')
throw new Error(`Failed to get latest patch version for ${version}`)
}

View File

@ -1,4 +1,4 @@
import {run} from './run.js'
import {run} from './run'
import * as core from '@actions/core'
run().catch(core.setFailed)

View File

@ -1,43 +1,28 @@
import {vi, describe, test, expect, beforeEach} from 'vitest'
import * as path from 'path'
import * as util from 'util'
vi.mock('os')
vi.mock('fs')
vi.mock('@actions/tool-cache', async (importOriginal) => {
const actual = await importOriginal<typeof import('@actions/tool-cache')>()
return {
...actual,
downloadTool: vi.fn(),
find: vi.fn(),
cacheFile: vi.fn()
}
})
vi.mock('@actions/core')
const os = await import('os')
const fs = await import('fs')
const toolCache = await import('@actions/tool-cache')
const core = await import('@actions/core')
const run = await import('./run.js')
const {
import * as run from './run'
import {
getkubectlDownloadURL,
getKubectlArch,
getExecutableExtension,
getLatestPatchVersion
} = await import('./helpers.js')
} from './helpers'
import * as os from 'os'
import * as toolCache from '@actions/tool-cache'
import * as fs from 'fs'
import * as path from 'path'
import * as core from '@actions/core'
import * as util from 'util'
describe('Testing all functions in run file.', () => {
beforeEach(() => {
vi.clearAllMocks()
jest.clearAllMocks()
})
test('getExecutableExtension() - return .exe when os is Windows', () => {
vi.mocked(os.type).mockReturnValue('Windows_NT')
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
expect(getExecutableExtension()).toBe('.exe')
expect(os.type).toHaveBeenCalled()
})
test('getExecutableExtension() - return empty string for non-windows OS', () => {
vi.mocked(os.type).mockReturnValue('Darwin')
jest.spyOn(os, 'type').mockReturnValue('Darwin')
expect(getExecutableExtension()).toBe('')
expect(os.type).toHaveBeenCalled()
})
@ -48,7 +33,7 @@ describe('Testing all functions in run file.', () => {
])(
'getKubectlArch() - return on %s os arch %s kubectl arch',
(osArch, kubectlArch) => {
vi.mocked(os.arch).mockReturnValue(osArch as NodeJS.Architecture)
jest.spyOn(os, 'arch').mockReturnValue(osArch as NodeJS.Architecture)
expect(getKubectlArch()).toBe(kubectlArch)
expect(os.arch).toHaveBeenCalled()
}
@ -56,7 +41,7 @@ describe('Testing all functions in run file.', () => {
test.each([['arm'], ['arm64'], ['amd64']])(
'getkubectlDownloadURL() - return the URL to download %s kubectl for Linux',
(arch) => {
vi.mocked(os.type).mockReturnValue('Linux')
jest.spyOn(os, 'type').mockReturnValue('Linux')
const kubectlLinuxUrl = util.format(
'https://dl.k8s.io/release/v1.15.0/bin/linux/%s/kubectl',
arch
@ -68,7 +53,7 @@ describe('Testing all functions in run file.', () => {
test.each([['arm'], ['arm64'], ['amd64']])(
'getkubectlDownloadURL() - return the URL to download %s kubectl for Darwin',
(arch) => {
vi.mocked(os.type).mockReturnValue('Darwin')
jest.spyOn(os, 'type').mockReturnValue('Darwin')
const kubectlDarwinUrl = util.format(
'https://dl.k8s.io/release/v1.15.0/bin/darwin/%s/kubectl',
arch
@ -80,7 +65,7 @@ describe('Testing all functions in run file.', () => {
test.each([['arm'], ['arm64'], ['amd64']])(
'getkubectlDownloadURL() - return the URL to download %s kubectl for Windows',
(arch) => {
vi.mocked(os.type).mockReturnValue('Windows_NT')
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
const kubectlWindowsUrl = util.format(
'https://dl.k8s.io/release/v1.15.0/bin/windows/%s/kubectl.exe',
arch
@ -90,30 +75,40 @@ describe('Testing all functions in run file.', () => {
}
)
test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(fs.readFileSync).mockReturnValue('v1.20.4')
jest
.spyOn(toolCache, 'downloadTool')
.mockReturnValue(Promise.resolve('pathToTool'))
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4')
expect(await run.getStableKubectlVersion()).toBe('v1.20.4')
expect(toolCache.downloadTool).toHaveBeenCalled()
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
})
test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => {
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(fs.readFileSync).mockReturnValue('')
jest
.spyOn(toolCache, 'downloadTool')
.mockReturnValue(Promise.resolve('pathToTool'))
jest.spyOn(fs, 'readFileSync').mockReturnValue('')
expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
expect(toolCache.downloadTool).toHaveBeenCalled()
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
})
test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => {
vi.mocked(toolCache.downloadTool).mockRejectedValue('Unable to download.')
jest
.spyOn(toolCache, 'downloadTool')
.mockRejectedValue('Unable to download.')
expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
expect(toolCache.downloadTool).toHaveBeenCalled()
})
test('downloadKubectl() - download kubectl, add it to toolCache and return path to it', async () => {
vi.mocked(toolCache.find).mockReturnValue('')
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(toolCache.cacheFile).mockResolvedValue('pathToCachedTool')
vi.mocked(os.type).mockReturnValue('Windows_NT')
vi.mocked(fs.chmodSync).mockImplementation(() => {})
jest.spyOn(toolCache, 'find').mockReturnValue('')
jest
.spyOn(toolCache, 'downloadTool')
.mockReturnValue(Promise.resolve('pathToTool'))
jest
.spyOn(toolCache, 'cacheFile')
.mockReturnValue(Promise.resolve('pathToCachedTool'))
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {})
expect(await run.downloadKubectl('v1.15.0')).toBe(
path.join('pathToCachedTool', 'kubectl.exe')
)
@ -127,10 +122,10 @@ describe('Testing all functions in run file.', () => {
)
})
test('downloadKubectl() - throw DownloadKubectlFailed error when unable to download kubectl', async () => {
vi.mocked(toolCache.find).mockReturnValue('')
vi.mocked(toolCache.downloadTool).mockRejectedValue(
'Unable to download kubectl.'
)
jest.spyOn(toolCache, 'find').mockReturnValue('')
jest
.spyOn(toolCache, 'downloadTool')
.mockRejectedValue('Unable to download kubectl.')
await expect(run.downloadKubectl('v1.15.0')).rejects.toThrow(
'DownloadKubectlFailed'
)
@ -140,9 +135,9 @@ describe('Testing all functions in run file.', () => {
test('downloadKubectl() - throw kubectl not found error when receive 404 response', async () => {
const kubectlVersion = 'v1.15.0'
const arch = 'arm128'
vi.mocked(os.arch).mockReturnValue(arch as NodeJS.Architecture)
vi.mocked(toolCache.find).mockReturnValue('')
vi.mocked(toolCache.downloadTool).mockImplementation((_) => {
jest.spyOn(os, 'arch').mockReturnValue(arch as any)
jest.spyOn(toolCache, 'find').mockReturnValue('')
jest.spyOn(toolCache, 'downloadTool').mockImplementation((_) => {
throw new toolCache.HTTPError(404)
})
await expect(run.downloadKubectl(kubectlVersion)).rejects.toThrow(
@ -157,10 +152,11 @@ describe('Testing all functions in run file.', () => {
expect(toolCache.downloadTool).toHaveBeenCalled()
})
test('downloadKubectl() - return path to existing cache of kubectl', async () => {
vi.mocked(core.getInput).mockImplementation(() => 'v1.15.5')
vi.mocked(toolCache.find).mockReturnValue('pathToCachedTool')
vi.mocked(os.type).mockReturnValue('Windows_NT')
vi.mocked(fs.chmodSync).mockImplementation(() => {})
jest.spyOn(core, 'getInput').mockImplementation(() => 'v1.15.5')
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {})
jest.spyOn(toolCache, 'downloadTool')
expect(await run.downloadKubectl('v1.15.0')).toBe(
path.join('pathToCachedTool', 'kubectl.exe')
)
@ -173,8 +169,8 @@ describe('Testing all functions in run file.', () => {
expect(toolCache.downloadTool).not.toHaveBeenCalled()
})
test('getLatestPatchVersion() - download and return latest patch version', async () => {
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(fs.readFileSync).mockReturnValue('v1.27.15')
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
const result = await getLatestPatchVersion('1', '27')
@ -185,8 +181,8 @@ describe('Testing all functions in run file.', () => {
})
test('getLatestPatchVersion() - throw error when patch version is empty', async () => {
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(fs.readFileSync).mockReturnValue('')
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
jest.spyOn(fs, 'readFileSync').mockReturnValue('')
await expect(getLatestPatchVersion('1', '27')).rejects.toThrow(
'Failed to get latest patch version for 1.27'
@ -194,17 +190,17 @@ describe('Testing all functions in run file.', () => {
})
test('getLatestPatchVersion() - throw error when download fails', async () => {
vi.mocked(toolCache.downloadTool).mockRejectedValue(
new Error('Network error')
)
jest
.spyOn(toolCache, 'downloadTool')
.mockRejectedValue(new Error('Network error'))
await expect(getLatestPatchVersion('1', '27')).rejects.toThrow(
'Failed to get latest patch version for 1.27'
)
})
test('resolveKubectlVersion() - expands major.minor to latest patch', async () => {
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(fs.readFileSync).mockReturnValue('v1.27.15')
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
const result = await run.resolveKubectlVersion('1.27')
expect(result).toBe('v1.27.15')
@ -219,20 +215,20 @@ describe('Testing all functions in run file.', () => {
expect(result).toBe('v1.27.15')
})
test('resolveKubectlVersion() - expands v-prefixed major.minor to latest patch', async () => {
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(fs.readFileSync).mockReturnValue('v1.27.15')
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
const result = await run.resolveKubectlVersion('v1.27')
expect(result).toBe('v1.27.15')
})
test('run() - download specified version and set output', async () => {
vi.mocked(core.getInput).mockReturnValue('v1.15.5')
vi.mocked(toolCache.find).mockReturnValue('pathToCachedTool')
vi.mocked(os.type).mockReturnValue('Windows_NT')
vi.mocked(fs.chmodSync).mockImplementation()
vi.mocked(core.addPath).mockImplementation()
vi.spyOn(console, 'log').mockImplementation()
vi.mocked(core.setOutput).mockImplementation()
jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
jest.spyOn(fs, 'chmodSync').mockImplementation()
jest.spyOn(core, 'addPath').mockImplementation()
jest.spyOn(console, 'log').mockImplementation()
jest.spyOn(core, 'setOutput').mockImplementation()
expect(await run.run()).toBeUndefined()
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
@ -242,15 +238,17 @@ describe('Testing all functions in run file.', () => {
)
})
test('run() - get latest version, download it and set output', async () => {
vi.mocked(core.getInput).mockReturnValue('latest')
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
vi.mocked(fs.readFileSync).mockReturnValue('v1.20.4')
vi.mocked(toolCache.find).mockReturnValue('pathToCachedTool')
vi.mocked(os.type).mockReturnValue('Windows_NT')
vi.mocked(fs.chmodSync).mockImplementation()
vi.mocked(core.addPath).mockImplementation()
vi.spyOn(console, 'log').mockImplementation()
vi.mocked(core.setOutput).mockImplementation()
jest.spyOn(core, 'getInput').mockReturnValue('latest')
jest
.spyOn(toolCache, 'downloadTool')
.mockReturnValue(Promise.resolve('pathToTool'))
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4')
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
jest.spyOn(fs, 'chmodSync').mockImplementation()
jest.spyOn(core, 'addPath').mockImplementation()
jest.spyOn(console, 'log').mockImplementation()
jest.spyOn(core, 'setOutput').mockImplementation()
expect(await run.run()).toBeUndefined()
expect(toolCache.downloadTool).toHaveBeenCalledWith(
'https://dl.k8s.io/release/stable.txt'

View File

@ -8,7 +8,7 @@ import {
getKubectlArch,
getExecutableExtension,
getLatestPatchVersion
} from './helpers.js'
} from './helpers'
const kubectlToolName = 'kubectl'
const stableKubectlVersion = 'v1.15.0'

View File

@ -1,12 +1,8 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "./src",
"outDir": "./lib",
"skipLibCheck": true,
"noEmit": true
"module": "commonjs",
"lib": ["ES2020", "DOM"]
},
"exclude": ["node_modules", "**/*.test.ts", "vitest.config.ts"]
"exclude": ["node_modules", "test"]
}

View File

@ -1,10 +0,0 @@
import {defineConfig} from 'vitest/config'
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['**/*.test.ts'],
clearMocks: true
}
})