import { Diagnostic , DiagnosticSeverity , Range , Position } from 'vscode-languageserver/node' ; import { TextDocument } from 'vscode-languageserver-textdocument' ; import * as vscode from 'vscode' ; import { Uri , workspace } from 'vscode' ; import * as path from 'path' ; import * as fs from 'fs' ; import * as assert from 'assert' ; import { VerilatorCompiler } from '../compiling/VerilatorCompiler' ; import { getPathFromUri } from '../utils/common' ; const testFolderLocation = '../../src/test/test-files/VerilatorCompiler.test' ; const file_path_placeholder = 'FILEPATH_PLACEHOLDER' ; let diagnosticCollection ; const documentCompiler = new VerilatorCompiler ( undefined , undefined , undefined , undefined , undefined ) ; suite ( 'VerilatorCompiler Tests' , ( ) => { test ( 'test #1: Diagnostics from %Error' , async ( ) => { diagnosticCollection = new Map ( ) ; const filePath = path . join ( __dirname , testFolderLocation , 'foo.sv' ) ; const uriDoc = Uri . file ( filePath ) ; const documentWorkspace = await workspace . openTextDocument ( uriDoc ) ; const document = castTextDocument ( documentWorkspace ) ; const compiledFilePath = getPathFromUri ( document . uri , __dirname ) ; const stderrFile = path . join ( __dirname , testFolderLocation , 'foo.stderr.txt' ) ; let stderr = fs . readFileSync ( stderrFile ) . toString ( ) ; stderr = stderrSetUp ( stderr , compiledFilePath ) ; documentCompiler . parseDiagnostics ( undefined , undefined , stderr , document , compiledFilePath , diagnosticCollection ) ; const collection = diagnosticCollection . get ( document . uri ) ; assert . strictEqual ( collection . length , 6 ) ; collection . forEach ( ( diagnostic ) => { if ( diagnostic . severity !== DiagnosticSeverity . Error ) { assert . fail ( ) ; } } ) ; } ) ; test ( 'test #2: Diagnostics from %Warning, %Warning-<flag>' , async ( ) => { diagnosticCollection = new Map ( ) ; const filePath = path . join ( __dirname , testFolderLocation , 'bar.sv' ) ; const uriDoc = Uri . file ( filePath ) ; const documentWorkspace = await workspace . openTextDocument ( uriDoc ) ; const document = castTextDocument ( documentWorkspace ) ; const compiledFilePath = getPathFromUri ( document . uri , __dirname ) ; const stderrFile = path . join ( __dirname , testFolderLocation , 'bar.stderr.txt' ) ; let stderr = fs . readFileSync ( stderrFile ) . toString ( ) ; stderr = stderrSetUp ( stderr , compiledFilePath ) ; documentCompiler . parseDiagnostics ( undefined , undefined , stderr , document , compiledFilePath , diagnosticCollection ) ; const collection = diagnosticCollection . get ( document . uri ) ; assert . strictEqual ( collection . length , 7 ) ; collection . forEach ( ( diagnostic ) => { if ( diagnostic . severity !== DiagnosticSeverity . Warning && diagnostic . severity !== DiagnosticSeverity . Information ) { assert . fail ( ) ; } } ) ; } ) ; test ( 'test #3: Diagnostics for empty stderr' , async ( ) => { diagnosticCollection = new Map ( ) ; const filePath = 'baz.sv' ; const uriDoc = Uri . file ( path . join ( __dirname , testFolderLocation , filePath ) ) ; const documentWorkspace = await workspace . openTextDocument ( uriDoc ) ; const document = castTextDocument ( documentWorkspace ) ; const compiledFilePath = getPathFromUri ( document . uri , __dirname ) ; documentCompiler . parseDiagnostics ( undefined , undefined , '' , document , compiledFilePath , diagnosticCollection ) ; const collection = diagnosticCollection . get ( document . uri ) ; if ( collection && collection . length > 0 ) { assert . fail ( ) ; } } ) ; test ( 'test #4: test skipCannotFindModuleTrailingErrors()' , async ( ) => { let filePath = 'bar.stderr.txt' ; let stderrFile = path . join ( __dirname , testFolderLocation , filePath ) ; let uriDoc = Uri . file ( path . join ( __dirname , testFolderLocation , filePath ) ) ; let document = await workspace . openTextDocument ( uriDoc ) ; let compiledFilePath = getPathFromUri ( document . uri . toString ( ) , __dirname ) ; let stderr = fs . readFileSync ( stderrFile ) . toString ( ) ; stderr = stderrSetUp ( stderr , compiledFilePath ) ; stderr = stderr . replace ( /\r\n|\n|\r/g , '\n' ) ; let errors = stderr . split ( /\n/g ) ; let i = documentCompiler . skipCannotFindModuleTrailingErrors ( errors , 0 , 'test' ) ; assert . strictEqual ( i , 0 ) ; filePath = 'qux.sv' ; stderrFile = path . join ( __dirname , testFolderLocation , 'qux.stderr.txt' ) ; uriDoc = Uri . file ( path . join ( __dirname , testFolderLocation , filePath ) ) ; document = await workspace . openTextDocument ( uriDoc ) ; compiledFilePath = getPathFromUri ( document . uri . toString ( ) , __dirname ) ; stderr = fs . readFileSync ( stderrFile ) . toString ( ) ; stderr = stderrSetUp ( stderr , compiledFilePath ) ; stderr = stderr . replace ( /\r\n|\n|\r/g , '\n' ) ; errors = stderr . split ( /\r|\n/g ) ; i = documentCompiler . skipCannotFindModuleTrailingErrors ( errors , 2 , 'externalModule' ) ; assert . strictEqual ( i , 10 ) ; } ) ; } ) ; function stderrSetUp ( stderr , rootPath ) { const regex = new RegExp ( file_path_placeholder , 'g' ) ; return stderr . replace ( regex , rootPath ) ; } param document returns function castTextDocument ( document ) { return { uri : document . uri . fsPath , languageId : document . languageId , version : document . version , getText ( range ? ) { return document . getText ( castRange ( range ) ) ; } , lineCount : document . lineCount , positionAt ( offset ) { const position = document . positionAt ( offset ) ; return { line : position . line , character : position . character } ; } , offsetAt ( position ) { return document . offsetAt ( castPosition ( position ) ) ; } } ; } param document returns function castRange ( range ) { const startOld = range . start ; const endOld = range . end ; const start = new vscode . Position ( startOld . line , startOld . character ) ; const end = new vscode . Position ( endOld . line , endOld . character ) ; return new vscode . Range ( start , end ) ; } param document returns function castPosition ( position ) { return new vscode . Position ( position . line , position . character ) ; }