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 { VCSCompiler } from '../compiling/VCSCompiler' ; import { getPathFromUri } from '../utils/common' ; const testFolderLocation = '../../src/test/test-files/VCSCompiler.test' ; const file_path_placeholder = 'FILEPATH_PLACEHOLDER' ; let diagnosticCollection ; const documentCompiler = new VCSCompiler ( undefined , undefined , undefined , undefined , undefined ) ; suite ( 'VCSCompiler 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.stdout.txt' ) ; let stdout = fs . readFileSync ( stderrFile ) . toString ( ) ; stdout = stderrSetUp ( stdout , compiledFilePath ) ; documentCompiler . parseDiagnostics ( undefined , stdout , undefined , document , compiledFilePath , diagnosticCollection ) ; const collection = diagnosticCollection . get ( document . uri ) ; assert . strictEqual ( collection . length , 4 ) ; collection . forEach ( ( diagnostic ) => { if ( diagnostic . severity !== DiagnosticSeverity . Error ) { assert . fail ( ) ; } } ) ; } ) ; test ( 'test #2: Diagnostics for empty stdout' , async ( ) => { diagnosticCollection = new Map ( ) ; const uriDoc = Uri . file ( path . join ( __dirname , testFolderLocation , 'baz.sv' ) ) ; 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 ( ) ; } } ) ; } ) ; function stderrSetUp ( stdout , rootPath ) { const regex = new RegExp ( file_path_placeholder , 'g' ) ; return stdout . 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 ) ; }