Skip to content

Commit e03d122

Browse files
committed
Chore: Add tests for always ignored diagnostics
1 parent 07886de commit e03d122

File tree

2 files changed

+168
-7
lines changed

2 files changed

+168
-7
lines changed
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
python () {
2-
error()
3-
}
1+
foo () {
2+
echo "${@"" if True else False}"
3+
echo "${@d.getVar('PV').split('.')[0]}"
4+
}
5+
6+
def test ():
7+
error('looooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line')
8+
9+
python __anonymous() {
10+
import os
11+
}

integration-tests/src/tests/diagnostics.test.ts

Lines changed: 157 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,163 @@ suite('Bitbake Diagnostics Test Suite', () => {
3535
test('Diagnostics', async () => {
3636
void vscode.workspace.openTextDocument(docUri)
3737
await assertWillComeTrue(async () => {
38-
const diagnostics = vscode.languages.getDiagnostics(docUri)
39-
return diagnostics.length === 1 &&
40-
diagnostics[0].source === 'Pylance, bitbake-python' &&
41-
diagnostics[0].range.isEqual(new vscode.Range(1, 4, 1, 9))
38+
const diagnosticsResult = vscode.languages.getDiagnostics()
39+
// generateTestCases(diagnosticsResult)
40+
const diagnostics = diagnosticsResult.find(
41+
([uri]) => uri.toString() === docUri.toString()
42+
)?.at(1)
43+
44+
if (
45+
!Array.isArray(diagnostics) || // For unknown reasons, Typescript thinks "diagnostics" could be an Uri
46+
diagnostics.length !== expectedDiagnostics.length
47+
) {
48+
return false
49+
}
50+
51+
const hasAllExpectedDiagnostics = diagnostics.every((diagnostic) => {
52+
return expectedDiagnostics.some((expectedDiagnostic) => {
53+
return (
54+
diagnostic.range.isEqual(expectedDiagnostic.range) &&
55+
diagnostic.message === expectedDiagnostic.message &&
56+
getCode(diagnostic) === expectedDiagnostic.code &&
57+
diagnostic.source === expectedDiagnostic.source
58+
)
59+
})
60+
})
61+
62+
// Make sure our test case has generated all the diagnostics that should be ignored
63+
/* const hasGeneratedDiagnosticsToIgnore = diagnosticsResult.some(
64+
// There is one file...
65+
([, diagnostics]) =>
66+
// for which all ignored codes...
67+
ignoredCodes.every(
68+
// have at least one corresponding diagnostic
69+
([source, code]) => diagnostics.some(
70+
(diagnostic) => hasSourceWithCode(diagnostic, source, code)
71+
)
72+
)
73+
) */
74+
75+
return hasAllExpectedDiagnostics // && hasGeneratedDiagnosticsToIgnore
4276
})
4377
}).timeout(BITBAKE_TIMEOUT)
4478
})
79+
80+
const getCode = (diagnostic: vscode.Diagnostic): string | number | undefined => {
81+
if (typeof diagnostic.code === 'string' || typeof diagnostic.code === 'number') {
82+
return diagnostic.code
83+
}
84+
return diagnostic.code?.value
85+
}
86+
87+
/* const hasSourceWithCode = (diagnostic: vscode.Diagnostic, source: string, code: string): boolean => {
88+
if (diagnostic.source?.includes(source) !== true) {
89+
return false
90+
}
91+
if (diagnostic.code === code) {
92+
return true
93+
}
94+
if (typeof diagnostic.code === 'object' && diagnostic.code?.value === code) {
95+
return true
96+
}
97+
return false
98+
}
99+
100+
const ignoredCodes = [
101+
['Flake8', 'E203'],
102+
['Flake8', 'E211'],
103+
['Flake8', 'E302'],
104+
['Flake8', 'E303'],
105+
['Flake8', 'E501'],
106+
['Flake8', 'W391'],
107+
['Pylint', 'C0114:missing-module-docstring'],
108+
['Pylint', 'C0116:missing-function-docstring'],
109+
['Pylint', 'C0305:trailing-newlines'],
110+
['Pylint', 'C0415:import-outside-toplevel'],
111+
['Pylint', 'W0104:pointless-statement'],
112+
['Pylint', 'W0106:expression-not-assigned']
113+
] */
114+
115+
const expectedDiagnostics = [
116+
{
117+
range: new vscode.Range(1, 19, 1, 23),
118+
message: 'Using a conditional statement with a constant value',
119+
code: 'W0125:using-constant-test',
120+
source: 'Pylint, bitbake-python'
121+
},
122+
{
123+
range: new vscode.Range(9, 4, 9, 13),
124+
message: 'Redefining name \'os\' (imported by BitBake)',
125+
code: 'W0621:redefined-outer-name',
126+
source: 'Pylint, bitbake-python'
127+
},
128+
{
129+
range: new vscode.Range(9, 4, 9, 13),
130+
message: 'Reimport \'os\' (imported by BitBake)',
131+
code: 'W0404:reimported',
132+
source: 'Pylint, bitbake-python'
133+
},
134+
{
135+
range: new vscode.Range(9, 4, 9, 13),
136+
message: 'Unused import os',
137+
code: 'W0611:unused-import',
138+
source: 'Pylint, bitbake-python'
139+
},
140+
{
141+
range: new vscode.Range(9, 4, 9, 4),
142+
message: '\'os\' imported but unused',
143+
code: 'F401',
144+
source: 'Flake8, bitbake-python'
145+
},
146+
{
147+
range: new vscode.Range(9, 4, 9, 4),
148+
message: 'redefinition of unused \'os\' (imported by BitBake)',
149+
code: 'F811',
150+
source: 'Flake8, bitbake-python'
151+
},
152+
{
153+
range: new vscode.Range(9, 11, 9, 13),
154+
message: '"os" is not accessed',
155+
code: undefined,
156+
source: 'Pylance, bitbake-python'
157+
},
158+
{
159+
range: new vscode.Range(6, 4, 6, 9),
160+
message: 'Undefined variable \'error\'',
161+
code: 'E0602:undefined-variable',
162+
source: 'Pylint, bitbake-python'
163+
},
164+
{
165+
range: new vscode.Range(6, 4, 6, 4),
166+
message: 'undefined name \'error\'',
167+
code: 'F821',
168+
source: 'Flake8, bitbake-python'
169+
},
170+
{
171+
range: new vscode.Range(6, 4, 6, 9),
172+
message: '"error" is not defined',
173+
code: 'reportUndefinedVariable',
174+
source: 'Pylance, bitbake-python'
175+
}
176+
]
177+
178+
// Minimal effort helper to generate the code for the test cases when they need to be updated.
179+
// The output is intented to be copy-pasted into this file.
180+
export const generateTestCases = (diagnosticsResult: ReturnType<typeof vscode.languages.getDiagnostics>): void => {
181+
for (const [uri, diagnostics] of diagnosticsResult) {
182+
console.log(uri)
183+
for (const diagnostic of diagnostics) {
184+
const range = diagnostic.range
185+
const rangeString = `${range.start.line}, ${range.start.character}, ${range.end.line}, ${range.end.character}`
186+
const message = diagnostic.message.replace(/'/g, '\\\'')
187+
const code = getCode(diagnostic)
188+
console.log(' {')
189+
console.log(` range: new vscode.Range(${rangeString}),`)
190+
console.log(` message: '${message}',`)
191+
console.log(` code: ${typeof code === 'string' ? `'${code}'` : code},`)
192+
console.log(` source: '${diagnostic.source}'`)
193+
console.log(' },')
194+
}
195+
console.log(']')
196+
}
197+
}

0 commit comments

Comments
 (0)