|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import {cacheDependencies} from '../src/setup-python'; |
| 5 | +import {getCacheDistributor} from '../src/cache-distributions/cache-factory'; |
| 6 | + |
| 7 | +jest.mock('fs', () => { |
| 8 | + const actualFs = jest.requireActual('fs'); |
| 9 | + return { |
| 10 | + ...actualFs, |
| 11 | + promises: { |
| 12 | + access: jest.fn(), |
| 13 | + mkdir: jest.fn(), |
| 14 | + copyFile: jest.fn(), |
| 15 | + writeFile: jest.fn(), |
| 16 | + appendFile: jest.fn() |
| 17 | + } |
| 18 | + }; |
| 19 | +}); |
| 20 | +jest.mock('@actions/core'); |
| 21 | +jest.mock('../src/cache-distributions/cache-factory'); |
| 22 | + |
| 23 | +const mockedFsPromises = fs.promises as jest.Mocked<typeof fs.promises>; |
| 24 | +const mockedCore = core as jest.Mocked<typeof core>; |
| 25 | +const mockedGetCacheDistributor = getCacheDistributor as jest.Mock; |
| 26 | + |
| 27 | +describe('cacheDependencies', () => { |
| 28 | + const mockRestoreCache = jest.fn(); |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + jest.clearAllMocks(); |
| 32 | + process.env.GITHUB_ACTION_PATH = '/github/action'; |
| 33 | + process.env.GITHUB_WORKSPACE = '/github/workspace'; |
| 34 | + |
| 35 | + mockedCore.getInput.mockReturnValue('nested/deps.lock'); |
| 36 | + |
| 37 | + // Simulate file exists by resolving access without error |
| 38 | + mockedFsPromises.access.mockImplementation(async p => { |
| 39 | + const pathStr = typeof p === 'string' ? p : p.toString(); |
| 40 | + if (pathStr === '/github/action/nested/deps.lock') { |
| 41 | + return Promise.resolve(); |
| 42 | + } |
| 43 | + // Simulate directory doesn't exist to test mkdir |
| 44 | + if (pathStr === path.dirname('/github/workspace/nested/deps.lock')) { |
| 45 | + return Promise.reject(new Error('no dir')); |
| 46 | + } |
| 47 | + return Promise.resolve(); |
| 48 | + }); |
| 49 | + |
| 50 | + // Simulate mkdir success |
| 51 | + mockedFsPromises.mkdir.mockResolvedValue(undefined); |
| 52 | + |
| 53 | + // Simulate copyFile success |
| 54 | + mockedFsPromises.copyFile.mockResolvedValue(undefined); |
| 55 | + |
| 56 | + mockedGetCacheDistributor.mockReturnValue({restoreCache: mockRestoreCache}); |
| 57 | + }); |
| 58 | + |
| 59 | + it('copies the dependency file and resolves the path with directory structure', async () => { |
| 60 | + await cacheDependencies('pip', '3.12'); |
| 61 | + |
| 62 | + const sourcePath = path.resolve('/github/action', 'nested/deps.lock'); |
| 63 | + const targetPath = path.resolve('/github/workspace', 'nested/deps.lock'); |
| 64 | + |
| 65 | + expect(mockedFsPromises.access).toHaveBeenCalledWith( |
| 66 | + sourcePath, |
| 67 | + fs.constants.F_OK |
| 68 | + ); |
| 69 | + expect(mockedFsPromises.mkdir).toHaveBeenCalledWith( |
| 70 | + path.dirname(targetPath), |
| 71 | + { |
| 72 | + recursive: true |
| 73 | + } |
| 74 | + ); |
| 75 | + expect(mockedFsPromises.copyFile).toHaveBeenCalledWith( |
| 76 | + sourcePath, |
| 77 | + targetPath |
| 78 | + ); |
| 79 | + expect(mockedCore.info).toHaveBeenCalledWith( |
| 80 | + `Copied ${sourcePath} to ${targetPath}` |
| 81 | + ); |
| 82 | + expect(mockedCore.info).toHaveBeenCalledWith( |
| 83 | + `Resolved cache-dependency-path: nested/deps.lock` |
| 84 | + ); |
| 85 | + expect(mockRestoreCache).toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('warns if the dependency file does not exist', async () => { |
| 89 | + // Simulate file does not exist by rejecting access |
| 90 | + mockedFsPromises.access.mockRejectedValue(new Error('file not found')); |
| 91 | + |
| 92 | + await cacheDependencies('pip', '3.12'); |
| 93 | + |
| 94 | + expect(mockedCore.warning).toHaveBeenCalledWith( |
| 95 | + expect.stringContaining('does not exist') |
| 96 | + ); |
| 97 | + expect(mockedFsPromises.copyFile).not.toHaveBeenCalled(); |
| 98 | + expect(mockRestoreCache).toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('warns if file copy fails', async () => { |
| 102 | + // Simulate copyFile failure |
| 103 | + mockedFsPromises.copyFile.mockRejectedValue(new Error('copy failed')); |
| 104 | + |
| 105 | + await cacheDependencies('pip', '3.12'); |
| 106 | + |
| 107 | + expect(mockedCore.warning).toHaveBeenCalledWith( |
| 108 | + expect.stringContaining('Failed to copy file') |
| 109 | + ); |
| 110 | + expect(mockRestoreCache).toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('skips path logic if no input is provided', async () => { |
| 114 | + mockedCore.getInput.mockReturnValue(''); |
| 115 | + |
| 116 | + await cacheDependencies('pip', '3.12'); |
| 117 | + |
| 118 | + expect(mockedFsPromises.copyFile).not.toHaveBeenCalled(); |
| 119 | + expect(mockedCore.warning).not.toHaveBeenCalled(); |
| 120 | + expect(mockRestoreCache).toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('does not copy if dependency file is already inside the workspace but still sets resolved path', async () => { |
| 124 | + // Simulate cacheDependencyPath inside workspace |
| 125 | + mockedCore.getInput.mockReturnValue('deps.lock'); |
| 126 | + |
| 127 | + // Override sourcePath and targetPath to be equal |
| 128 | + const actionPath = '/github/workspace'; // same path for action and workspace |
| 129 | + process.env.GITHUB_ACTION_PATH = actionPath; |
| 130 | + process.env.GITHUB_WORKSPACE = actionPath; |
| 131 | + |
| 132 | + // access resolves to simulate file exists |
| 133 | + mockedFsPromises.access.mockResolvedValue(); |
| 134 | + |
| 135 | + await cacheDependencies('pip', '3.12'); |
| 136 | + |
| 137 | + const sourcePath = path.resolve(actionPath, 'deps.lock'); |
| 138 | + const targetPath = sourcePath; // same path |
| 139 | + |
| 140 | + expect(mockedFsPromises.copyFile).not.toHaveBeenCalled(); |
| 141 | + expect(mockedCore.info).toHaveBeenCalledWith( |
| 142 | + `Dependency file is already inside the workspace: ${sourcePath}` |
| 143 | + ); |
| 144 | + expect(mockedCore.info).toHaveBeenCalledWith( |
| 145 | + `Resolved cache-dependency-path: deps.lock` |
| 146 | + ); |
| 147 | + expect(mockRestoreCache).toHaveBeenCalled(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments