Skip to content

Commit

Permalink
Add some more coverage to utils functions (#21026)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpinamtz authored May 4, 2023
1 parent 7dfa631 commit 4112b04
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/test/common/terminals/shellDetector.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ suite('Shell Detector', () => {

getNamesAndValues<OSType>(OSType).forEach((os) => {
const testSuffix = `(OS ${os.name})`;
test('Test identification of Terminal Shells in order of priority', async () => {
test(`Test identification of Terminal Shells in order of priority ${testSuffix}`, async () => {
const callOrder: string[] = [];
const nameDetectorIdentify = sandbox.stub(TerminalNameShellDetector.prototype, 'identify');
nameDetectorIdentify.callsFake(() => {
Expand Down
25 changes: 25 additions & 0 deletions src/test/common/utils/exec.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { expect } from 'chai';
import { OSType } from '../../common';
import { getSearchPathEnvVarNames } from '../../../client/common/utils/exec';

suite('Utils for exec - getSearchPathEnvVarNames function', () => {
const testsData = [
{ os: 'Unknown', expected: ['PATH'] },
{ os: 'Windows', expected: ['Path', 'PATH'] },
{ os: 'OSX', expected: ['PATH'] },
{ os: 'Linux', expected: ['PATH'] },
];

testsData.forEach((testData) => {
test(`getSearchPathEnvVarNames when os is ${testData.os}`, () => {
const pathVariables = getSearchPathEnvVarNames(testData.os as OSType);

expect(pathVariables).to.deep.equal(testData.expected);
});
});
});
50 changes: 50 additions & 0 deletions src/test/common/utils/filesystem.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { expect } from 'chai';
import { convertFileType } from '../../../client/common/utils/filesystem';

class KnowsFileTypeDummyImpl {
private _isFile: boolean;

private _isDirectory: boolean;

private _isSymbolicLink: boolean;

constructor(isFile = false, isDirectory = false, isSymbolicLink = false) {
this._isFile = isFile;
this._isDirectory = isDirectory;
this._isSymbolicLink = isSymbolicLink;
}

public isFile() {
return this._isFile;
}

public isDirectory() {
return this._isDirectory;
}

public isSymbolicLink() {
return this._isSymbolicLink;
}
}

suite('Utils for filesystem - convertFileType function', () => {
const testsData = [
{ info: new KnowsFileTypeDummyImpl(true, false, false), kind: 'File', expected: 1 },
{ info: new KnowsFileTypeDummyImpl(false, true, false), kind: 'Directory', expected: 2 },
{ info: new KnowsFileTypeDummyImpl(false, false, true), kind: 'Symbolic Link', expected: 64 },
{ info: new KnowsFileTypeDummyImpl(false, false, false), kind: 'Unknown', expected: 0 },
];

testsData.forEach((testData) => {
test(`convertFileType when info is a ${testData.kind}`, () => {
const fileType = convertFileType(testData.info);

expect(fileType).equals(testData.expected);
});
});
});
23 changes: 23 additions & 0 deletions src/test/common/utils/platform.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { expect } from 'chai';
import { OSType, getOSType } from '../../../client/common/utils/platform';

suite('Utils for platform - getOSType function', () => {
const testsData = [
{ platform: 'linux', expected: OSType.Linux },
{ platform: 'darwin', expected: OSType.OSX },
{ platform: 'anunknownplatform', expected: OSType.Unknown },
{ platform: 'windows', expected: OSType.Windows },
];

testsData.forEach((testData) => {
test(`getOSType when platform is ${testData.platform}`, () => {
const osType = getOSType(testData.platform);
expect(osType).equal(testData.expected);
});
});
});
51 changes: 50 additions & 1 deletion src/test/common/utils/text.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { expect } from 'chai';
import { Position, Range } from 'vscode';
import { parsePosition, parseRange } from '../../../client/common/utils/text';
import { getDedentedLines, getIndent, parsePosition, parseRange } from '../../../client/common/utils/text';

suite('parseRange()', () => {
test('valid strings', async () => {
Expand Down Expand Up @@ -98,3 +98,52 @@ suite('parsePosition()', () => {
}
});
});

suite('getIndent()', () => {
const testsData = [
{ line: 'text', expected: '' },
{ line: ' text', expected: ' ' },
{ line: ' text', expected: ' ' },
{ line: ' tabulatedtext', expected: '' },
];

testsData.forEach((testData) => {
test(`getIndent when line is ${testData.line}`, () => {
const indent = getIndent(testData.line);

expect(indent).equal(testData.expected);
});
});
});

suite('getDedentedLines()', () => {
const testsData = [
{ text: '', expected: [] },
{ text: '\n', expected: Error, exceptionMessage: 'expected "first" line to not be blank' },
{ text: 'line1\n', expected: Error, exceptionMessage: 'expected actual first line to be blank' },
{
text: '\n line2\n line3',
expected: Error,
exceptionMessage: 'line 1 has less indent than the "first" line',
},
{
text: '\n line2\n line3',
expected: ['line2', 'line3'],
},
{
text: '\n line2\n line3',
expected: ['line2', ' line3'],
},
];

testsData.forEach((testData) => {
test(`getDedentedLines when line is ${testData.text}`, () => {
if (Array.isArray(testData.expected)) {
const dedentedLines = getDedentedLines(testData.text);
expect(dedentedLines).to.deep.equal(testData.expected);
} else {
expect(() => getDedentedLines(testData.text)).to.throw(testData.expected, testData.exceptionMessage);
}
});
});
});

0 comments on commit 4112b04

Please sign in to comment.