-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from weavejester/patch-add-cljfmt
Add cljfmt support
- Loading branch information
Showing
7 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import _os from 'os' | ||
import * as _core from '@actions/core' | ||
import * as _tc from '@actions/tool-cache' | ||
import * as cljfmt from '../src/cljfmt' | ||
|
||
const getJson = jest.fn() | ||
jest.mock('@actions/http-client', () => ({ | ||
HttpClient: function () { | ||
return {getJson} | ||
} | ||
})) | ||
|
||
jest.mock('os') | ||
const os: jest.Mocked<typeof _os> = _os as never | ||
|
||
jest.mock('@actions/tool-cache') | ||
const tc: jest.Mocked<typeof _tc> = _tc as never | ||
|
||
jest.mock('@actions/core') | ||
const core: jest.Mocked<typeof _core> = _core as never | ||
|
||
describe('cljfmt tests', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('getLatestCljFmt', () => { | ||
it('uses tag_name as latest version', async () => { | ||
getJson.mockResolvedValueOnce({ | ||
result: {tag_name: '1.2.3'} | ||
}) | ||
const res = await cljfmt.getLatestCljFmt() | ||
expect(res).toBe('1.2.3') | ||
expect(getJson).toHaveBeenCalledWith( | ||
'https://api.github.com/repos/weavejester/cljfmt/releases/latest', | ||
undefined | ||
) | ||
}) | ||
|
||
it('supports authorization', async () => { | ||
getJson.mockResolvedValueOnce({ | ||
result: {tag_name: '1.2.3'} | ||
}) | ||
const res = await cljfmt.getLatestCljFmt('token 123') | ||
expect(res).toBe('1.2.3') | ||
expect(getJson).toHaveBeenCalledWith( | ||
'https://api.github.com/repos/weavejester/cljfmt/releases/latest', | ||
{Authorization: 'token 123'} | ||
) | ||
}) | ||
|
||
it('throws on http client error', async () => { | ||
getJson.mockRejectedValueOnce(new Error('some error')) | ||
await expect(cljfmt.getLatestCljFmt()).rejects.toThrow('some error') | ||
}) | ||
|
||
it('throws on wrong client answer', async () => { | ||
getJson.mockResolvedValueOnce({result: {foo: 'bar'}}) | ||
await expect(cljfmt.getLatestCljFmt()).rejects.toThrow( | ||
`Can't obtain latest cljfmt version` | ||
) | ||
}) | ||
}) | ||
|
||
describe('getArtifactName', () => { | ||
test.each` | ||
platform | artifact | ||
${'win32'} | ${`cljfmt-1.2.3-win-amd64.zip`} | ||
${'darwin'} | ${`cljfmt-1.2.3-darwin-amd64.tar.gz`} | ||
${'linux'} | ${`cljfmt-1.2.3-linux-amd64.tar.gz`} | ||
${'foobar'} | ${`cljfmt-1.2.3-linux-amd64.tar.gz`} | ||
`('$platform -> $artifact', ({platform, artifact}) => { | ||
os.platform.mockReturnValueOnce(platform as never) | ||
expect(cljfmt.getArtifactName('1.2.3')).toBe(artifact) | ||
}) | ||
}) | ||
|
||
describe('getArtifactUrl', () => { | ||
test.each` | ||
platform | artifact | ||
${'win32'} | ${`cljfmt-1.2.3-win-amd64.zip`} | ||
${'darwin'} | ${`cljfmt-1.2.3-darwin-amd64.tar.gz`} | ||
${'linux'} | ${`cljfmt-1.2.3-linux-amd64.tar.gz`} | ||
${'foobar'} | ${`cljfmt-1.2.3-linux-amd64.tar.gz`} | ||
`('$platform -> $artifact', ({platform, artifact}) => { | ||
os.platform.mockReturnValueOnce(platform as never) | ||
expect(cljfmt.getArtifactUrl('1.2.3')).toBe( | ||
`https://github.com/weavejester/cljfmt/releases/download/1.2.3/${artifact}` | ||
) | ||
}) | ||
}) | ||
|
||
describe('setup', () => { | ||
it('uses cache', async () => { | ||
tc.find.mockReturnValueOnce('/foo/bar') | ||
|
||
await cljfmt.setup('1.2.3') | ||
|
||
expect(tc.find).toHaveBeenCalledWith('cljfmt', '1.2.3') | ||
expect(core.addPath).toHaveBeenCalledWith('/foo/bar') | ||
}) | ||
|
||
it('uses cache', async () => { | ||
tc.find.mockReturnValueOnce('/foo/bar') | ||
|
||
await cljfmt.setup('1.2.3') | ||
|
||
expect(tc.find).toHaveBeenCalledWith('cljfmt', '1.2.3') | ||
expect(core.addPath).toHaveBeenCalledWith('/foo/bar') | ||
}) | ||
|
||
it('fetches exact version', async () => { | ||
tc.downloadTool.mockResolvedValueOnce('/foo/cljfmt.tar.gz') | ||
tc.extractTar.mockResolvedValueOnce('/bar/baz') | ||
tc.cacheDir.mockResolvedValueOnce('/qux') | ||
|
||
await cljfmt.setup('1.2.3', 'token 123') | ||
|
||
expect(tc.find).toHaveBeenCalledWith('cljfmt', '1.2.3') | ||
expect(tc.downloadTool).toHaveBeenCalledWith( | ||
'https://github.com/weavejester/cljfmt/releases/download/1.2.3/cljfmt-1.2.3-linux-amd64.tar.gz', | ||
undefined, | ||
'token 123' | ||
) | ||
expect(tc.cacheDir).toHaveBeenCalledWith('/bar/baz', 'cljfmt', '1.2.3') | ||
expect(core.addPath).toHaveBeenCalledWith('/qux') | ||
}) | ||
|
||
it('fetches latest version', async () => { | ||
getJson.mockResolvedValueOnce({ | ||
result: {tag_name: '9.9.9'} | ||
}) | ||
tc.downloadTool.mockResolvedValueOnce('/foo/cljfmt.tar.gz') | ||
tc.extractTar.mockResolvedValueOnce('/bar/baz') | ||
tc.cacheDir.mockResolvedValueOnce('/qux') | ||
|
||
await cljfmt.setup('latest', 'token 123') | ||
|
||
expect(getJson).toHaveBeenCalledWith( | ||
'https://api.github.com/repos/weavejester/cljfmt/releases/latest', | ||
{Authorization: 'token 123'} | ||
) | ||
expect(tc.find).toHaveBeenCalledWith('cljfmt', '9.9.9') | ||
expect(tc.downloadTool).toHaveBeenCalledWith( | ||
'https://github.com/weavejester/cljfmt/releases/download/9.9.9/cljfmt-9.9.9-linux-amd64.tar.gz', | ||
undefined, | ||
'token 123' | ||
) | ||
expect(tc.cacheDir).toHaveBeenCalledWith('/bar/baz', 'cljfmt', '9.9.9') | ||
expect(core.addPath).toHaveBeenCalledWith('/qux') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as core from '@actions/core' | ||
import * as http from '@actions/http-client' | ||
import * as os from 'os' | ||
import * as tc from '@actions/tool-cache' | ||
|
||
export const identifier = 'cljfmt' | ||
|
||
export async function getLatestCljFmt(githubAuth?: string): Promise<string> { | ||
const client = new http.HttpClient('actions/setup-cljfmt', undefined, { | ||
allowRetries: true, | ||
maxRetries: 3 | ||
}) | ||
|
||
const res = await client.getJson<{tag_name: string}>( | ||
`https://api.github.com/repos/weavejester/cljfmt/releases/latest`, | ||
githubAuth ? {Authorization: githubAuth} : undefined | ||
) | ||
|
||
const result = res.result?.tag_name | ||
if (result) { | ||
return result | ||
} | ||
|
||
throw new Error(`Can't obtain latest cljfmt version`) | ||
} | ||
|
||
export function getArtifactName(version: string): string { | ||
const platform = os.platform() | ||
switch (platform) { | ||
case 'win32': | ||
return `cljfmt-${version}-win-amd64.zip` | ||
case 'darwin': | ||
return `cljfmt-${version}-darwin-amd64.tar.gz` | ||
default: | ||
return `cljfmt-${version}-linux-amd64.tar.gz` | ||
} | ||
} | ||
|
||
export function getArtifactUrl(version: string): string { | ||
const archiveName = getArtifactName(version) | ||
return `https://github.com/weavejester/cljfmt/releases/download/${version}/${archiveName}` | ||
} | ||
|
||
export async function extract(source: string): Promise<string> { | ||
return source.endsWith('.zip') | ||
? await tc.extractZip(source) | ||
: await tc.extractTar(source) | ||
} | ||
|
||
export async function setup( | ||
version: string, | ||
githubAuth?: string | ||
): Promise<void> { | ||
const ver = version === 'latest' ? await getLatestCljFmt(githubAuth) : version | ||
|
||
let toolDir = tc.find(identifier, ver) | ||
if (!toolDir) { | ||
const archiveUrl = getArtifactUrl(ver) | ||
core.info(`Downloading: ${archiveUrl}`) | ||
|
||
const artifactFile = await tc.downloadTool( | ||
archiveUrl, | ||
undefined, | ||
githubAuth | ||
) | ||
|
||
const extractedDir = await extract(artifactFile) | ||
toolDir = await tc.cacheDir(extractedDir, identifier, ver) | ||
core.info(`Caching directory: ${toolDir}`) | ||
} else { | ||
core.info(`Using cached directory: ${toolDir}`) | ||
} | ||
|
||
core.addPath(toolDir) | ||
} |
Oops, something went wrong.