|
| 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 |
| 7 | + |
| 8 | +import { expect, use } from 'chai'; |
| 9 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 10 | +import * as path from 'path'; |
| 11 | +import { FileSystem } from '../../../client/common/platform/fileSystem'; |
| 12 | +import { PathUtils } from '../../../client/common/platform/pathUtils'; |
| 13 | +import { PlatformService } from '../../../client/common/platform/platformService'; |
| 14 | +import { IPathUtils } from '../../../client/common/types'; |
| 15 | +import { OSType } from '../../../client/common/utils/platform'; |
| 16 | +import { EnvironmentVariablesService } from '../../../client/common/variables/environment'; |
| 17 | +import { IEnvironmentVariablesService } from '../../../client/common/variables/types'; |
| 18 | +import { getOSType } from '../../common'; |
| 19 | + |
| 20 | +use(chaiAsPromised); |
| 21 | + |
| 22 | +const envFilesFolderPath = path.join(__dirname, '..', '..', '..', '..', 'src', 'testMultiRootWkspc', 'workspace4'); |
| 23 | + |
| 24 | +suite('Environment Variables Service', () => { |
| 25 | + let pathUtils: IPathUtils; |
| 26 | + let variablesService: IEnvironmentVariablesService; |
| 27 | + setup(() => { |
| 28 | + pathUtils = new PathUtils(getOSType() === OSType.Windows); |
| 29 | + const fs = new FileSystem(new PlatformService()); |
| 30 | + variablesService = new EnvironmentVariablesService(pathUtils, fs); |
| 31 | + }); |
| 32 | + |
| 33 | + suite('parseFile()', () => { |
| 34 | + test('Custom variables should be undefined with no argument', async () => { |
| 35 | + const vars = await variablesService.parseFile(undefined); |
| 36 | + expect(vars).to.equal(undefined, 'Variables should be undefined'); |
| 37 | + }); |
| 38 | + |
| 39 | + test('Custom variables should be undefined with non-existent files', async () => { |
| 40 | + const vars = await variablesService.parseFile(path.join(envFilesFolderPath, 'abcd')); |
| 41 | + expect(vars).to.equal(undefined, 'Variables should be undefined'); |
| 42 | + }); |
| 43 | + |
| 44 | + test('Custom variables should be undefined when folder name is passed instead of a file name', async () => { |
| 45 | + const vars = await variablesService.parseFile(envFilesFolderPath); |
| 46 | + expect(vars).to.equal(undefined, 'Variables should be undefined'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('Custom variables should be not undefined with a valid environment file', async () => { |
| 50 | + const vars = await variablesService.parseFile(path.join(envFilesFolderPath, '.env')); |
| 51 | + expect(vars).to.not.equal(undefined, 'Variables should be undefined'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('Custom variables should be parsed from env file', async () => { |
| 55 | + const vars = await variablesService.parseFile(path.join(envFilesFolderPath, '.env')); |
| 56 | + |
| 57 | + expect(vars).to.not.equal(undefined, 'Variables is is undefiend'); |
| 58 | + expect(Object.keys(vars!)).lengthOf(2, 'Incorrect number of variables'); |
| 59 | + expect(vars).to.have.property('X1234PYEXTUNITTESTVAR', '1234', 'X1234PYEXTUNITTESTVAR value is invalid'); |
| 60 | + expect(vars).to.have.property('PYTHONPATH', '../workspace5', 'PYTHONPATH value is invalid'); |
| 61 | + }); |
| 62 | + |
| 63 | + test('PATH and PYTHONPATH from env file should be returned as is', async () => { |
| 64 | + const vars = await variablesService.parseFile(path.join(envFilesFolderPath, '.env5')); |
| 65 | + const expectedPythonPath = '/usr/one/three:/usr/one/four'; |
| 66 | + const expectedPath = '/usr/x:/usr/y'; |
| 67 | + expect(vars).to.not.equal(undefined, 'Variables is is undefiend'); |
| 68 | + expect(Object.keys(vars!)).lengthOf(5, 'Incorrect number of variables'); |
| 69 | + expect(vars).to.have.property('X', '1', 'X value is invalid'); |
| 70 | + expect(vars).to.have.property('Y', '2', 'Y value is invalid'); |
| 71 | + expect(vars).to.have.property('PYTHONPATH', expectedPythonPath, 'PYTHONPATH value is invalid'); |
| 72 | + expect(vars).to.have.property('PATH', expectedPath, 'PATH value is invalid'); |
| 73 | + }); |
| 74 | + |
| 75 | + test('Simple variable substitution is supported', async () => { |
| 76 | + const vars = await variablesService.parseFile(path.join(envFilesFolderPath, '.env6'), { BINDIR: '/usr/bin' }); |
| 77 | + |
| 78 | + expect(vars).to.not.equal(undefined, 'Variables is undefiend'); |
| 79 | + expect(Object.keys(vars!)).lengthOf(3, 'Incorrect number of variables'); |
| 80 | + expect(vars).to.have.property('REPO', '/home/user/git/foobar', 'value is invalid'); |
| 81 | + expect(vars).to.have.property('PYTHONPATH', '/home/user/git/foobar/foo:/home/user/git/foobar/bar', 'value is invalid'); |
| 82 | + expect(vars).to.have.property('PYTHON', '/usr/bin/python3', 'value is invalid'); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments