44 ensureDirSync ,
55 ensureFileSync ,
66 remove ,
7+ readFileSync ,
78 writeFileSync
89} from 'fs-extra'
910import { TextDocument , window , workspace } from 'vscode'
@@ -18,27 +19,34 @@ import {
1819 writeJson ,
1920 writeCsv ,
2021 writeTsv ,
21- isPathInProject
22+ isPathInProject ,
23+ getPidFromFile
2224} from '.'
2325import { dvcDemoPath } from '../test/util'
2426import { DOT_DVC } from '../cli/dvc/constants'
2527import { ScriptCommand } from '../pipeline'
28+ import { processExists } from '../process/execution'
2629
2730jest . mock ( '../cli/dvc/reader' )
31+ jest . mock ( '../process/execution' )
2832jest . mock ( 'fs-extra' , ( ) => {
2933 const actualModule = jest . requireActual ( 'fs-extra' )
3034 return {
3135 __esModule : true ,
3236 ...actualModule ,
3337 appendFileSync : jest . fn ( ) ,
3438 ensureFileSync : jest . fn ( ) ,
39+ readFileSync : jest . fn ( ) ,
3540 writeFileSync : jest . fn ( )
3641 }
3742} )
3843
44+ const mockedProcessExists = jest . mocked ( processExists )
45+
3946const mockedAppendFileSync = jest . mocked ( appendFileSync )
4047const mockedEnsureFileSync = jest . mocked ( ensureFileSync )
4148const mockedWriteFileSync = jest . mocked ( writeFileSync )
49+ const mockedReadFileSync = jest . mocked ( readFileSync )
4250const mockedWorkspace = jest . mocked ( workspace )
4351const mockedWindow = jest . mocked ( window )
4452const mockedOpenTextDocument = jest . fn ( )
@@ -474,3 +482,39 @@ describe('isPathInProject', () => {
474482 expect ( isPathInProject ( path , dvcRoot , subProjects ) ) . toBe ( true )
475483 } )
476484} )
485+
486+ describe ( 'getPidFromFile' , ( ) => {
487+ it ( 'should handle a file containing a number' , async ( ) => {
488+ mockedReadFileSync . mockReturnValueOnce ( Buffer . from ( '3675' ) )
489+ mockedProcessExists . mockResolvedValueOnce ( true )
490+ const pid = await getPidFromFile ( __filename )
491+
492+ expect ( mockedReadFileSync ) . toHaveBeenCalledTimes ( 1 )
493+ expect ( mockedProcessExists ) . toHaveBeenCalledTimes ( 1 )
494+ expect ( pid ) . toBe ( 3675 )
495+ } )
496+
497+ it ( 'should handle a file containing a JSON object with a numeric pid' , async ( ) => {
498+ mockedReadFileSync . mockReturnValueOnce (
499+ Buffer . from ( JSON . stringify ( { pid : 3676 } ) )
500+ )
501+ mockedProcessExists . mockResolvedValueOnce ( true )
502+ const pid = await getPidFromFile ( __filename )
503+
504+ expect ( mockedReadFileSync ) . toHaveBeenCalledTimes ( 1 )
505+ expect ( mockedProcessExists ) . toHaveBeenCalledTimes ( 1 )
506+ expect ( pid ) . toBe ( 3676 )
507+ } )
508+
509+ it ( 'should handle a file containing a JSON object with a string pid' , async ( ) => {
510+ mockedReadFileSync . mockReturnValueOnce (
511+ Buffer . from ( JSON . stringify ( { pid : '3676' } ) )
512+ )
513+ mockedProcessExists . mockResolvedValueOnce ( true )
514+ const pid = await getPidFromFile ( __filename )
515+
516+ expect ( mockedReadFileSync ) . toHaveBeenCalledTimes ( 1 )
517+ expect ( mockedProcessExists ) . toHaveBeenCalledTimes ( 1 )
518+ expect ( pid ) . toBe ( 3676 )
519+ } )
520+ } )
0 commit comments