Skip to content

Commit e14dc20

Browse files
author
Kartik Raj
committed
Add tests
1 parent 0df0fc4 commit e14dc20

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/test/configuration/interpreterSelector/commands/installPythonViaTerminal.unit.test.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,34 @@
33

44
'use strict';
55

6+
import { expect } from 'chai';
67
import rewiremock from 'rewiremock';
78
import * as sinon from 'sinon';
8-
import { anything, instance, mock, verify, when } from 'ts-mockito';
9+
import { anything, instance, mock, reset, verify, when } from 'ts-mockito';
910
import * as TypeMoq from 'typemoq';
1011
import { ICommandManager } from '../../../../client/common/application/types';
1112
import { Commands } from '../../../../client/common/constants';
1213
import { ITerminalService, ITerminalServiceFactory } from '../../../../client/common/terminal/types';
1314
import { IDisposable } from '../../../../client/common/types';
15+
import { Interpreters } from '../../../../client/common/utils/localize';
1416
import { InstallPythonViaTerminal } from '../../../../client/interpreter/configuration/interpreterSelector/commands/installPython/installPythonViaTerminal';
1517

1618
suite('Install Python via Terminal', () => {
1719
let cmdManager: ICommandManager;
1820
let terminalServiceFactory: ITerminalServiceFactory;
1921
let installPythonCommand: InstallPythonViaTerminal;
2022
let terminalService: ITerminalService;
23+
let message: string | undefined;
2124
setup(() => {
2225
rewiremock.enable();
2326
cmdManager = mock<ICommandManager>();
2427
terminalServiceFactory = mock<ITerminalServiceFactory>();
2528
terminalService = mock<ITerminalService>();
26-
when(terminalServiceFactory.getTerminalService(anything())).thenReturn(instance(terminalService));
29+
message = undefined;
30+
when(terminalServiceFactory.getTerminalService(anything())).thenCall((options) => {
31+
message = options.message;
32+
return instance(terminalService);
33+
});
2734
installPythonCommand = new InstallPythonViaTerminal(instance(cmdManager), instance(terminalServiceFactory), []);
2835
});
2936

@@ -32,12 +39,18 @@ suite('Install Python via Terminal', () => {
3239
sinon.restore();
3340
});
3441

35-
test('Sends expected commands when InstallPythonOnLinux command is executed if no dnf is available', async () => {
42+
test('Sends expected commands when InstallPythonOnLinux command is executed if apt is available', async () => {
3643
let installCommandHandler: () => Promise<void>;
3744
when(cmdManager.registerCommand(Commands.InstallPythonOnLinux, anything())).thenCall((_, cb) => {
3845
installCommandHandler = cb;
3946
return TypeMoq.Mock.ofType<IDisposable>().object;
4047
});
48+
rewiremock('which').with((cmd: string) => {
49+
if (cmd === 'apt') {
50+
return 'path/to/apt';
51+
}
52+
throw new Error('Command not found');
53+
});
4154
await installPythonCommand.activate();
4255
when(terminalService.sendText('sudo apt-get update')).thenResolve();
4356
when(terminalService.sendText('sudo apt-get install python3 python3-venv python3-pip')).thenResolve();
@@ -67,6 +80,23 @@ suite('Install Python via Terminal', () => {
6780
await installCommandHandler!();
6881

6982
verify(terminalService.sendText('sudo dnf install python3')).once();
83+
expect(message).to.be.equal(undefined);
84+
});
85+
86+
test('Creates terminal with appropriate message when InstallPythonOnLinux command is executed if no known linux package managers are available', async () => {
87+
let installCommandHandler: () => Promise<void>;
88+
when(cmdManager.registerCommand(Commands.InstallPythonOnLinux, anything())).thenCall((_, cb) => {
89+
installCommandHandler = cb;
90+
return TypeMoq.Mock.ofType<IDisposable>().object;
91+
});
92+
rewiremock('which').with((_cmd: string) => {
93+
throw new Error('Command not found');
94+
});
95+
96+
await installPythonCommand.activate();
97+
await installCommandHandler!();
98+
99+
expect(message).to.be.equal(Interpreters.installPythonTerminalMessage);
70100
});
71101

72102
test('Sends expected commands on Mac when InstallPythonOnMac command is executed if no dnf is available', async () => {
@@ -81,5 +111,6 @@ suite('Install Python via Terminal', () => {
81111
await installCommandHandler!();
82112

83113
verify(terminalService.sendText('brew install python3')).once();
114+
expect(message).to.be.equal(undefined);
84115
});
85116
});

0 commit comments

Comments
 (0)