|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:max-func-body-length no-any |
| 7 | + |
| 8 | +import { expect } from 'chai'; |
| 9 | +import * as path from 'path'; |
| 10 | +import * as TypeMoq from 'typemoq'; |
| 11 | +import { IPlatformService } from '../../client/common/platform/types'; |
| 12 | +import { ICurrentProcess, IPathUtils } from '../../client/common/types'; |
| 13 | +import { IKnownSearchPathsForInterpreters } from '../../client/interpreter/contracts'; |
| 14 | +import { KnownSearchPathsForInterpreters } from '../../client/interpreter/locators/services/KnownPathsService'; |
| 15 | +import { IServiceContainer } from '../../client/ioc/types'; |
| 16 | + |
| 17 | +suite('Interpreters Known Paths', () => { |
| 18 | + let serviceContainer: TypeMoq.IMock<IServiceContainer>; |
| 19 | + let currentProcess: TypeMoq.IMock<ICurrentProcess>; |
| 20 | + let platformService: TypeMoq.IMock<IPlatformService>; |
| 21 | + let pathUtils: TypeMoq.IMock<IPathUtils>; |
| 22 | + let knownSearchPaths: IKnownSearchPathsForInterpreters; |
| 23 | + |
| 24 | + setup(async () => { |
| 25 | + serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>(); |
| 26 | + currentProcess = TypeMoq.Mock.ofType<ICurrentProcess>(); |
| 27 | + platformService = TypeMoq.Mock.ofType<IPlatformService>(); |
| 28 | + pathUtils = TypeMoq.Mock.ofType<IPathUtils>(); |
| 29 | + serviceContainer.setup(c => c.get(TypeMoq.It.isValue(ICurrentProcess), TypeMoq.It.isAny())).returns(() => currentProcess.object); |
| 30 | + serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IPlatformService), TypeMoq.It.isAny())).returns(() => platformService.object); |
| 31 | + serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IPathUtils), TypeMoq.It.isAny())).returns(() => pathUtils.object); |
| 32 | + |
| 33 | + knownSearchPaths = new KnownSearchPathsForInterpreters(serviceContainer.object); |
| 34 | + }); |
| 35 | + |
| 36 | + test('Ensure known list of paths are returned', async () => { |
| 37 | + const pathDelimiter = 'X'; |
| 38 | + const pathsInPATHVar = [path.join('a', 'b', 'c'), '', path.join('1', '2'), '3']; |
| 39 | + pathUtils.setup(p => p.delimiter).returns(() => pathDelimiter); |
| 40 | + platformService.setup(p => p.isWindows).returns(() => true); |
| 41 | + platformService.setup(p => p.pathVariableName).returns(() => 'PATH'); |
| 42 | + currentProcess.setup(p => p.env).returns(() => { |
| 43 | + return { PATH: pathsInPATHVar.join(pathDelimiter) }; |
| 44 | + }); |
| 45 | + |
| 46 | + const expectedPaths = [...pathsInPATHVar].filter(item => item.length > 0); |
| 47 | + |
| 48 | + const paths = knownSearchPaths.getSearchPaths(); |
| 49 | + |
| 50 | + expect(paths).to.deep.equal(expectedPaths); |
| 51 | + }); |
| 52 | + |
| 53 | + test('Ensure known list of paths are returned on non-windows', async () => { |
| 54 | + const homeDir = '/users/peter Smith'; |
| 55 | + const pathDelimiter = 'X'; |
| 56 | + pathUtils.setup(p => p.delimiter).returns(() => pathDelimiter); |
| 57 | + pathUtils.setup(p => p.home).returns(() => homeDir); |
| 58 | + platformService.setup(p => p.isWindows).returns(() => false); |
| 59 | + platformService.setup(p => p.pathVariableName).returns(() => 'PATH'); |
| 60 | + currentProcess.setup(p => p.env).returns(() => { |
| 61 | + return { PATH: '' }; |
| 62 | + }); |
| 63 | + |
| 64 | + const expectedPaths: string[] = []; |
| 65 | + ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/sbin'] |
| 66 | + .forEach(p => { |
| 67 | + expectedPaths.push(p); |
| 68 | + expectedPaths.push(path.join(homeDir, p)); |
| 69 | + }); |
| 70 | + |
| 71 | + expectedPaths.push(path.join(homeDir, 'anaconda', 'bin')); |
| 72 | + expectedPaths.push(path.join(homeDir, 'python', 'bin')); |
| 73 | + |
| 74 | + const paths = knownSearchPaths.getSearchPaths(); |
| 75 | + |
| 76 | + expect(paths).to.deep.equal(expectedPaths); |
| 77 | + }); |
| 78 | + |
| 79 | + test('Ensure PATH variable and known list of paths are merged on non-windows', async () => { |
| 80 | + const homeDir = '/users/peter Smith'; |
| 81 | + const pathDelimiter = 'X'; |
| 82 | + const pathsInPATHVar = [path.join('a', 'b', 'c'), '', path.join('1', '2'), '3']; |
| 83 | + pathUtils.setup(p => p.delimiter).returns(() => pathDelimiter); |
| 84 | + pathUtils.setup(p => p.home).returns(() => homeDir); |
| 85 | + platformService.setup(p => p.isWindows).returns(() => false); |
| 86 | + platformService.setup(p => p.pathVariableName).returns(() => 'PATH'); |
| 87 | + currentProcess.setup(p => p.env).returns(() => { |
| 88 | + return { PATH: pathsInPATHVar.join(pathDelimiter) }; |
| 89 | + }); |
| 90 | + |
| 91 | + const expectedPaths = [...pathsInPATHVar].filter(item => item.length > 0); |
| 92 | + ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/sbin'] |
| 93 | + .forEach(p => { |
| 94 | + expectedPaths.push(p); |
| 95 | + expectedPaths.push(path.join(homeDir, p)); |
| 96 | + }); |
| 97 | + |
| 98 | + expectedPaths.push(path.join(homeDir, 'anaconda', 'bin')); |
| 99 | + expectedPaths.push(path.join(homeDir, 'python', 'bin')); |
| 100 | + |
| 101 | + const paths = knownSearchPaths.getSearchPaths(); |
| 102 | + |
| 103 | + expect(paths).to.deep.equal(expectedPaths); |
| 104 | + }); |
| 105 | +}); |
0 commit comments