|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:no-any |
| 7 | + |
| 8 | +import { expect } from 'chai'; |
| 9 | +import * as path from 'path'; |
| 10 | +import * as typeMoq from 'typemoq'; |
| 11 | +import { IFileSystem } from '../../client/common/platform/types'; |
| 12 | +import { ExecutionResult, IProcessService, IProcessServiceFactory } from '../../client/common/process/types'; |
| 13 | +import { ExcutableValidator } from '../../client/debugger/executableValidator'; |
| 14 | +import { IServiceContainer } from '../../client/ioc/types'; |
| 15 | + |
| 16 | +suite('Debugger Executable Validator', () => { |
| 17 | + let fs: typeMoq.IMock<IFileSystem>; |
| 18 | + let processService: typeMoq.IMock<IProcessService>; |
| 19 | + let validator: ExcutableValidator; |
| 20 | + setup(() => { |
| 21 | + const serviceContainer = typeMoq.Mock.ofType<IServiceContainer>(); |
| 22 | + fs = typeMoq.Mock.ofType<IFileSystem>(undefined, typeMoq.MockBehavior.Strict); |
| 23 | + processService = typeMoq.Mock.ofType<IProcessService>(); |
| 24 | + processService.setup((p: any) => p.then).returns(() => undefined); |
| 25 | + const processFactory = typeMoq.Mock.ofType<IProcessServiceFactory>(); |
| 26 | + processFactory.setup(p => p.create()).returns(() => Promise.resolve(processService.object)); |
| 27 | + |
| 28 | + serviceContainer.setup(c => c.get(typeMoq.It.isValue(IFileSystem))).returns(() => fs.object); |
| 29 | + serviceContainer.setup(c => c.get(typeMoq.It.isValue(IProcessServiceFactory))).returns(() => processFactory.object); |
| 30 | + validator = new ExcutableValidator(serviceContainer.object); |
| 31 | + }); |
| 32 | + |
| 33 | + async function validate(pythonPath: string, expectedResult: boolean, fileExists: boolean, processOutput: ExecutionResult<string>) { |
| 34 | + fs.setup(f => f.fileExists(typeMoq.It.isValue(pythonPath))).returns(() => Promise.resolve(fileExists)); |
| 35 | + processService.setup(p => p.exec(typeMoq.It.isValue(pythonPath), typeMoq.It.isValue(['-c', 'print("1")']))) |
| 36 | + .returns(() => Promise.resolve(processOutput)) |
| 37 | + .verifiable(typeMoq.Times.once()); |
| 38 | + |
| 39 | + const isValid = await validator.validateExecutable(pythonPath); |
| 40 | + |
| 41 | + expect(isValid).to.be.equal(expectedResult, 'Incorrect value'); |
| 42 | + fs.verifyAll(); |
| 43 | + processService.verifyAll(); |
| 44 | + } |
| 45 | + test('Validate \'python\' command', async () => { |
| 46 | + const pythonPath = 'python'; |
| 47 | + const output = { stdout: '1' }; |
| 48 | + await validate(pythonPath, true, false, output); |
| 49 | + }); |
| 50 | + |
| 51 | + test('Validate \'python\' Executable with a valida path', async () => { |
| 52 | + const pythonPath = path.join('a', 'b', 'bin', 'python'); |
| 53 | + const output = { stdout: '1' }; |
| 54 | + await validate(pythonPath, true, true, output); |
| 55 | + }); |
| 56 | + |
| 57 | + test('Validate \'spark-submit\'', async () => { |
| 58 | + const pythonPath = path.join('a', 'b', 'bin', 'spark-submit'); |
| 59 | + const output = { stderr: 'ex', stdout: '' }; |
| 60 | + await validate(pythonPath, true, true, output); |
| 61 | + }); |
| 62 | + |
| 63 | + test('Validate invalid \'python\' command', async () => { |
| 64 | + const pythonPath = path.join('python'); |
| 65 | + const output = { stderr: 'ex', stdout: '' }; |
| 66 | + await validate(pythonPath, false, false, output); |
| 67 | + }); |
| 68 | + |
| 69 | + test('Validate invalid executable', async () => { |
| 70 | + const pythonPath = path.join('a', 'b', 'bin', 'spark-submit'); |
| 71 | + const output = { stderr: 'ex', stdout: '' }; |
| 72 | + await validate(pythonPath, false, false, output); |
| 73 | + }); |
| 74 | +}); |
0 commit comments