Skip to content

Commit

Permalink
Fix echo output in check-action.yml and update logging messages in ut…
Browse files Browse the repository at this point in the history
…ils.ts
  • Loading branch information
gorkem committed Mar 30, 2024
1 parent ae1c7ad commit c44ba8a
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

- name: Echo output of installer
if: always()
run: echo "${{ steps.install_kit.outputs }}"
run: echo "${{ steps.install_kit.outputs.kit-path }}"

- name: Run kit tools
shell: bash
Expand All @@ -52,7 +52,7 @@ jobs:

- name: Echo output of installer
if: always()
run: echo "${{ steps.install_kit.outputs }}"
run: echo "${{ steps.install_kit.outputs.kit-path }}"

- name: Run installed tools
shell: bash
Expand Down
38 changes: 38 additions & 0 deletions __tests__/kit-release.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { findMatchingRelease, getReleases } from '../src/releases/kit-release'

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

it('should retrieve releases successfully', async () => {
const release = await getReleases(getToken(), false)
expect(release).toBeDefined()
expect(release.length).toBeGreaterThan(0)
})

it('should retrieve latest release successfully', async () => {
const release = await getReleases(getToken(), true)
expect(release).toBeDefined()
expect(release.length).toBe(1)
})

it('should find matching release', async () => {
const releases = await getReleases(getToken(), false)
const matchingRelease = findMatchingRelease(releases, 'v0.1.0')
expect(matchingRelease).toBeDefined()
expect(matchingRelease?.tag).toBe('v0.1.0')
})
})

function getToken(): string {
const token = process.env['GITHUB_TOKEN'] || ''
if (!token) {
/* eslint-disable-next-line no-console */
console.warn(
'Skipping GitHub tests. Set $GITHUB_TOKEN to run GitHub tests.'
)
}

return token
}
59 changes: 5 additions & 54 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,78 +12,29 @@ import * as main from '../src/main'
// Mock the action's main function
const runMock = jest.spyOn(main, 'run')

// Other utilities
const timeRegex = /^\d{2}:\d{2}:\d{2}/

// Mock the GitHub Actions core library
let debugMock: jest.SpiedFunction<typeof core.debug>
let errorMock: jest.SpiedFunction<typeof core.error>
let getInputMock: jest.SpiedFunction<typeof core.getInput>
let setFailedMock: jest.SpiedFunction<typeof core.setFailed>
let setOutputMock: jest.SpiedFunction<typeof core.setOutput>

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

debugMock = jest.spyOn(core, 'debug').mockImplementation()
errorMock = jest.spyOn(core, 'error').mockImplementation()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
})

it('sets the time output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return '500'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...')
expect(debugMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching(timeRegex)
)
expect(debugMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(timeRegex)
)
expect(setOutputMock).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
)
expect(errorMock).not.toHaveBeenCalled()
})

it('sets a failed status', async () => {
// Set the action's inputs as return values from core.getInput()
it('runs main', async () => {
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return 'this is not a number'
case 'token':
return process.env['GITHUB_TOKEN'] || ''
case 'version':
return 'latest'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'milliseconds not a number'
)
expect(errorMock).not.toHaveBeenCalled()
})
})
11 changes: 4 additions & 7 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.

5 changes: 1 addition & 4 deletions src/installer/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import { getTmpDir } from '../utils/utils'
* @returns The path the file was downloaded to.
*/
export async function downloadFile(file: KitArchiveFile): Promise<string> {
// tool-cache download downloads to /tmp/<guid> to prevent collisions.
// we mimic that behaviour here but keep the file's name so it has the correct extension
// a GUID is 128 bits = 16 bytes - this one has no hyphens but it serves the same purpose.
const guid = crypto.randomBytes(16).toString('hex')
const filename = `${guid}-${file.archiveFilename}`
const dlStartTime = Date.now()
Expand All @@ -23,7 +20,7 @@ export async function downloadFile(file: KitArchiveFile): Promise<string> {
ghCore.debug(`Downloaded to ${downloadPath}`)
const elapsed = Date.now() - dlStartTime
ghCore.info(
`Downloaded ${file.archiveFilename} in ${(elapsed / 1000).toFixed(1)}s`
`📦 Downloaded ${file.archiveFilename} in ${(elapsed / 1000).toFixed(1)}s`
)
ghToolCache.isExplicitVersion
return downloadPath
Expand Down
2 changes: 1 addition & 1 deletion src/installer/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function verifyHash(
)
}

ghCore.info(`sha256 verification of ${downloadedArchivePath} succeeded.`)
ghCore.info(`sha256 verification of ${downloadedArchivePath} succeeded.`)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function getOS(): OS {
default:
currentOS = 'linux'
}
ghCore.info(`Current operating system is ${currentOS}`)
ghCore.debug(`Current operating system is ${currentOS}`)
}
return currentOS
}
Expand All @@ -56,7 +56,7 @@ export function getArch(): string {
default:
currentArch = 'x86_64'
}
ghCore.info(`Current architecture is ${currentArch}`)
ghCore.debug(`Current architecture is ${currentArch}`)
}
return currentArch
}
Expand Down

0 comments on commit c44ba8a

Please sign in to comment.