Skip to content

Commit

Permalink
add tests for new dvc reader functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Mar 10, 2023
1 parent 22fe748 commit c5f7eaa
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
52 changes: 51 additions & 1 deletion extension/src/cli/dvc/reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const mockedEnv = {
}
const JSON_FLAG = '--json'

const mockedGetPythonBinPath = jest.fn()

beforeEach(() => {
jest.resetAllMocks()
mockedGetProcessEnv.mockReturnValueOnce(mockedEnv)
Expand All @@ -49,7 +51,7 @@ describe('CliReader', () => {
const dvcReader = new DvcReader(
{
getCliPath: () => undefined,
getPythonBinPath: () => undefined
getPythonBinPath: mockedGetPythonBinPath
} as unknown as Config,
{
processCompleted: {
Expand Down Expand Up @@ -144,6 +146,33 @@ describe('CliReader', () => {
})
})

describe('globalVersion', () => {
it('should call execute process with the correct parameters (does not respect pythonBinPath)', async () => {
const cwd = __dirname
const stdout = '3.9.11'
mockedCreateProcess.mockReturnValueOnce(getMockedProcess(stdout))
mockedGetPythonBinPath.mockReturnValueOnce('python')
const output = await dvcReader.globalVersion(cwd)

expect(output).toStrictEqual(stdout)
expect(mockedCreateProcess).toHaveBeenCalledWith({
args: ['--version'],
cwd,
env: mockedEnv,
executable: 'dvc'
})
})

it('should not retry if the process fails (cannot find cli - extension should reset)', async () => {
const cwd = __dirname
mockedCreateProcess.mockImplementationOnce(() => {
throw new Error('spawn dvc ENOENT retrying...')
})

await expect(dvcReader.globalVersion(cwd)).rejects.toBeTruthy()
})
})

describe('plotsDiff', () => {
it('should handle empty output being returned', async () => {
const cwd = __dirname
Expand Down Expand Up @@ -229,6 +258,27 @@ describe('CliReader', () => {
})
})

it('should respect the pythonBinPath parameters', async () => {
const cwd = __dirname
const stdout = '2.47.0'
const mockedPythonPath = 'some/python/path'
const mockedPythonBinPath = [mockedPythonPath, 'python'].join('/')
mockedGetPythonBinPath.mockReturnValue(mockedPythonBinPath)
mockedCreateProcess.mockReturnValueOnce(getMockedProcess(stdout))
const output = await dvcReader.version(cwd)

expect(output).toStrictEqual(stdout)
expect(mockedCreateProcess).toHaveBeenCalledWith({
args: ['-m', 'dvc', '--version'],
cwd,
env: {
...mockedEnv,
PATH: [mockedPythonPath, mockedEnv.PATH].join(':')
},
executable: mockedPythonBinPath
})
})

it('should not retry if the process fails (cannot find cli - extension should reset)', async () => {
const cwd = __dirname
mockedCreateProcess.mockImplementationOnce(() => {
Expand Down
28 changes: 28 additions & 0 deletions extension/src/test/suite/setup/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,5 +605,33 @@ suite('Setup Test Suite', () => {
'should warn the user if the CLI throws an error'
).to.be.calledOnce
})

it('should call the CLI to see if there is a global version of DVC available if the Python version fails', async () => {
const {
config,
mockExecuteCommand,
mockGlobalVersion,
mockRunSetup,
mockVersion,
setup
} = buildSetup(disposable, true, false, false)

mockExecuteCommand.restore()
mockRunSetup.restore()
stub(config, 'isPythonExtensionUsed').returns(true)

mockVersion.resetBehavior()
mockVersion.rejects(new Error('no CLI here'))

const executeCommandSpy = spy(commands, 'executeCommand')
await run(setup)

expect(mockVersion).to.be.calledOnce
expect(mockGlobalVersion).to.be.calledOnce
expect(
executeCommandSpy,
'should set dvc.cli.incompatible to false if the version is compatible'
).to.be.calledWithExactly('setContext', 'dvc.cli.incompatible', false)
})
})
})

0 comments on commit c5f7eaa

Please sign in to comment.