'use strict' ; import fs = require ( 'graceful-fs' ) ; import tmp = require ( 'tmp' ) ; import vscode = require ( 'vscode' ) ; import { expect } from 'chai' ; import { after , before , describe , it } from 'mocha' ; import { BazelrcFeatureName } from '../../bazelrc/feature' ; import { BazelFlagSupport } from '../../bazelrc/flags' ; import path = require ( 'path' ) ; import { ConfigurationContext } from '../../common' ; import { FakeMemento } from '../memento' ; import { Container } from '../../container' ; tmp . setGracefulCleanup ( ) ; type flagHoverTest = { d : string ; input : string ; col : number ; match ? : string ; range ? : vscode . Range ; } ; type flagCompletionTest = { d : string ; input : string ; triggerKind ? : vscode . CompletionTriggerKind ; triggerCharacter ? : string ; numItems ? : number ; } ; describe ( BazelrcFeatureName , function ( ) { let support ; const cancellationTokenSource = new vscode . CancellationTokenSource ( ) ; const extensionPath = path . join ( __dirname , '..' , '..' , '..' ) ; const extensionUri = vscode . Uri . file ( extensionPath ) ; const configCtx = new ConfigurationContext ( extensionUri , vscode . Uri . file ( '' ) , new FakeMemento ( ) ) ; Container . initialize ( configCtx , [ ] ) ; before ( async ( ) => { const emitter = new vscode . EventEmitter < void > ( ) ; support = new BazelFlagSupport ( configCtx , emitter . event ) ; emitter . fire ( ) ; } ) ; after ( ( ) => { support . dispose ( ) ; } ) ; describe ( 'hover' , ( ) => { const cases = [ { d : 'should miss empty line' , input : '' , col : 1 , } , { d : 'should miss before token boundary start' , input : ' --config' , col : 1 , } , { d : 'should hit at token boundary start' , input : ' --config' , col : 2 , match : '--config' , range : new vscode . Range ( new vscode . Position ( 0 , 1 ) , new vscode . Position ( 0 , 9 ) ) , } , { d : 'should hit at token boundary end' , input : ' --config' , col : 10 , match : '--config' , } , { d : 'should miss after token boundary end' , input : ' --config' , col : 11 , } , { d : 'should miss before token boundary start (abbrev)' , input : ' -j' , col : 1 , } , { d : 'should hit at token boundary start (abbrev)' , input : ' -j' , col : 2 , match : '--jobs' , range : new vscode . Range ( new vscode . Position ( 0 , 1 ) , new vscode . Position ( 0 , 3 ) ) , } , { d : 'should hit at token boundary end (abbrev)' , input : ' -j' , col : 4 , match : '--jobs' , } , { d : 'should miss after token boundary end (abbrev)' , input : ' -j ' , col : 5 , } , { d : 'also matches inside a comment line' , input : '# --config' , col : 3 , match : '--config' , } , ] ; cases . forEach ( tc => { it ( tc . d , async ( ) => { const filename = tmp . tmpNameSync ( { postfix : '.bazelrc' } ) ; fs . writeFileSync ( filename , tc . input ) ; const uri = vscode . Uri . file ( filename ) ; const document = await vscode . workspace . openTextDocument ( uri ) ; const position = new vscode . Position ( 0 , tc . col - 1 ) ; const hover = await support . provideHover ( document , position , cancellationTokenSource . token ) ; if ( ! tc . match ) { expect ( hover ) . to . be . undefined ; return ; } expect ( hover ) . not . to . be . undefined ; expect ( hover ?. contents . length ) . eq ( 1 ) ; const content = hover ?. contents [ 0 ] as vscode . MarkdownString ; expect ( content . value ) . to . contain ( tc . match ) ; if ( tc . range ) { expect ( tc . range . start . line ) . equals ( hover ?. range ?. start . line ) ; expect ( tc . range . start . character ) . equals ( hover ?. range ?. start . character ) ; expect ( tc . range . end . line ) . equals ( hover ?. range ?. end . line ) ; expect ( tc . range . end . character ) . equals ( hover ?. range ?. end . character ) ; } } ) ; } ) ; } ) ; describe ( 'completion' , ( ) => { const cases = [ { d : 'should miss empty document' , input : '' , numItems : 0 , } , { d : 'should miss comment lines' , input : '# build --' , numItems : 0 , } , { d : 'should report all possible shorts' , input : '  -' , numItems : 7 , } , { d : 'should report all at long + negatables' , input : ' --' , numItems : 1267 , } , { d : 'should report all at negatables' , input : ' --no' , numItems : 436 , } , { d : 'should filter by command name (short option)' , input : 'build -' , numItems : 5 , } , { d : 'should filter by command name (long option)' , input : 'build --' , numItems : 1086 , } , { d : 'should filter by command name and current token' , input : 'build --an' , numItems : 17 , } , { d : 'should provide single match (short)' , input : 'build -j' , numItems : 1 , } , { d : 'should provide single match (long)' , input : 'build --jobs' , numItems : 1 , } , ] ; cases . forEach ( tc => { it ( tc . d , async ( ) => { const filename = tmp . tmpNameSync ( { postfix : '.bazelrc' } ) ; fs . writeFileSync ( filename , tc . input ) ; const uri = vscode . Uri . file ( filename ) ; const document = await vscode . workspace . openTextDocument ( uri ) ; const position = new vscode . Position ( 0 , tc . input . length ) ; const items = await support . provideCompletionItems ( document , position , cancellationTokenSource . token , { triggerKind : tc . triggerKind || vscode . CompletionTriggerKind . Invoke , triggerCharacter : tc . triggerCharacter , } ) ; if ( ! tc . numItems ) { expect ( items ) . to . be . undefined ; return ; } expect ( items ) . not . to . be . undefined ; expect ( items ?. length ) . eq ( tc . numItems ) ; } ) ; } ) ; } ) ; } ) ;