|
| 1 | +import _os from 'os' |
| 2 | +import * as _core from '@actions/core' |
| 3 | +import * as _tc from '@actions/tool-cache' |
| 4 | +import * as cljfmt from '../src/cljfmt' |
| 5 | + |
| 6 | +const getJson = jest.fn() |
| 7 | +jest.mock('@actions/http-client', () => ({ |
| 8 | + HttpClient: function () { |
| 9 | + return {getJson} |
| 10 | + } |
| 11 | +})) |
| 12 | + |
| 13 | +jest.mock('os') |
| 14 | +const os: jest.Mocked<typeof _os> = _os as never |
| 15 | + |
| 16 | +jest.mock('@actions/tool-cache') |
| 17 | +const tc: jest.Mocked<typeof _tc> = _tc as never |
| 18 | + |
| 19 | +jest.mock('@actions/core') |
| 20 | +const core: jest.Mocked<typeof _core> = _core as never |
| 21 | + |
| 22 | +describe('cljfmt tests', () => { |
| 23 | + beforeEach(() => { |
| 24 | + jest.resetAllMocks() |
| 25 | + }) |
| 26 | + |
| 27 | + describe('getLatestCljFmt', () => { |
| 28 | + it('uses tag_name as latest version', async () => { |
| 29 | + getJson.mockResolvedValueOnce({ |
| 30 | + result: {tag_name: '1.2.3'} |
| 31 | + }) |
| 32 | + const res = await cljfmt.getLatestCljFmt() |
| 33 | + expect(res).toBe('1.2.3') |
| 34 | + expect(getJson).toHaveBeenCalledWith( |
| 35 | + 'https://api.github.com/repos/weavejester/cljfmt/releases/latest', |
| 36 | + undefined |
| 37 | + ) |
| 38 | + }) |
| 39 | + |
| 40 | + it('supports authorization', async () => { |
| 41 | + getJson.mockResolvedValueOnce({ |
| 42 | + result: {tag_name: '1.2.3'} |
| 43 | + }) |
| 44 | + const res = await cljfmt.getLatestCljFmt('token 123') |
| 45 | + expect(res).toBe('1.2.3') |
| 46 | + expect(getJson).toHaveBeenCalledWith( |
| 47 | + 'https://api.github.com/repos/weavejester/cljfmt/releases/latest', |
| 48 | + {Authorization: 'token 123'} |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + it('throws on http client error', async () => { |
| 53 | + getJson.mockRejectedValueOnce(new Error('some error')) |
| 54 | + await expect(cljfmt.getLatestCljFmt()).rejects.toThrow('some error') |
| 55 | + }) |
| 56 | + |
| 57 | + it('throws on wrong client answer', async () => { |
| 58 | + getJson.mockResolvedValueOnce({result: {foo: 'bar'}}) |
| 59 | + await expect(cljfmt.getLatestCljFmt()).rejects.toThrow( |
| 60 | + `Can't obtain latest cljfmt version` |
| 61 | + ) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe('getArtifactName', () => { |
| 66 | + test.each` |
| 67 | + platform | artifact |
| 68 | + ${'win32'} | ${`cljfmt-1.2.3-win-amd64.zip`} |
| 69 | + ${'darwin'} | ${`cljfmt-1.2.3-darwin-amd64.tar.gz`} |
| 70 | + ${'linux'} | ${`cljfmt-1.2.3-linux-amd64.tar.gz`} |
| 71 | + ${'foobar'} | ${`cljfmt-1.2.3-linux-amd64.tar.gz`} |
| 72 | + `('$platform -> $artifact', ({platform, artifact}) => { |
| 73 | + os.platform.mockReturnValueOnce(platform as never) |
| 74 | + expect(cljfmt.getArtifactName('1.2.3')).toBe(artifact) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('getArtifactUrl', () => { |
| 79 | + test.each` |
| 80 | + platform | artifact |
| 81 | + ${'win32'} | ${`cljfmt-1.2.3-win-amd64.zip`} |
| 82 | + ${'darwin'} | ${`cljfmt-1.2.3-darwin-amd64.tar.gz`} |
| 83 | + ${'linux'} | ${`cljfmt-1.2.3-linux-amd64.tar.gz`} |
| 84 | + ${'foobar'} | ${`cljfmt-1.2.3-linux-amd64.tar.gz`} |
| 85 | + `('$platform -> $artifact', ({platform, artifact}) => { |
| 86 | + os.platform.mockReturnValueOnce(platform as never) |
| 87 | + expect(cljfmt.getArtifactUrl('1.2.3')).toBe( |
| 88 | + `https://github.com/weavejester/cljfmt/releases/download/1.2.3/${artifact}` |
| 89 | + ) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + describe('setup', () => { |
| 94 | + it('uses cache', async () => { |
| 95 | + tc.find.mockReturnValueOnce('/foo/bar') |
| 96 | + |
| 97 | + await cljfmt.setup('1.2.3') |
| 98 | + |
| 99 | + expect(tc.find).toHaveBeenCalledWith('cljfmt', '1.2.3') |
| 100 | + expect(core.addPath).toHaveBeenCalledWith('/foo/bar') |
| 101 | + }) |
| 102 | + |
| 103 | + it('uses cache', async () => { |
| 104 | + tc.find.mockReturnValueOnce('/foo/bar') |
| 105 | + |
| 106 | + await cljfmt.setup('1.2.3') |
| 107 | + |
| 108 | + expect(tc.find).toHaveBeenCalledWith('cljfmt', '1.2.3') |
| 109 | + expect(core.addPath).toHaveBeenCalledWith('/foo/bar') |
| 110 | + }) |
| 111 | + |
| 112 | + it('fetches exact version', async () => { |
| 113 | + tc.downloadTool.mockResolvedValueOnce('/foo/cljfmt.tar.gz') |
| 114 | + tc.extractTar.mockResolvedValueOnce('/bar/baz') |
| 115 | + tc.cacheDir.mockResolvedValueOnce('/qux') |
| 116 | + |
| 117 | + await cljfmt.setup('1.2.3', 'token 123') |
| 118 | + |
| 119 | + expect(tc.find).toHaveBeenCalledWith('cljfmt', '1.2.3') |
| 120 | + expect(tc.downloadTool).toHaveBeenCalledWith( |
| 121 | + 'https://github.com/weavejester/cljfmt/releases/download/1.2.3/cljfmt-1.2.3-linux-amd64.tar.gz', |
| 122 | + undefined, |
| 123 | + 'token 123' |
| 124 | + ) |
| 125 | + expect(tc.cacheDir).toHaveBeenCalledWith('/bar/baz', 'cljfmt', '1.2.3') |
| 126 | + expect(core.addPath).toHaveBeenCalledWith('/qux') |
| 127 | + }) |
| 128 | + |
| 129 | + it('fetches latest version', async () => { |
| 130 | + getJson.mockResolvedValueOnce({ |
| 131 | + result: {tag_name: '9.9.9'} |
| 132 | + }) |
| 133 | + tc.downloadTool.mockResolvedValueOnce('/foo/cljfmt.tar.gz') |
| 134 | + tc.extractTar.mockResolvedValueOnce('/bar/baz') |
| 135 | + tc.cacheDir.mockResolvedValueOnce('/qux') |
| 136 | + |
| 137 | + await cljfmt.setup('latest', 'token 123') |
| 138 | + |
| 139 | + expect(getJson).toHaveBeenCalledWith( |
| 140 | + 'https://api.github.com/repos/weavejester/cljfmt/releases/latest', |
| 141 | + {Authorization: 'token 123'} |
| 142 | + ) |
| 143 | + expect(tc.find).toHaveBeenCalledWith('cljfmt', '9.9.9') |
| 144 | + expect(tc.downloadTool).toHaveBeenCalledWith( |
| 145 | + 'https://github.com/weavejester/cljfmt/releases/download/9.9.9/cljfmt-9.9.9-linux-amd64.tar.gz', |
| 146 | + undefined, |
| 147 | + 'token 123' |
| 148 | + ) |
| 149 | + expect(tc.cacheDir).toHaveBeenCalledWith('/bar/baz', 'cljfmt', '9.9.9') |
| 150 | + expect(core.addPath).toHaveBeenCalledWith('/qux') |
| 151 | + }) |
| 152 | + }) |
| 153 | +}) |
0 commit comments