|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import * as core from '@actions/core' |
| 10 | +import * as tc from '@actions/tool-cache' |
| 11 | +import os from 'os' |
10 | 12 | import * as main from '../src/main' |
11 | 13 |
|
12 | | -// Mock the action's main function |
| 14 | +// // Mock the action's main function |
13 | 15 | const runMock = jest.spyOn(main, 'run') |
14 | 16 |
|
15 | | -// Other utilities |
16 | | -const timeRegex = /^\d{2}:\d{2}:\d{2}/ |
| 17 | +jest.mock('os') |
17 | 18 |
|
18 | | -// Mock the GitHub Actions core library |
19 | | -let debugMock: jest.SpyInstance |
20 | 19 | let errorMock: jest.SpyInstance |
21 | 20 | let getInputMock: jest.SpyInstance |
22 | | -let setFailedMock: jest.SpyInstance |
23 | | -let setOutputMock: jest.SpyInstance |
| 21 | +let addPathMock: jest.SpyInstance |
| 22 | +let downloadToolMock: jest.SpyInstance |
| 23 | +let extractTarMock: jest.SpyInstance |
24 | 24 |
|
25 | 25 | describe('action', () => { |
26 | 26 | beforeEach(() => { |
27 | 27 | jest.clearAllMocks() |
28 | 28 |
|
29 | | - debugMock = jest.spyOn(core, 'debug').mockImplementation() |
30 | | - errorMock = jest.spyOn(core, 'error').mockImplementation() |
31 | 29 | getInputMock = jest.spyOn(core, 'getInput').mockImplementation() |
32 | | - setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation() |
33 | | - setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation() |
| 30 | + addPathMock = jest.spyOn(core, 'addPath').mockImplementation() |
| 31 | + errorMock = jest.spyOn(core, 'setFailed').mockImplementation() |
| 32 | + downloadToolMock = jest.spyOn(tc, 'downloadTool').mockImplementation() |
| 33 | + extractTarMock = jest.spyOn(tc, 'extractTar').mockImplementation() |
| 34 | + |
| 35 | + os.platform = jest.fn().mockReturnValue('linux') |
| 36 | + os.arch = jest.fn().mockReturnValue('amd64') |
34 | 37 | }) |
35 | 38 |
|
36 | | - it('sets the time output', async () => { |
| 39 | + it('sets the version output', async () => { |
37 | 40 | // Set the action's inputs as return values from core.getInput() |
38 | 41 | getInputMock.mockImplementation((name: string): string => { |
39 | 42 | switch (name) { |
40 | | - case 'milliseconds': |
41 | | - return '500' |
| 43 | + case 'version': |
| 44 | + return 'v0.0.18' |
42 | 45 | default: |
43 | 46 | return '' |
44 | 47 | } |
45 | 48 | }) |
46 | 49 |
|
47 | | - await main.run() |
48 | | - expect(runMock).toHaveReturned() |
49 | | - |
50 | | - // Verify that all of the core library functions were called correctly |
51 | | - expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...') |
52 | | - expect(debugMock).toHaveBeenNthCalledWith( |
53 | | - 2, |
54 | | - expect.stringMatching(timeRegex) |
55 | | - ) |
56 | | - expect(debugMock).toHaveBeenNthCalledWith( |
57 | | - 3, |
58 | | - expect.stringMatching(timeRegex) |
59 | | - ) |
60 | | - expect(setOutputMock).toHaveBeenNthCalledWith( |
61 | | - 1, |
62 | | - 'time', |
63 | | - expect.stringMatching(timeRegex) |
64 | | - ) |
65 | | - expect(errorMock).not.toHaveBeenCalled() |
66 | | - }) |
67 | | - |
68 | | - it('sets a failed status', async () => { |
69 | | - // Set the action's inputs as return values from core.getInput() |
70 | | - getInputMock.mockImplementation((name: string): string => { |
71 | | - switch (name) { |
72 | | - case 'milliseconds': |
73 | | - return 'this is not a number' |
| 50 | + downloadToolMock.mockImplementation((dlUrl: string): string => { |
| 51 | + switch (dlUrl) { |
| 52 | + case 'https://github.com/nucleuscloud/neosync/releases/download/v0.0.18/neosync_0.0.18_linux_amd64.tar.gz': |
| 53 | + return 'fake-tar-path' |
| 54 | + default: |
| 55 | + return '' |
| 56 | + } |
| 57 | + }) |
| 58 | + extractTarMock.mockImplementation((tarPath: string): string => { |
| 59 | + switch (tarPath) { |
| 60 | + case 'fake-tar-path': |
| 61 | + return '/path/to/tarball' |
74 | 62 | default: |
75 | 63 | return '' |
76 | 64 | } |
77 | 65 | }) |
| 66 | + addPathMock.mockImplementation((cliPath: string): void => { |
| 67 | + switch (cliPath) { |
| 68 | + case '/path/to/tarball': |
| 69 | + return |
| 70 | + default: |
| 71 | + throw new Error('test: invalid cli path') |
| 72 | + } |
| 73 | + }) |
78 | 74 |
|
79 | 75 | await main.run() |
80 | 76 | expect(runMock).toHaveReturned() |
81 | | - |
82 | | - // Verify that all of the core library functions were called correctly |
83 | | - expect(setFailedMock).toHaveBeenNthCalledWith( |
84 | | - 1, |
85 | | - 'milliseconds not a number' |
86 | | - ) |
87 | 77 | expect(errorMock).not.toHaveBeenCalled() |
| 78 | + expect(downloadToolMock).toHaveBeenCalled() |
| 79 | + expect(extractTarMock).toHaveBeenCalled() |
| 80 | + expect(addPathMock).toHaveBeenCalled() |
88 | 81 | }) |
89 | 82 | }) |
0 commit comments