import * as assert from 'assert' ; import * as path from 'path' ; import * as CompareVersions from 'compare-versions' ; import * as vscode from 'vscode' ; import { waitForSonarLintDiagnostics } from '../common/util' ; const sampleFolderLocation = '../../../samples/' ; const sampleJavaFolderLocation = '../../../samples/sample-java-maven-multi-module/' ; suite ( 'Java Test Suite' , ( ) => { vscode . window . showInformationMessage ( 'Start java tests.' ) ; test ( 'Extension should be present' , ( ) => { assert . ok ( vscode . extensions . getExtension ( 'sonarsource.sonarlint-vscode' ) ) ; } ) ; test ( 'Java extension should be present' , ( ) => { assert . ok ( vscode . extensions . getExtension ( 'redhat.java' ) ) ; } ) ; test ( 'should report issue on java file' , async function ( ) { const vscodeJavaVersion = vscode . extensions . getExtension ( 'redhat.java' ) ?. packageJSON . version ; if ( CompareVersions . compare ( vscodeJavaVersion , '0.56' , '>=' ) ) { const fileUri = vscode . Uri . file ( path . join ( __dirname , sampleFolderLocation , 'sample-java-maven-multi-module' , 'module-java/src/main/java/edu/marcelo/App.java' ) ) ; const document = await vscode . workspace . openTextDocument ( fileUri ) ; const editor = await vscode . window . showTextDocument ( document ) ; const diags = await waitForSonarLintDiagnostics ( fileUri ) ; assert . deepEqual ( diags . map ( d => [ d . code , d . message ] ) , [ [ 'java:S1130' , 'Remove the declaration of thrown exception \'edu.marcelo.App$MyException\', as it cannot be thrown from method\'s body.' ] , [ 'java:S106' , 'Replace this use of System.out or System.err by a logger.' ] ] ) ; const rangeInMiddleOfThrowsMyException = new vscode . Range ( 8 , 54 , 8 , 54 ) ; const codeActionsResult = ( await vscode . commands . executeCommand < ( vscode . Command | vscode . CodeAction ) [ ] > ( 'vscode.executeCodeActionProvider' , document . uri , rangeInMiddleOfThrowsMyException , vscode . CodeActionKind . QuickFix . value ) ) ! ; const expectedActionTitles = [ 'SonarLint: Remove "MyException"' , "SonarLint: Open description of rule 'java:S1130'" , "SonarLint: Deactivate rule 'java:S1130'" ] ; const actualCodeActionTitles = codeActionsResult . filter ( c => expectedActionTitles . indexOf ( c . title ) >= 0 ) . map ( c => c . title ) ; assert . deepEqual ( actualCodeActionTitles , expectedActionTitles ) ; const quickFix = codeActionsResult . filter ( c => c . title === 'SonarLint: Remove "MyException"' ) [ 0 ] as vscode . CodeAction ; const fixApplied = await vscode . workspace . applyEdit ( quickFix . edit ! ) ; assert . equal ( fixApplied , true ) ; vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ; } else { this . skip ( ) ; } } ) . timeout ( 60 * 1000 ) ; test ( 'should report issue on single java test file' , async function ( ) { const vscodeJavaVersion = vscode . extensions . getExtension ( 'redhat.java' ) ?. packageJSON . version ; if ( CompareVersions . compare ( vscodeJavaVersion , '0.56' , '>=' ) ) { const fileUri = vscode . Uri . file ( path . join ( __dirname , sampleJavaFolderLocation , 'module-java/src/test/java/edu/marcelo' , 'AppTest.java' ) ) ; const document = await vscode . workspace . openTextDocument ( fileUri ) ; const editor = await vscode . window . showTextDocument ( document ) ; const diags = await waitForSonarLintDiagnostics ( fileUri ) ; assert . deepEqual ( diags . length , 1 ) ; assert . equal ( diags [ 0 ] . message , 'Add at least one assertion to this test case.' ) ; vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ; } else { this . skip ( ) ; } } ) . timeout ( 120 * 1000 ) ; } ) ;