|
| 1 | +import { extensions } from 'vscode' |
| 2 | +import { |
| 3 | + getPythonBinPath, |
| 4 | + getOnDidChangePythonExecutionDetails, |
| 5 | + VscodePython |
| 6 | +} from './python' |
| 7 | +import { executeProcess } from '../processExecution' |
| 8 | + |
| 9 | +jest.mock('vscode') |
| 10 | +jest.mock('../processExecution') |
| 11 | + |
| 12 | +const mockedExecuteProcess = jest.mocked(executeProcess) |
| 13 | + |
| 14 | +const mockedExtensions = jest.mocked(extensions) |
| 15 | +const mockedGetExtension = jest.fn() |
| 16 | +mockedExtensions.getExtension = mockedGetExtension |
| 17 | + |
| 18 | +const mockedReady = jest.fn() |
| 19 | +const mockedOnDidChangeExecutionDetails = jest.fn() |
| 20 | +let mockedExecCommand: string[] | undefined |
| 21 | + |
| 22 | +const mockedSettings = { |
| 23 | + getExecutionDetails: () => ({ |
| 24 | + execCommand: mockedExecCommand |
| 25 | + }), |
| 26 | + onDidChangeExecutionDetails: mockedOnDidChangeExecutionDetails |
| 27 | +} |
| 28 | + |
| 29 | +const mockedVscodePythonAPI = { |
| 30 | + ready: mockedReady, |
| 31 | + settings: mockedSettings |
| 32 | +} as unknown as VscodePython |
| 33 | + |
| 34 | +const mockedVscodePython = { |
| 35 | + activate: () => Promise.resolve(mockedVscodePythonAPI) |
| 36 | +} |
| 37 | + |
| 38 | +beforeEach(() => { |
| 39 | + jest.resetAllMocks() |
| 40 | + mockedGetExtension.mockReturnValueOnce(mockedVscodePython) |
| 41 | +}) |
| 42 | + |
| 43 | +describe('getPythonBinPath', () => { |
| 44 | + const mockedPythonBinPath = '/some/path/to/python' |
| 45 | + mockedExecCommand = [mockedPythonBinPath] |
| 46 | + |
| 47 | + it('should return the python path even if the python ready promise rejects', async () => { |
| 48 | + mockedReady.mockRejectedValueOnce(undefined) |
| 49 | + mockedExecuteProcess.mockResolvedValueOnce(mockedPythonBinPath) |
| 50 | + |
| 51 | + const pythonBinPath = await getPythonBinPath() |
| 52 | + |
| 53 | + expect(pythonBinPath).toStrictEqual(mockedPythonBinPath) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should return the python path if the python extension initializes as expected', async () => { |
| 57 | + mockedReady.mockResolvedValueOnce(undefined) |
| 58 | + mockedExecuteProcess.mockResolvedValueOnce(mockedPythonBinPath) |
| 59 | + |
| 60 | + const pythonBinPath = await getPythonBinPath() |
| 61 | + |
| 62 | + expect(pythonBinPath).toStrictEqual(mockedPythonBinPath) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +describe('getOnDidChangePythonExecutionDetails', () => { |
| 67 | + it('should return the listener if the python ready promise rejects', async () => { |
| 68 | + mockedReady.mockRejectedValueOnce(undefined) |
| 69 | + |
| 70 | + const listener = await getOnDidChangePythonExecutionDetails() |
| 71 | + |
| 72 | + expect(listener).toBe(mockedOnDidChangeExecutionDetails) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should return the listener if the python extension initializes as expected', async () => { |
| 76 | + mockedReady.mockResolvedValueOnce(undefined) |
| 77 | + |
| 78 | + const listener = await getOnDidChangePythonExecutionDetails() |
| 79 | + |
| 80 | + expect(listener).toBe(mockedOnDidChangeExecutionDetails) |
| 81 | + }) |
| 82 | +}) |
0 commit comments