Skip to content

Commit 9a8e7ee

Browse files
author
Eric Snow
authored
Make proper unit tests for EnvironmentVariablesService. (#9704)
(for #8895) This change helps simplify the changes in #9499.
1 parent 66ec955 commit 9a8e7ee

3 files changed

Lines changed: 574 additions & 353 deletions

File tree

src/client/common/variables/environment.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ import { EnvironmentVariables, IEnvironmentVariablesService } from './types';
1111

1212
@injectable()
1313
export class EnvironmentVariablesService implements IEnvironmentVariablesService {
14-
private readonly pathVariable: 'PATH' | 'Path';
15-
constructor(@inject(IPathUtils) pathUtils: IPathUtils, @inject(IFileSystem) private readonly fs: IFileSystem) {
16-
this.pathVariable = pathUtils.getPathVariableName();
17-
}
14+
private _pathVariable?: 'Path' | 'PATH';
15+
constructor(
16+
// We only use a small portion of either of these interfaces.
17+
@inject(IPathUtils) private readonly pathUtils: IPathUtils,
18+
@inject(IFileSystem) private readonly fs: IFileSystem
19+
) {}
20+
1821
public async parseFile(filePath?: string, baseVars?: EnvironmentVariables): Promise<EnvironmentVariables | undefined> {
1922
if (!filePath || !(await this.fs.fileExists(filePath))) {
2023
return;
2124
}
2225
return parseEnvFile(await this.fs.readFile(filePath), baseVars);
2326
}
27+
2428
public mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables) {
2529
if (!target) {
2630
return;
@@ -35,12 +39,22 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
3539
}
3640
});
3741
}
42+
3843
public appendPythonPath(vars: EnvironmentVariables, ...pythonPaths: string[]) {
3944
return this.appendPaths(vars, 'PYTHONPATH', ...pythonPaths);
4045
}
46+
4147
public appendPath(vars: EnvironmentVariables, ...paths: string[]) {
4248
return this.appendPaths(vars, this.pathVariable, ...paths);
4349
}
50+
51+
private get pathVariable(): 'Path' | 'PATH' {
52+
if (!this._pathVariable) {
53+
this._pathVariable = this.pathUtils.getPathVariableName();
54+
}
55+
return this._pathVariable!;
56+
}
57+
4458
private appendPaths(vars: EnvironmentVariables, variableName: 'PATH' | 'Path' | 'PYTHONPATH', ...pathsToAppend: string[]) {
4559
const valueToAppend = pathsToAppend
4660
.filter(item => typeof item === 'string' && item.trim().length > 0)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)