11import { join } from 'path'
22import { afterEach , beforeEach , describe , it , suite } from 'mocha'
3- import { SinonStub , restore , stub } from 'sinon'
3+ import { SinonStub , restore , spy , stub } from 'sinon'
44import { expect } from 'chai'
55import { QuickPickItem , Uri , window } from 'vscode'
66import { buildPipeline } from './util'
77import {
88 bypassProcessManagerDebounce ,
99 closeAllEditors ,
10+ getActiveEditorUpdatedEvent ,
1011 getMockNow
1112} from '../util'
1213import { Disposable } from '../../../extension'
@@ -15,6 +16,7 @@ import * as QuickPick from '../../../vscode/quickPick'
1516import { QuickPickOptionsWithTitle } from '../../../vscode/quickPick'
1617import * as FileSystem from '../../../fileSystem'
1718import { ScriptCommand } from '../../../pipeline'
19+ import * as VscodeContext from '../../../vscode/context'
1820
1921suite ( 'Pipeline Test Suite' , ( ) => {
2022 const disposable = Disposable . fn ( )
@@ -285,4 +287,76 @@ suite('Pipeline Test Suite', () => {
285287 expect ( mockFindOrCreateDvcYamlFile ) . not . to . be . called
286288 } )
287289 } )
290+
291+ it ( 'should set the appropriate context value when a dvc.yaml is open in the active editor' , async ( ) => {
292+ const dvcYaml = Uri . file ( join ( dvcDemoPath , 'dvc.yaml' ) )
293+ await window . showTextDocument ( dvcYaml )
294+
295+ const mockContext : { [ key : string ] : unknown } = {
296+ 'dvc.pipeline.file.active' : false
297+ }
298+
299+ const mockSetContextValue = stub ( VscodeContext , 'setContextValue' )
300+ mockSetContextValue . callsFake ( ( key : string , value : unknown ) => {
301+ mockContext [ key ] = value
302+ return Promise . resolve ( undefined )
303+ } )
304+
305+ const { pipeline } = buildPipeline ( { disposer : disposable } )
306+ await pipeline . isReady ( )
307+
308+ expect (
309+ mockContext [ 'dvc.pipeline.file.active' ] ,
310+ 'should set dvc.pipeline.file.active to true when a dvc.yaml is open and the extension starts'
311+ ) . to . be . true
312+
313+ mockSetContextValue . resetHistory ( )
314+
315+ const startupEditorClosed = getActiveEditorUpdatedEvent ( disposable )
316+
317+ await closeAllEditors ( )
318+ await startupEditorClosed
319+
320+ expect (
321+ mockContext [ 'dvc.pipeline.file.active' ] ,
322+ 'should set dvc.pipeline.file.active to false when the dvc.yaml in the active editor is closed'
323+ ) . to . be . false
324+
325+ mockSetContextValue . resetHistory ( )
326+
327+ const activeEditorUpdated = getActiveEditorUpdatedEvent ( disposable )
328+
329+ await window . showTextDocument ( dvcYaml )
330+ await activeEditorUpdated
331+
332+ const activeEditorClosed = getActiveEditorUpdatedEvent ( disposable )
333+
334+ expect (
335+ mockContext [ 'dvc.pipeline.file.active' ] ,
336+ 'should set dvc.pipeline.file.active to true when a dvc.yaml file is in the active editor'
337+ ) . to . be . true
338+
339+ await closeAllEditors ( )
340+ await activeEditorClosed
341+
342+ expect (
343+ mockContext [ 'dvc.pipeline.file.active' ] ,
344+ 'should set dvc.pipeline.file.active to false when the dvc.yaml in the active editor is closed again'
345+ ) . to . be . false
346+ } )
347+
348+ it ( 'should set dvc.pipeline.file.active to false when a dvc.yaml is not open and the extension starts' , async ( ) => {
349+ const nonDvcYaml = Uri . file ( join ( dvcDemoPath , '.gitignore' ) )
350+ await window . showTextDocument ( nonDvcYaml )
351+
352+ const setContextValueSpy = spy ( VscodeContext , 'setContextValue' )
353+
354+ const { pipeline } = buildPipeline ( { disposer : disposable } )
355+ await pipeline . isReady ( )
356+
357+ expect ( setContextValueSpy ) . to . be . calledWith (
358+ 'dvc.pipeline.file.active' ,
359+ false
360+ )
361+ } )
288362} )
0 commit comments