3
3
4
4
'use strict' ;
5
5
6
+ import { expect } from 'chai' ;
6
7
import rewiremock from 'rewiremock' ;
7
8
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' ;
9
10
import * as TypeMoq from 'typemoq' ;
10
11
import { ICommandManager } from '../../../../client/common/application/types' ;
11
12
import { Commands } from '../../../../client/common/constants' ;
12
13
import { ITerminalService , ITerminalServiceFactory } from '../../../../client/common/terminal/types' ;
13
14
import { IDisposable } from '../../../../client/common/types' ;
15
+ import { Interpreters } from '../../../../client/common/utils/localize' ;
14
16
import { InstallPythonViaTerminal } from '../../../../client/interpreter/configuration/interpreterSelector/commands/installPython/installPythonViaTerminal' ;
15
17
16
18
suite ( 'Install Python via Terminal' , ( ) => {
17
19
let cmdManager : ICommandManager ;
18
20
let terminalServiceFactory : ITerminalServiceFactory ;
19
21
let installPythonCommand : InstallPythonViaTerminal ;
20
22
let terminalService : ITerminalService ;
23
+ let message : string | undefined ;
21
24
setup ( ( ) => {
22
25
rewiremock . enable ( ) ;
23
26
cmdManager = mock < ICommandManager > ( ) ;
24
27
terminalServiceFactory = mock < ITerminalServiceFactory > ( ) ;
25
28
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
+ } ) ;
27
34
installPythonCommand = new InstallPythonViaTerminal ( instance ( cmdManager ) , instance ( terminalServiceFactory ) , [ ] ) ;
28
35
} ) ;
29
36
@@ -32,12 +39,18 @@ suite('Install Python via Terminal', () => {
32
39
sinon . restore ( ) ;
33
40
} ) ;
34
41
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 ( ) => {
36
43
let installCommandHandler : ( ) => Promise < void > ;
37
44
when ( cmdManager . registerCommand ( Commands . InstallPythonOnLinux , anything ( ) ) ) . thenCall ( ( _ , cb ) => {
38
45
installCommandHandler = cb ;
39
46
return TypeMoq . Mock . ofType < IDisposable > ( ) . object ;
40
47
} ) ;
48
+ rewiremock ( 'which' ) . with ( ( cmd : string ) => {
49
+ if ( cmd === 'apt' ) {
50
+ return 'path/to/apt' ;
51
+ }
52
+ throw new Error ( 'Command not found' ) ;
53
+ } ) ;
41
54
await installPythonCommand . activate ( ) ;
42
55
when ( terminalService . sendText ( 'sudo apt-get update' ) ) . thenResolve ( ) ;
43
56
when ( terminalService . sendText ( 'sudo apt-get install python3 python3-venv python3-pip' ) ) . thenResolve ( ) ;
@@ -67,6 +80,23 @@ suite('Install Python via Terminal', () => {
67
80
await installCommandHandler ! ( ) ;
68
81
69
82
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 ) ;
70
100
} ) ;
71
101
72
102
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', () => {
81
111
await installCommandHandler ! ( ) ;
82
112
83
113
verify ( terminalService . sendText ( 'brew install python3' ) ) . once ( ) ;
114
+ expect ( message ) . to . be . equal ( undefined ) ;
84
115
} ) ;
85
116
} ) ;
0 commit comments