Skip to content

Commit 98bcb8b

Browse files
committed
Add coverage to getUserHomeDir func
1 parent 5aeb94a commit 98bcb8b

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/client/common/utils/platform.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ export function getEnvironmentVariable(key: string): string | undefined {
6060
* Get the current user's home directory.
6161
*
6262
* The lookup is limited to environment variables.
63+
*
64+
* Note: It's used the `exports.getEnvironmentVariable` to allow proper testing of this function
6365
*/
6466
export function getUserHomeDir(): string | undefined {
6567
if (getOSType() === OSType.Windows) {
66-
return getEnvironmentVariable('USERPROFILE');
68+
return exports.getEnvironmentVariable('USERPROFILE');
6769
}
68-
return getEnvironmentVariable('HOME') || getEnvironmentVariable('HOMEPATH');
70+
return exports.getEnvironmentVariable('HOME') || exports.getEnvironmentVariable('HOMEPATH');
6971
}

src/test/common/utils/platform.unit.test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,70 @@
33

44
'use strict';
55

6+
import * as sinon from 'sinon';
7+
68
import { expect } from 'chai';
7-
import { OSType, getOSType } from '../../../client/common/utils/platform';
9+
import * as platformApis from '../../../client/common/utils/platform';
10+
11+
const { OSType, getOSType } = platformApis;
12+
13+
suite('Utils for platform - getUserHomeDir function', () => {
14+
let getEnvVarStub: sinon.SinonStub;
15+
let getOSTypeStub: sinon.SinonStub;
16+
17+
setup(() => {
18+
getEnvVarStub = sinon.stub(platformApis, 'getEnvironmentVariable');
19+
getEnvVarStub.callsFake((envVarName) => {
20+
if (envVarName === 'USERPROFILE') return 'my/home/dir/in/windows/os';
21+
return 'my/home/dir/in/a/not/windows/os';
22+
});
23+
});
24+
25+
teardown(() => {
26+
getEnvVarStub.restore();
27+
getOSTypeStub.restore();
28+
});
29+
30+
test('getUserHomeDir when platform is Linux', () => {
31+
getOSTypeStub = sinon.stub(platformApis, 'getOSType').callsFake(() => platformApis.OSType.Linux);
32+
33+
const os = getOSTypeStub();
34+
const homeDirEnvVar = platformApis.getUserHomeDir();
35+
36+
expect(homeDirEnvVar).equal('my/home/dir/in/a/not/windows/os');
37+
expect(os).equal(platformApis.OSType.Linux);
38+
});
39+
40+
test('getUserHomeDir when platform is OSX', () => {
41+
getOSTypeStub = sinon.stub(platformApis, 'getOSType').callsFake(() => platformApis.OSType.OSX);
42+
43+
const os = getOSTypeStub();
44+
const homeDirEnvVar = platformApis.getUserHomeDir();
45+
46+
expect(homeDirEnvVar).equal('my/home/dir/in/a/not/windows/os');
47+
expect(os).equal(platformApis.OSType.OSX);
48+
});
49+
50+
test('getUserHomeDir when platform is Unknown', () => {
51+
getOSTypeStub = sinon.stub(platformApis, 'getOSType').callsFake(() => platformApis.OSType.Unknown);
52+
53+
const os = getOSTypeStub();
54+
const homeDirEnvVar = platformApis.getUserHomeDir();
55+
56+
expect(homeDirEnvVar).equal('my/home/dir/in/a/not/windows/os');
57+
expect(os).equal(platformApis.OSType.Unknown);
58+
});
59+
60+
test('getUserHomeDir when platform is Windows', () => {
61+
getOSTypeStub = sinon.stub(platformApis, 'getOSType').callsFake(() => platformApis.OSType.Windows);
62+
63+
const os = getOSTypeStub();
64+
const homeDirEnvVar = platformApis.getUserHomeDir();
65+
66+
expect(homeDirEnvVar).equal('my/home/dir/in/a/not/windows/os');
67+
expect(os).equal(platformApis.OSType.Windows);
68+
});
69+
});
870

971
suite('Utils for platform - getOSType function', () => {
1072
const testsData = [

0 commit comments

Comments
 (0)