import * as fse from 'fs-extra' ; import * as mocha from 'mocha' ; import * as path from 'path' ; import * as rimraf from 'rimraf' ; import * as vscode from 'vscode' ; import { parseError } from 'vscode-azureextensionui' ; import { armTemplateLanguageId , configKeys , configPrefix , ext , stopArmLanguageServer } from "../extension.bundle" ; import { displayCacheStatus } from './support/cache' ; import { delay } from "./support/delay" ; import { ensureExtensionHasInitialized } from './support/ensureExtensionHasInitialized' ; import { publishVsCodeLogs } from './support/publishVsCodeLogs' ; import { alwaysEchoTestLog , deleteTestLog , getTestLogContents , setTestLogOutputFile , writeToError , writeToLog } from './support/testLog' ; import { useTestSnippets } from './support/TestSnippets' ; import { logsFolder } from './testConstants' ; import { useTestFunctionMetadata } from "./TestData" ; let previousSettings = { autoDetectJsonTemplates : < boolean | undefined > undefined , fileAssociations : < { [ key ] : string ; } > { } } ; var _bailed = false ; export function bail ( ) { _bailed = true ; } suiteSetup ( async function ( this ) : Promise < void > { writeToLog ( ">>> suiteSetup" , true ) ; const timeout = 5 * 60 * 1000 ; this . timeout ( timeout ) ; if ( await fse . pathExists ( logsFolder ) ) { rimraf . sync ( logsFolder ) ; } await fse . mkdir ( logsFolder ) ; setTestLogOutputFile ( path . join ( logsFolder , "testlog.txt" ) ) ; await displayCacheStatus ( ) ; process . env . ARM_DOTNET_INSTALL_FOLDER = ".dotnet O'Hare O'Donald" ; useTestFunctionMetadata ( ) ; useTestSnippets ( ) ; ext . addCompletedDiagnostic = true ; vscode . commands . executeCommand ( 'workbench.actions.view.problems' ) ; writeToLog ( "Updating user settings" ) ; previousSettings . autoDetectJsonTemplates = vscode . workspace . getConfiguration ( configPrefix ) . get < boolean > ( configKeys . autoDetectJsonTemplates ) ; await vscode . workspace . getConfiguration ( configPrefix ) . update ( configKeys . autoDetectJsonTemplates , true , vscode . ConfigurationTarget . Global ) ; let fileAssociations = previousSettings . fileAssociations = Object . assign ( { } , vscode . workspace . getConfiguration ( 'files' ) . get < { } > ( 'associations' ) ) ; console . warn ( "Old file associations:" , fileAssociations ) ; let newAssociations = Object . assign ( { } , fileAssociations , { '*.azrm' : armTemplateLanguageId } ) ; vscode . workspace . getConfiguration ( 'files' , null ) . update ( 'associations' , newAssociations , vscode . ConfigurationTarget . Global ) ; await delay ( 5 * 1000 ) ; const confirmedNewAssociations = Object . assign ( { } , vscode . workspace . getConfiguration ( 'files' ) . get < { } > ( 'associations' ) ) ; console . warn ( "Confirmed new file associations:" , confirmedNewAssociations ) ; await ensureExtensionHasInitialized ( timeout * 0.95 ) ; writeToLog ( 'Done: global.test.ts: suiteSetup' ) ; } ) ; suiteTeardown ( async function ( this ) : Promise < void > { writeToLog ( 'suiteTeardown' , true ) ; await displayCacheStatus ( ) ; if ( ext . extensionStartupComplete ) { await publishVsCodeLogs ( 'ms-dotnettools.vscode-dotnet-runtime' ) ; await publishVsCodeLogs ( path . basename ( ext . context . logPath ) ) ; await publishVsCodeLogs ( undefined ) ; await stopArmLanguageServer ( ) ; writeToLog ( "Tests complete." , true ) ; } else { console . warn ( "Cannot publish logs because extension context is not available (startup didn't complete)" ) ; } } ) ; setup ( function ( this ) : void { writeToLog ( `Running test: ${ this . currentTest ! . title }` , true ) ; } ) ; teardown ( function ( this ) : void { let message ; const failed = ( ! this . currentTest ! . state || this . currentTest ! . state === 'failed' ) ; if ( failed ) { if ( getTestLogContents ( ) ) { message = `Test Failed: ${ this . currentTest ! . title }:` ; message += `\n  ${ parseError ( this . currentTest ?. err ) . message }` ; } else { message = `Test Failed: (test log is empty): ${ this . currentTest ! . title }` ; message += `\n  ${ parseError ( this . currentTest ?. err ) . message }` ; } } else { message = `Test passed: ${ this . currentTest ! . title }\n` ; } if ( alwaysEchoTestLog ) { message += `TEST LOG:\n${ getTestLogContents ( ) }\n` ; } if ( failed ) { writeToError ( message ) ; } else { writeToLog ( message ) ; } if ( _bailed || this . currentTest ! . timedOut || parseError ( this . currentTest ?. err ) . message . match ( /timed/i ) ) { throw new Error ( `A test timed out ('${ this . currentTest ! . title }'), so exiting out of the rest of the tests because tests tend to be unstable after a timeout` ) ; } deleteTestLog ( ) ; } ) ;