|
| 1 | +import { log } from '@clack/prompts' |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { getVersion } from '../src/utils/get-version' |
| 4 | +import { initScriptVersionAnchor } from '../src/utils/init-script-version-anchor' |
| 5 | +import { validateVersion } from '../src/utils/validate-version' |
| 6 | + |
| 7 | +vi.mock('../src/utils/get-version') |
| 8 | +vi.mock('../src/utils/validate-version') |
| 9 | +vi.mock('@clack/prompts', () => ({ |
| 10 | + log: { |
| 11 | + warn: vi.fn(), |
| 12 | + }, |
| 13 | +})) |
| 14 | + |
| 15 | +describe('initScriptVersionAnchor', () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.resetAllMocks() |
| 18 | + }) |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + vi.clearAllMocks() |
| 22 | + }) |
| 23 | + |
| 24 | + it('should return early if no required version is provided', async () => { |
| 25 | + await initScriptVersionAnchor() |
| 26 | + expect(getVersion).not.toHaveBeenCalled() |
| 27 | + expect(validateVersion).not.toHaveBeenCalled() |
| 28 | + expect(log.warn).not.toHaveBeenCalled() |
| 29 | + }) |
| 30 | + |
| 31 | + it('should log warning if anchor version is not found', async () => { |
| 32 | + const required = '1.0.0' |
| 33 | + vi.mocked(getVersion).mockReturnValue(undefined) |
| 34 | + vi.mocked(validateVersion).mockReturnValue({ valid: false, version: undefined }) |
| 35 | + await initScriptVersionAnchor(required) |
| 36 | + expect(getVersion).toHaveBeenCalledWith('anchor') |
| 37 | + expect(validateVersion).toHaveBeenCalledWith({ required, version: undefined }) |
| 38 | + expect(log.warn).toHaveBeenCalledWith( |
| 39 | + expect.stringContaining('Could not find Anchor version. Please install Anchor.'), |
| 40 | + ) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should log warning if anchor version does not satisfy the requirement', async () => { |
| 44 | + const required = '1.0.0' |
| 45 | + const version = '0.9.0' |
| 46 | + vi.mocked(getVersion).mockReturnValue(version) |
| 47 | + vi.mocked(validateVersion).mockReturnValue({ valid: false, version }) |
| 48 | + await initScriptVersionAnchor(required) |
| 49 | + expect(getVersion).toHaveBeenCalledWith('anchor') |
| 50 | + expect(validateVersion).toHaveBeenCalledWith({ required, version }) |
| 51 | + expect(log.warn).toHaveBeenCalledWith( |
| 52 | + expect.stringContaining(`Found Anchor version ${version}. Expected Anchor version ${required}.`), |
| 53 | + ) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should not log warning if anchor version satisfies the requirement', async () => { |
| 57 | + const required = '1.0.0' |
| 58 | + const version = '1.0.0' |
| 59 | + vi.mocked(getVersion).mockReturnValue(version) |
| 60 | + vi.mocked(validateVersion).mockReturnValue({ valid: true, version }) |
| 61 | + await initScriptVersionAnchor(required) |
| 62 | + expect(getVersion).toHaveBeenCalledWith('anchor') |
| 63 | + expect(validateVersion).toHaveBeenCalledWith({ required, version }) |
| 64 | + expect(log.warn).not.toHaveBeenCalled() |
| 65 | + }) |
| 66 | + |
| 67 | + it('should log verbose message if verbose is true', async () => { |
| 68 | + const required = '1.0.0' |
| 69 | + const version = '1.0.0' |
| 70 | + vi.mocked(getVersion).mockReturnValue(version) |
| 71 | + vi.mocked(validateVersion).mockReturnValue({ valid: true, version }) |
| 72 | + await initScriptVersionAnchor(required, true) |
| 73 | + expect(getVersion).toHaveBeenCalledWith('anchor') |
| 74 | + expect(validateVersion).toHaveBeenCalledWith({ required, version }) |
| 75 | + expect(log.warn).toHaveBeenCalledWith( |
| 76 | + `initScriptVersionAnchor: required: ${required}, version: ${version}, valid: true`, |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should log error if an exception occurs', async () => { |
| 81 | + const required = '1.0.0' |
| 82 | + const error = new Error('Test error') |
| 83 | + vi.mocked(getVersion).mockImplementation(() => { |
| 84 | + throw error |
| 85 | + }) |
| 86 | + await initScriptVersionAnchor(required) |
| 87 | + expect(log.warn).toHaveBeenCalledWith(`Error ${error}`) |
| 88 | + }) |
| 89 | +}) |
0 commit comments