6
6
// tslint:disable:no-object-literal-type-assertion
7
7
8
8
import { expect } from 'chai' ;
9
- import * as path from 'path' ;
10
- import * as sinon from 'sinon' ;
11
- import { anything , instance , mock , when } from 'ts-mockito' ;
12
- import { CancellationToken , CancellationTokenSource , TextDocument , Uri } from 'vscode' ;
13
- import { Product } from '../../client/common/types' ;
14
- import { ServiceContainer } from '../../client/ioc/container' ;
15
9
import { parseLine } from '../../client/linters/baseLinter' ;
16
- import { LinterManager } from '../../client/linters/linterManager' ;
17
- import { MyPy , REGEX } from '../../client/linters/mypy' ;
18
- import { ILinterManager , ILintMessage , LintMessageSeverity } from '../../client/linters/types' ;
19
- import { MockOutputChannel } from '../mockClasses' ;
10
+ import { REGEX } from '../../client/linters/mypy' ;
11
+ import { ILintMessage } from '../../client/linters/types' ;
20
12
21
13
// This following is a real-world example. See gh=2380.
22
- // tslint:disable:no-multiline-string no-any max-func-body-length
14
+ // tslint:disable-next-line :no-multiline-string
23
15
const output = `
24
16
provider.pyi:10: error: Incompatible types in assignment (expression has type "str", variable has type "int")
25
17
provider.pyi:11: error: Name 'not_declared_var' is not defined
@@ -30,30 +22,39 @@ suite('Linting - MyPy', () => {
30
22
test ( 'regex' , async ( ) => {
31
23
const lines = output . split ( '\n' ) ;
32
24
const tests : [ string , ILintMessage ] [ ] = [
33
- [ lines [ 1 ] , {
34
- code : undefined ,
35
- message : 'Incompatible types in assignment (expression has type "str", variable has type "int")' ,
36
- column : 0 ,
37
- line : 10 ,
38
- type : 'error' ,
39
- provider : 'mypy'
40
- } as ILintMessage ] ,
41
- [ lines [ 2 ] , {
42
- code : undefined ,
43
- message : 'Name \'not_declared_var\' is not defined' ,
44
- column : 0 ,
45
- line : 11 ,
46
- type : 'error' ,
47
- provider : 'mypy'
48
- } as ILintMessage ] ,
49
- [ lines [ 3 ] , {
50
- code : undefined ,
51
- message : 'Expression has type "Any"' ,
52
- column : 21 ,
53
- line : 12 ,
54
- type : 'error' ,
55
- provider : 'mypy'
56
- } as ILintMessage ]
25
+ [
26
+ lines [ 1 ] ,
27
+ {
28
+ code : undefined ,
29
+ message : 'Incompatible types in assignment (expression has type "str", variable has type "int")' ,
30
+ column : 0 ,
31
+ line : 10 ,
32
+ type : 'error' ,
33
+ provider : 'mypy'
34
+ } as ILintMessage
35
+ ] ,
36
+ [
37
+ lines [ 2 ] ,
38
+ {
39
+ code : undefined ,
40
+ message : "Name 'not_declared_var' is not defined" ,
41
+ column : 0 ,
42
+ line : 11 ,
43
+ type : 'error' ,
44
+ provider : 'mypy'
45
+ } as ILintMessage
46
+ ] ,
47
+ [
48
+ lines [ 3 ] ,
49
+ {
50
+ code : undefined ,
51
+ message : 'Expression has type "Any"' ,
52
+ column : 21 ,
53
+ line : 12 ,
54
+ type : 'error' ,
55
+ provider : 'mypy'
56
+ } as ILintMessage
57
+ ]
57
58
] ;
58
59
for ( const [ line , expected ] of tests ) {
59
60
const msg = parseLine ( line , REGEX , 'mypy' ) ;
@@ -62,72 +63,3 @@ suite('Linting - MyPy', () => {
62
63
}
63
64
} ) ;
64
65
} ) ;
65
-
66
- suite ( 'Test Linter' , ( ) => {
67
- class TestMyPyLinter extends MyPy {
68
- // tslint:disable: no-unnecessary-override
69
- public async runLinter ( document : TextDocument , cancellation : CancellationToken ) : Promise < ILintMessage [ ] > {
70
- return super . runLinter ( document , cancellation ) ;
71
- }
72
- public getWorkspaceRootPath ( document : TextDocument ) : string {
73
- return super . getWorkspaceRootPath ( document ) ;
74
- }
75
- public async run ( args : string [ ] , document : TextDocument , cancellation : CancellationToken , regEx : string = REGEX ) : Promise < ILintMessage [ ] > {
76
- return super . run ( args , document , cancellation , regEx ) ;
77
- }
78
- public parseMessagesSeverity ( error : string , severity : any ) : LintMessageSeverity {
79
- return super . parseMessagesSeverity ( error , severity ) ;
80
- }
81
- }
82
-
83
- let linter : TestMyPyLinter ;
84
- let getWorkspaceRootPathStub : sinon . SinonStub < [ TextDocument ] , string > ;
85
- let runStub : sinon . SinonStub < [ string [ ] , TextDocument , CancellationToken , ( string | undefined ) ?] , Promise < ILintMessage [ ] > > ;
86
- const token = new CancellationTokenSource ( ) . token ;
87
- teardown ( ( ) => sinon . restore ( ) ) ;
88
- setup ( ( ) => {
89
- const linterManager = mock ( LinterManager ) ;
90
- when ( linterManager . getLinterInfo ( anything ( ) ) ) . thenReturn ( { product : Product . mypy } as any ) ;
91
- const serviceContainer = mock ( ServiceContainer ) ;
92
- when ( serviceContainer . get < ILinterManager > ( ILinterManager ) ) . thenReturn ( instance ( linterManager ) ) ;
93
- getWorkspaceRootPathStub = sinon . stub ( TestMyPyLinter . prototype , 'getWorkspaceRootPath' ) ;
94
- runStub = sinon . stub ( TestMyPyLinter . prototype , 'run' ) ;
95
- linter = new TestMyPyLinter ( instance ( mock ( MockOutputChannel ) ) , instance ( serviceContainer ) ) ;
96
- } ) ;
97
-
98
- test ( 'Get cwd based on document' , async ( ) => {
99
- const fileUri = Uri . file ( path . join ( 'a' , 'b' , 'c' , 'd' , 'e' , 'filename.py' ) ) ;
100
- const cwd = path . join ( 'a' , 'b' , 'c' ) ;
101
- const doc = { uri : fileUri } as any as TextDocument ;
102
- getWorkspaceRootPathStub . callsFake ( ( ) => cwd ) ;
103
- runStub . callsFake ( ( ) => Promise . resolve ( [ ] ) ) ;
104
-
105
- await linter . runLinter ( doc , token ) ;
106
-
107
- expect ( getWorkspaceRootPathStub . callCount ) . to . equal ( 1 ) ;
108
- expect ( getWorkspaceRootPathStub . args [ 0 ] ) . to . deep . equal ( [ doc ] ) ;
109
- } ) ;
110
- test ( 'Pass relative path of document to linter' , async ( ) => {
111
- const fileUri = Uri . file ( path . join ( 'a' , 'b' , 'c' , 'd' , 'e' , 'filename.py' ) ) ;
112
- const cwd = path . join ( 'a' , 'b' , 'c' ) ;
113
- const doc = { uri : fileUri } as any as TextDocument ;
114
- getWorkspaceRootPathStub . callsFake ( ( ) => cwd ) ;
115
- runStub . callsFake ( ( ) => Promise . resolve ( [ ] ) ) ;
116
-
117
- await linter . runLinter ( doc , token ) ;
118
-
119
- expect ( runStub . callCount ) . to . equal ( 1 ) ;
120
- expect ( runStub . args [ 0 ] ) . to . deep . equal ( [ [ path . relative ( cwd , fileUri . fsPath ) ] , doc , token , REGEX ] ) ;
121
- } ) ;
122
- test ( 'Return empty messages' , async ( ) => {
123
- const fileUri = Uri . file ( path . join ( 'a' , 'b' , 'c' , 'd' , 'e' , 'filename.py' ) ) ;
124
- const cwd = path . join ( 'a' , 'b' , 'c' ) ;
125
- const doc = { uri : fileUri } as any as TextDocument ;
126
- getWorkspaceRootPathStub . callsFake ( ( ) => cwd ) ;
127
- runStub . callsFake ( ( ) => Promise . resolve ( [ ] ) ) ;
128
-
129
- const messages = await linter . runLinter ( doc , token ) ;
130
-
131
- expect ( messages ) . to . be . deep . equal ( [ ] ) ;
132
- } ) ;
133
- } ) ;
0 commit comments