Skip to content

Commit 2150d00

Browse files
authored
Accomodate new DVCLive signal file format (#4565)
1 parent d7b0c58 commit 2150d00

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

extension/src/fileSystem/index.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ensureDirSync,
55
ensureFileSync,
66
remove,
7+
readFileSync,
78
writeFileSync
89
} from 'fs-extra'
910
import { TextDocument, window, workspace } from 'vscode'
@@ -18,27 +19,34 @@ import {
1819
writeJson,
1920
writeCsv,
2021
writeTsv,
21-
isPathInProject
22+
isPathInProject,
23+
getPidFromFile
2224
} from '.'
2325
import { dvcDemoPath } from '../test/util'
2426
import { DOT_DVC } from '../cli/dvc/constants'
2527
import { ScriptCommand } from '../pipeline'
28+
import { processExists } from '../process/execution'
2629

2730
jest.mock('../cli/dvc/reader')
31+
jest.mock('../process/execution')
2832
jest.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+
3946
const mockedAppendFileSync = jest.mocked(appendFileSync)
4047
const mockedEnsureFileSync = jest.mocked(ensureFileSync)
4148
const mockedWriteFileSync = jest.mocked(writeFileSync)
49+
const mockedReadFileSync = jest.mocked(readFileSync)
4250
const mockedWorkspace = jest.mocked(workspace)
4351
const mockedWindow = jest.mocked(window)
4452
const 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+
})

extension/src/fileSystem/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ export const writeTsv = async (
252252
return writeFileSync(path, csv)
253253
}
254254

255+
const getPid = (contents: string): number | undefined => {
256+
try {
257+
const { pid } = JSON.parse(contents) as { pid?: string }
258+
if (pid) {
259+
return createValidInteger(pid)
260+
}
261+
} catch {}
262+
return createValidInteger(contents)
263+
}
264+
255265
export const getPidFromFile = async (
256266
path: string
257267
): Promise<number | undefined> => {
@@ -260,7 +270,7 @@ export const getPidFromFile = async (
260270
}
261271

262272
const contents = readFileSync(path).toString()
263-
const pid = createValidInteger(contents)
273+
const pid = getPid(contents)
264274

265275
if (!pid || !(await processExists(pid))) {
266276
removeSync(path)

0 commit comments

Comments
 (0)