|
1 | 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
2 | 2 | import { CustomUpdater } from '../../src/updaters/customUpdater'; |
3 | 3 | import { FileHandler, ManifestParser } from '../../src/utils'; |
4 | | -import * as core from '@actions/core'; |
5 | 4 |
|
6 | 5 | vi.mock('../../src/utils/fileHandler'); |
7 | 6 | vi.mock('../../src/utils/manifestParser'); |
8 | | -vi.mock('@actions/core'); |
9 | 7 |
|
10 | 8 | describe('CustomUpdater', () => { |
11 | | - let customUpdater: CustomUpdater; |
12 | | - let mockFileHandler: vi.Mocked<FileHandler>; |
13 | | - let mockManifestParser: vi.Mocked<ManifestParser>; |
14 | | - |
15 | | - const filePath = 'test.txt'; |
16 | | - const variableName = 'APP_VERSION'; |
| 9 | + let fileHandler: FileHandler; |
| 10 | + let manifestParser: ManifestParser; |
17 | 11 |
|
18 | 12 | beforeEach(() => { |
19 | | - mockFileHandler = new FileHandler() as vi.Mocked<FileHandler>; |
20 | | - mockManifestParser = new ManifestParser(mockFileHandler) as vi.Mocked<ManifestParser>; |
21 | | - customUpdater = new CustomUpdater(filePath, variableName); |
22 | | - |
23 | | - // Mock the constructor's internal assignments |
24 | | - (customUpdater as unknown).fileHandler = mockFileHandler; |
25 | | - (customUpdater as unknown).manifestParser = mockManifestParser; |
| 13 | + fileHandler = new FileHandler(); |
| 14 | + manifestParser = new ManifestParser(fileHandler); |
26 | 15 | }); |
27 | 16 |
|
28 | | - it('should always return true for canHandle', () => { |
29 | | - expect(customUpdater.canHandle()).toBe(true); |
| 17 | + it('should always be able to handle the request', () => { |
| 18 | + const updater = new CustomUpdater('dummy.txt', 'version'); |
| 19 | + expect(updater.canHandle()).toBe(true); |
30 | 20 | }); |
31 | 21 |
|
32 | | - describe('getCurrentVersion', () => { |
33 | | - it('should return the current version if found', () => { |
34 | | - mockManifestParser.getVersion.mockReturnValue('1.0.0'); |
35 | | - expect(customUpdater.getCurrentVersion()).toBe('1.0.0'); |
36 | | - expect(mockManifestParser.getVersion).toHaveBeenCalledWith( |
37 | | - filePath, |
38 | | - 'regex', |
39 | | - expect.objectContaining({ regex: expect.any(RegExp) }), |
40 | | - ); |
41 | | - }); |
42 | | - |
43 | | - it('should return null if version is not found', () => { |
44 | | - mockManifestParser.getVersion.mockReturnValue(null); |
45 | | - expect(customUpdater.getCurrentVersion()).toBeNull(); |
46 | | - }); |
47 | | - |
48 | | - it('should return null and log debug message if manifestParser.getVersion throws an error', () => { |
49 | | - const error = new Error('Parse error'); |
50 | | - mockManifestParser.getVersion.mockImplementation(() => { |
51 | | - throw error; |
52 | | - }); |
53 | | - const coreDebugSpy = vi.spyOn(core, 'debug').mockImplementation(() => {}); |
54 | | - |
55 | | - expect(customUpdater.getCurrentVersion()).toBeNull(); |
56 | | - expect(coreDebugSpy).toHaveBeenCalledWith( |
57 | | - `Could not read or parse version from ${filePath}: ${error}`, |
58 | | - ); |
59 | | - }); |
| 22 | + it('should get the current version from a custom file', () => { |
| 23 | + const updater = new CustomUpdater('dummy.txt', 'version'); |
| 24 | + updater['manifestParser'] = manifestParser; |
| 25 | + const getVersionSpy = vi.spyOn(manifestParser, 'getVersion').mockReturnValue('1.2.3'); |
| 26 | + const version = updater.getCurrentVersion(); |
| 27 | + expect(getVersionSpy).toHaveBeenCalled(); |
| 28 | + expect(version).toBe('1.2.3'); |
60 | 29 | }); |
61 | 30 |
|
62 | | - describe('bumpVersion', () => { |
63 | | - it('should successfully bump the version', () => { |
64 | | - vi.spyOn(customUpdater, 'getCurrentVersion').mockReturnValue('1.0.0'); |
65 | | - mockManifestParser.updateVersion.mockReturnValue(undefined); |
66 | | - const coreInfoSpy = vi.spyOn(core, 'info').mockImplementation(() => {}); |
67 | | - |
68 | | - const newVersion = customUpdater.bumpVersion('patch'); |
69 | | - |
70 | | - expect(newVersion).toBe('1.0.1'); |
71 | | - expect(mockManifestParser.updateVersion).toHaveBeenCalledWith( |
72 | | - filePath, |
73 | | - '1.0.1', |
74 | | - 'regex', |
75 | | - expect.objectContaining({ regexReplace: expect.any(RegExp) }), |
76 | | - ); |
77 | | - expect(coreInfoSpy).toHaveBeenCalledWith( |
78 | | - `Bumped ${variableName} in ${filePath} from 1.0.0 to 1.0.1`, |
79 | | - ); |
80 | | - }); |
| 31 | + it('should bump the version in a custom file', () => { |
| 32 | + const updater = new CustomUpdater('dummy.txt', 'version'); |
| 33 | + updater['manifestParser'] = manifestParser; |
| 34 | + vi.spyOn(manifestParser, 'getVersion').mockReturnValue('1.2.3'); |
| 35 | + const updateVersionSpy = vi.spyOn(manifestParser, 'updateVersion'); |
| 36 | + const newVersion = updater.bumpVersion('patch'); |
| 37 | + expect(newVersion).toBe('1.2.4'); |
| 38 | + expect(updateVersionSpy).toHaveBeenCalled(); |
| 39 | + }); |
81 | 40 |
|
82 | | - it('should throw an error if current version is not found', () => { |
83 | | - vi.spyOn(customUpdater, 'getCurrentVersion').mockReturnValue(null); |
| 41 | + it('should return null if the version is not found', () => { |
| 42 | + const updater = new CustomUpdater('dummy.txt', 'version'); |
| 43 | + updater['manifestParser'] = manifestParser; |
| 44 | + vi.spyOn(manifestParser, 'getVersion').mockReturnValue(null); |
| 45 | + expect(updater.getCurrentVersion()).toBeNull(); |
| 46 | + }); |
84 | 47 |
|
85 | | - expect(() => customUpdater.bumpVersion('patch')).toThrow( |
86 | | - `Could not find current version for variable '${variableName}' in file '${filePath}'`, |
87 | | - ); |
88 | | - }); |
| 48 | + it('should throw an error when bumping if the version is not found', () => { |
| 49 | + const updater = new CustomUpdater('dummy.txt', 'version'); |
| 50 | + updater['manifestParser'] = manifestParser; |
| 51 | + vi.spyOn(manifestParser, 'getVersion').mockReturnValue(null); |
| 52 | + expect(() => updater.bumpVersion('patch')).toThrow( |
| 53 | + "Could not find current version for variable 'version' in file 'dummy.txt'", |
| 54 | + ); |
89 | 55 | }); |
90 | 56 | }); |
0 commit comments