Skip to content

Commit

Permalink
use tool-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
markandersontrocme committed Dec 5, 2024
1 parent 5b3ba08 commit 75e8496
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 74 deletions.
82 changes: 43 additions & 39 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ const infoMock = jest.spyOn(core, 'info').mockImplementation()
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
const execMock = jest.spyOn(exec, 'exec').mockImplementation()
const downloadToolMock = jest.spyOn(tc, 'downloadTool').mockImplementation()
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {})
jest.spyOn(tc, 'cacheFile').mockResolvedValue('/cached/path')
jest.spyOn(tc, 'find').mockImplementation(() => null) // Simulate cache miss

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()

// Mock platform and architecture
Object.defineProperty(process, 'platform', {
value: 'linux'
})
Object.defineProperty(process, 'arch', {
value: 'x64'
})
})

it('installs the up CLI and logs in when token is provided', async () => {
it('installs the Up CLI and logs in when token is provided', async () => {
// Mock inputs
getInputMock.mockImplementation(name => {
switch (name) {
Expand All @@ -36,46 +45,44 @@ describe('action', () => {
})

// Mock tool-cache and exec
downloadToolMock.mockResolvedValue('download')
downloadToolMock.mockResolvedValue('/tmp/downloaded/up')
execMock.mockResolvedValue(0)

await main.run()

// Verify downloadTool is called with the correct URL
expect(downloadToolMock).toHaveBeenCalledWith('https://cli.upbound.io')

expect(fs.chmodSync).toHaveBeenCalledWith('download', '775')
expect(downloadToolMock).toHaveBeenCalledWith(
'https://cli.upbound.io/stable/v0.12.1/bin/linux_amd64/up'
)

// Verify script execution
expect(execMock).toHaveBeenNthCalledWith(1, 'bash', ['download'], {
env: expect.objectContaining({
CHANNEL: 'stable',
VERSION: 'v0.12.1'
})
})
// Verify caching behavior
expect(tc.cacheFile).toHaveBeenCalledWith(
'/tmp/downloaded/up',
'up',
'up',
'v0.12.1'
)

// Verify version check
expect(execMock).toHaveBeenNthCalledWith(2, 'up', ['--version'])
expect(execMock).toHaveBeenNthCalledWith(1, 'up', ['version'])

// Verify login command
expect(execMock).toHaveBeenNthCalledWith(3, 'up', [
expect(execMock).toHaveBeenNthCalledWith(2, 'up', [
'login',
'--token',
'test-token'
])

// Verify logs
expect(core.info).toHaveBeenCalledWith(
'Downloading the Up CLI installation script...'
)
expect(core.info).toHaveBeenCalledWith(
'Running the Up CLI installation script...'
'up CLI version v0.12.1 installed at /cached/path'
)
expect(core.info).toHaveBeenCalledWith('Verifying installation...')
expect(core.info).toHaveBeenCalledWith('Up CLI installation complete!')
expect(core.info).toHaveBeenCalledWith('Logging in to Upbound...')
expect(core.info).toHaveBeenCalledWith('up CLI installation complete!')
})

it('installs the up CLI but skips login when token is not provided', async () => {
it('installs the Up CLI but skips login when token is not provided', async () => {
// Mock inputs
getInputMock.mockImplementation(name => {
switch (name) {
Expand All @@ -91,26 +98,26 @@ describe('action', () => {
})

// Mock tool-cache and exec
downloadToolMock.mockResolvedValue('download')
execMock.mockResolvedValue(0) // Simulate successful script and commands
downloadToolMock.mockResolvedValue('/tmp/downloaded/up')
execMock.mockResolvedValue(0)

await main.run()

// Verify downloadTool is called with the correct URL
expect(downloadToolMock).toHaveBeenCalledWith('https://cli.upbound.io')

expect(fs.chmodSync).toHaveBeenCalledWith('download', '775')
expect(downloadToolMock).toHaveBeenCalledWith(
'https://cli.upbound.io/stable/v0.12.1/bin/linux_amd64/up'
)

// Verify script execution
expect(execMock).toHaveBeenNthCalledWith(1, 'bash', ['download'], {
env: expect.objectContaining({
CHANNEL: 'stable',
VERSION: 'v0.12.1'
})
})
// Verify caching behavior
expect(tc.cacheFile).toHaveBeenCalledWith(
'/tmp/downloaded/up',
'up',
'up',
'v0.12.1'
)

// Verify version check
expect(execMock).toHaveBeenNthCalledWith(2, 'up', ['--version'])
expect(execMock).toHaveBeenNthCalledWith(1, 'up', ['version'])

// Verify login command is not called
expect(execMock).not.toHaveBeenCalledWith('up', [
Expand All @@ -121,13 +128,10 @@ describe('action', () => {

// Verify logs
expect(core.info).toHaveBeenCalledWith(
'Downloading the Up CLI installation script...'
)
expect(core.info).toHaveBeenCalledWith(
'Running the Up CLI installation script...'
'up CLI version v0.12.1 installed at /cached/path'
)
expect(core.info).toHaveBeenCalledWith('Verifying installation...')
expect(core.info).toHaveBeenCalledWith('Up CLI installation complete!')
expect(core.info).toHaveBeenCalledWith('up CLI installation complete!')
})

it('adds a "v" prefix if not present', () => {
Expand Down
79 changes: 62 additions & 17 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

79 changes: 62 additions & 17 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,75 @@ function formatVersion(version) {
return /^\d/.test(version) ? `v${version}` : version
}

async function downloadUp(version, channel, url, platform, architecture) {
let os = 'linux'
let arch = 'amd64'
let bin = 'up'
let extension = ''

Check failure on line 14 in src/main.js

View workflow job for this annotation

GitHub Actions / Lint Codebase

'extension' is never reassigned. Use 'const' instead

Check failure on line 14 in src/main.js

View workflow job for this annotation

GitHub Actions / JavaScript Tests

'extension' is never reassigned. Use 'const' instead

switch (platform) {
case 'darwin':
os = 'darwin'
break
case 'win32':
os = 'windows'
bin = 'up.exe'
break
case 'linux':
os = 'linux'
break
default:
core.warning(`Unknown platform: ${platform}; defaulting to ${os}`)
break
}

switch (architecture) {
case 'arm64':
arch = 'arm64'
break
case 'x64':
arch = 'amd64'
break
default:
core.warning(
`Unknown architecture: ${architecture}; defaulting to ${arch}`
)
break
}

const osArch = `${os}_${arch}`
const downloadURL = `${url}/${channel}/${version}/bin/${osArch}/${bin}`
core.debug(`up CLI download URL: ${downloadURL}`)

// Download binary
const downloadPath = await tc.downloadTool(downloadURL)

// Return binary path
return platform === 'win32'
? downloadPath
: tc.cacheFile(downloadPath, bin, 'up', version)
}

async function run() {
try {
const version = formatVersion(core.getInput('version'))
const channel = core.getInput('channel')
const url = core.getInput('url')
const token = core.getInput('token')

// Download the installation script
const downloadPath = await tc.downloadTool(url)

// Make the script executable
fs.chmodSync(downloadPath, '775')

await exec.exec('bash', [downloadPath], {
env: {
...process.env,
CHANNEL: channel,
VERSION: version
}
})

const cachedPath = await tc.cacheDir('./up', 'up', version)
let installPath = tc.find('up', version)
if (!installPath) {
installPath = await downloadUp(
version,
channel,
url,
process.platform,
process.arch
)
}

core.addPath(cachedPath)
core.addPath(installPath)
core.info(`up CLI version ${version} installed at ${installPath}`)

core.info('Verifying installation...')
await exec.exec('up', ['version'])
Expand All @@ -40,7 +85,7 @@ async function run() {
await exec.exec('up', ['login', '--token', token])
}

core.info('Up CLI installation complete!')
core.info('up CLI installation complete!')
} catch (error) {
core.setFailed(error.message)
}
Expand Down

0 comments on commit 75e8496

Please sign in to comment.