Skip to content

Commit 58873bc

Browse files
author
Kartik Raj
authored
Added tests for productPath.ts & condaInstaller.ts (#9291)
1 parent af7480c commit 58873bc

4 files changed

Lines changed: 51 additions & 9 deletions

File tree

src/client/common/installer/condaInstaller.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { InterpreterUri } from './types';
1414
*/
1515
@injectable()
1616
export class CondaInstaller extends ModuleInstaller {
17-
private isCondaAvailable: boolean | undefined;
17+
public _isCondaAvailable: boolean | undefined;
1818

1919
constructor(
2020
@inject(IServiceContainer) serviceContainer: IServiceContainer
@@ -43,12 +43,12 @@ export class CondaInstaller extends ModuleInstaller {
4343
* @returns {Promise<boolean>} Whether conda is supported as a module installer or not.
4444
*/
4545
public async isSupported(resource?: InterpreterUri): Promise<boolean> {
46-
if (this.isCondaAvailable === false) {
46+
if (this._isCondaAvailable === false) {
4747
return false;
4848
}
4949
const condaLocator = this.serviceContainer.get<ICondaService>(ICondaService);
50-
this.isCondaAvailable = await condaLocator.isCondaAvailable();
51-
if (!this.isCondaAvailable) {
50+
this._isCondaAvailable = await condaLocator.isCondaAvailable();
51+
if (!this._isCondaAvailable) {
5252
return false;
5353
}
5454
// Now we need to check if the current environment is a conda environment or not.
@@ -88,8 +88,8 @@ export class CondaInstaller extends ModuleInstaller {
8888
private async isCurrentEnvironmentACondaEnvironment(resource?: InterpreterUri): Promise<boolean> {
8989
const condaService = this.serviceContainer.get<ICondaService>(ICondaService);
9090
const pythonPath = isResource(resource) ?
91-
this.serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings(resource).pythonPath :
92-
resource.path;
91+
this.serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings(resource).pythonPath :
92+
resource.path;
9393
return condaService.isCondaEnvironment(pythonPath);
9494
}
9595
}

src/client/common/installer/productPath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { IConfigurationService, IInstaller, ModuleNamePurpose, Product } from '.
1616
import { IProductPathService } from './types';
1717

1818
@injectable()
19-
abstract class BaseProductPathsService implements IProductPathService {
19+
export abstract class BaseProductPathsService implements IProductPathService {
2020
protected readonly configService: IConfigurationService;
2121
protected readonly productInstaller: IInstaller;
2222
constructor(@inject(IServiceContainer) protected serviceContainer: IServiceContainer) {
@@ -25,7 +25,7 @@ abstract class BaseProductPathsService implements IProductPathService {
2525
}
2626
public abstract getExecutableNameFromSettings(product: Product, resource?: Uri): string;
2727
public isExecutableAModule(product: Product, resource?: Uri): Boolean {
28-
if (product === Product.ipykernel){
28+
if (product === Product.ipykernel) {
2929
return true;
3030
}
3131
let moduleName: string | undefined;

src/test/common/installer/condaInstaller.unit.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ suite('Common - Conda Installer', () => {
3030
});
3131
test('Name and priority', async () => {
3232
assert.equal(installer.displayName, 'Conda');
33+
assert.equal(installer.name, 'Conda');
3334
assert.equal(installer.priority, 0);
3435
});
36+
test('Installer is not supported when conda is available variable is set to false', async () => {
37+
const uri = Uri.file(__filename);
38+
installer._isCondaAvailable = false;
39+
40+
const supported = await installer.isSupported(uri);
41+
42+
assert.equal(supported, false);
43+
});
3544
test('Installer is not supported when conda is not available', async () => {
3645
const uri = Uri.file(__filename);
3746
when(condaService.isCondaAvailable()).thenResolve(false);

src/test/common/installer/productPath.unit.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as TypeMoq from 'typemoq';
1212
import { OutputChannel, Uri } from 'vscode';
1313
import '../../../client/common/extensions';
1414
import { ProductInstaller } from '../../../client/common/installer/productInstaller';
15-
import { CTagsProductPathService, DataScienceProductPathService, FormatterProductPathService, LinterProductPathService, RefactoringLibraryProductPathService, TestFrameworkProductPathService } from '../../../client/common/installer/productPath';
15+
import { BaseProductPathsService, CTagsProductPathService, DataScienceProductPathService, FormatterProductPathService, LinterProductPathService, RefactoringLibraryProductPathService, TestFrameworkProductPathService } from '../../../client/common/installer/productPath';
1616
import { ProductService } from '../../../client/common/installer/productService';
1717
import { IProductService } from '../../../client/common/installer/types';
1818
import { IConfigurationService, IFormattingSettings, IInstaller, IPythonSettings, ITestingSettings, IWorkspaceSymbolSettings, ModuleNamePurpose, Product, ProductType } from '../../../client/common/types';
@@ -27,6 +27,11 @@ use(chaiAsPromised);
2727
suite('Product Path', () => {
2828
[undefined, Uri.file('resource')].forEach(resource => {
2929
getNamesAndValues<Product>(Product).forEach(product => {
30+
class TestBaseProductPathsService extends BaseProductPathsService {
31+
public getExecutableNameFromSettings(_: Product, _resource?: Uri): string {
32+
return '';
33+
}
34+
}
3035
let serviceContainer: TypeMoq.IMock<IServiceContainer>;
3136
let formattingSettings: TypeMoq.IMock<IFormattingSettings>;
3237
let unitTestSettings: TypeMoq.IMock<ITestingSettings>;
@@ -58,6 +63,34 @@ suite('Product Path', () => {
5863
if (product.value === Product.isort) {
5964
return;
6065
}
66+
suite('Method isExecutableAModule()', () => {
67+
if (product.value === Product.ipykernel) {
68+
test('Returns true if product is ipykernel', () => {
69+
const productPathService = new TestBaseProductPathsService(serviceContainer.object);
70+
expect(productPathService.isExecutableAModule(product.value)).to.equal(true, 'Should be true');
71+
});
72+
} else {
73+
test('Returns true if User has customized the executable name', () => {
74+
productInstaller.translateProductToModuleName = () => 'moduleName';
75+
const productPathService = new TestBaseProductPathsService(serviceContainer.object);
76+
productPathService.getExecutableNameFromSettings = () => 'executableName';
77+
expect(productPathService.isExecutableAModule(product.value)).to.equal(true, 'Should be true');
78+
});
79+
test('Returns false if User has customized the full path to executable', () => {
80+
productInstaller.translateProductToModuleName = () => 'moduleName';
81+
const productPathService = new TestBaseProductPathsService(serviceContainer.object);
82+
productPathService.getExecutableNameFromSettings = () => 'path/to/executable';
83+
expect(productPathService.isExecutableAModule(product.value)).to.equal(false, 'Should be false');
84+
});
85+
test('Returns false if translating product to module name fails with error', () => {
86+
// tslint:disable-next-line: no-any
87+
productInstaller.translateProductToModuleName = () => { return new Error('Kaboom') as any; };
88+
const productPathService = new TestBaseProductPathsService(serviceContainer.object);
89+
productPathService.getExecutableNameFromSettings = () => 'executableName';
90+
expect(productPathService.isExecutableAModule(product.value)).to.equal(false, 'Should be false');
91+
});
92+
}
93+
});
6194
const productType = new ProductService().getProductType(product.value);
6295
switch (productType) {
6396
case ProductType.Formatter: {

0 commit comments

Comments
 (0)