Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python Extension's "Create Env" command into DVC Setup #4058

Merged
merged 6 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extension/src/extensions/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export const getOnDidChangePythonExecutionDetails = async () => {

export const isPythonExtensionInstalled = () => isInstalled(PYTHON_EXTENSION_ID)

export const createPythonEnv = () => {
void commands.executeCommand('python.createEnvironment')
}

export const selectPythonInterpreter = () => {
void commands.executeCommand('python.setInterpreter')
}
16 changes: 14 additions & 2 deletions extension/src/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { validateTokenInput } from './inputBox'
import { findPythonBinForInstall } from './autoInstall'
import { run, runWithRecheck, runWorkspace } from './runner'
import { isStudioAccessToken } from './token'
import { pickFocusedProjects } from './quickPick'
import { pickFocusedProjects, pickPythonExtensionAction } from './quickPick'
import { ViewKey } from '../webview/constants'
import { BaseRepository } from '../webview/repository'
import { Resource } from '../resourceLocator'
Expand Down Expand Up @@ -56,6 +56,7 @@ import { Title } from '../vscode/title'
import { getDVCAppDir } from '../util/appdirs'
import { getOptions } from '../cli/dvc/options'
import { isAboveLatestTestedVersion } from '../cli/dvc/version'
import { createPythonEnv, selectPythonInterpreter } from '../extensions/python'

export class Setup
extends BaseRepository<TSetupData>
Expand Down Expand Up @@ -407,7 +408,8 @@ export class Setup
const webviewMessages = new WebviewMessages(
() => this.getWebview(),
() => this.initializeGit(),
(offline: boolean) => this.updateStudioOffline(offline)
(offline: boolean) => this.updateStudioOffline(offline),
() => this.updatePythonEnvironment()
)
this.dispose.track(
this.onDidReceivedWebviewMessage(message =>
Expand Down Expand Up @@ -499,6 +501,16 @@ export class Setup
}
}

private async updatePythonEnvironment() {
const value = await pickPythonExtensionAction()

if (!value) {
return
}

return value === 1 ? createPythonEnv() : selectPythonInterpreter()
julieg18 marked this conversation as resolved.
Show resolved Hide resolved
}

private needsGitCommit(needsGitInit: boolean) {
if (needsGitInit) {
return true
Expand Down
30 changes: 28 additions & 2 deletions extension/src/setup/quickPick.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { join } from 'path'
import { pickFocusedProjects } from './quickPick'
import { quickPickManyValues } from '../vscode/quickPick'
import { pickFocusedProjects, pickPythonExtensionAction } from './quickPick'
import { quickPickManyValues, quickPickValue } from '../vscode/quickPick'
import { Toast } from '../vscode/toast'
import { Title } from '../vscode/title'

jest.mock('../vscode/quickPick')

const mockedQuickPickManyValues = jest.mocked(quickPickManyValues)
const mockedQuickValue = jest.mocked(quickPickValue)

const mockedToast = jest.mocked(Toast)
const mockedShowError = jest.fn()
Expand Down Expand Up @@ -55,3 +56,28 @@ describe('pickFocusedProjects', () => {
)
})
})

describe('pickPythonExtensionAction', () => {
it('should call a quick pick with the correct values', () => {
void pickPythonExtensionAction()

expect(mockedQuickValue).toHaveBeenCalledWith(
[
{
description: 'Create an environment',
label: 'Create',
value: 1
},
{
description: 'Choose from already created environments',
label: 'Select',
value: 2
}
],
{
placeHolder: 'Select or Create a Python Environment',
title: Title.UPDATE_PYTHON_ENVIRONMENT
}
)
})
})
21 changes: 20 additions & 1 deletion extension/src/setup/quickPick.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sortCollectedArray } from '../util/array'
import { quickPickManyValues } from '../vscode/quickPick'
import { quickPickManyValues, quickPickValue } from '../vscode/quickPick'
import { Title } from '../vscode/title'
import { Toast } from '../vscode/toast'

Expand All @@ -26,3 +26,22 @@ export const pickFocusedProjects = async (

return sortCollectedArray(values)
}

export const pickPythonExtensionAction = () => {
const options = [
{
description: 'Create an environment',
label: 'Create',
value: 1
},
{
description: 'Choose from already created environments',
label: 'Select',
value: 2
}
]
return quickPickValue<number>(options, {
placeHolder: 'Select or Create a Python Environment',
title: Title.UPDATE_PYTHON_ENVIRONMENT
})
}
16 changes: 9 additions & 7 deletions extension/src/setup/webview/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { BaseWebview } from '../../webview'
import { sendTelemetryEvent } from '../../telemetry'
import { EventName } from '../../telemetry/constants'
import { selectPythonInterpreter } from '../../extensions/python'
import { autoInstallDvc, autoUpgradeDvc } from '../autoInstall'
import {
RegisteredCliCommands,
Expand All @@ -20,15 +19,18 @@ export class WebviewMessages {
private readonly getWebview: () => BaseWebview<TSetupData> | undefined
private readonly initializeGit: () => void
private readonly updateStudioOffline: (offline: boolean) => Promise<void>
private readonly updatePythonEnv: () => Promise<void>

constructor(
getWebview: () => BaseWebview<TSetupData> | undefined,
initializeGit: () => void,
updateStudioOffline: (shareLive: boolean) => Promise<void>
updateStudioOffline: (shareLive: boolean) => Promise<void>,
updatePythonEnv: () => Promise<void>
) {
this.getWebview = getWebview
this.initializeGit = initializeGit
this.updateStudioOffline = updateStudioOffline
this.updatePythonEnv = updatePythonEnv
}

public sendWebviewMessage({
Expand Down Expand Up @@ -79,8 +81,8 @@ export class WebviewMessages {
return commands.executeCommand(RegisteredCommands.EXTENSION_GET_STARTED)
case MessageFromWebviewType.SHOW_SCM_PANEL:
return this.showScmForCommit()
case MessageFromWebviewType.SELECT_PYTHON_INTERPRETER:
return this.selectPythonInterpreter()
case MessageFromWebviewType.UPDATE_PYTHON_ENVIRONMENT:
return this.updatePythonEnvironment()
case MessageFromWebviewType.INSTALL_DVC:
return this.installDvc()
case MessageFromWebviewType.UPGRADE_DVC:
Expand Down Expand Up @@ -131,13 +133,13 @@ export class WebviewMessages {
return commands.executeCommand('workbench.view.scm')
}

private selectPythonInterpreter() {
private updatePythonEnvironment() {
sendTelemetryEvent(
EventName.VIEWS_SETUP_SELECT_PYTHON_INTERPRETER,
EventName.VIEWS_SETUP_UPDATE_PYTHON_ENVIRONMENT,
undefined,
undefined
)
return selectPythonInterpreter()
return this.updatePythonEnv()
}

private upgradeDvc() {
Expand Down
6 changes: 3 additions & 3 deletions extension/src/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ export const EventName = Object.assign(
VIEWS_SETUP_FOCUS_CHANGED: 'views.setup.focusChanged',
VIEWS_SETUP_INIT_GIT: 'views.setup.initializeGit',
VIEWS_SETUP_INSTALL_DVC: 'views.setup.installDvc',
VIEWS_SETUP_SELECT_PYTHON_INTERPRETER:
'views.setup.selectPythonInterpreter',
VIEWS_SETUP_SHOW_SCM_FOR_COMMIT: 'views.setup.showScmForCommit',
VIEWS_SETUP_UPDATE_PYTHON_ENVIRONMENT:
'views.setup.updatePythonEnvironment',
VIEWS_SETUP_UPGRADE_DVC: 'view.setup.upgradeDvc',

VIEWS_TERMINAL_CLOSED: 'views.terminal.closed',
Expand Down Expand Up @@ -277,7 +277,7 @@ export interface IEventNamePropertyMapping {
[EventName.VIEWS_SETUP_CLOSE]: undefined
[EventName.VIEWS_SETUP_CREATED]: undefined
[EventName.VIEWS_SETUP_FOCUS_CHANGED]: undefined
[EventName.VIEWS_SETUP_SELECT_PYTHON_INTERPRETER]: undefined
[EventName.VIEWS_SETUP_UPDATE_PYTHON_ENVIRONMENT]: undefined
[EventName.VIEWS_SETUP_SHOW_SCM_FOR_COMMIT]: undefined
[EventName.VIEWS_SETUP_INIT_GIT]: undefined
[EventName.VIEWS_SETUP_INSTALL_DVC]: undefined
Expand Down
65 changes: 60 additions & 5 deletions extension/src/test/suite/setup/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { SetupSection } from '../../../setup/webview/contract'
import { getFirstWorkspaceFolder } from '../../../vscode/workspaceFolders'
import { Response } from '../../../vscode/response'
import { DvcConfig } from '../../../cli/dvc/config'
import * as QuickPickUtil from '../../../setup/quickPick'

suite('Setup Test Suite', () => {
const disposable = Disposable.fn()
Expand Down Expand Up @@ -156,21 +157,75 @@ suite('Setup Test Suite', () => {
expect(mockAutoUpgradeDvc).to.be.calledOnce
}).timeout(WEBVIEW_TEST_TIMEOUT)

it('should handle a select Python interpreter message from the webview', async () => {
const { messageSpy, mockExecuteCommand, setup } = buildSetup(disposable)
const setInterpreterCommand = 'python.setInterpreter'
it('should handle an update Python environment message from the webview', async () => {
const { messageSpy, setup, mockExecuteCommand } = buildSetup(disposable)

const webview = await setup.showWebview()
await webview.isReady()

const mockPickExtensionAction = stub(
QuickPickUtil,
'pickPythonExtensionAction'
)

const firstQuickPickEvent = new Promise(resolve => {
mockPickExtensionAction.onFirstCall().callsFake(() => {
resolve(undefined)
return Promise.resolve(undefined)
})
})

const mockMessageReceived = getMessageReceivedEmitter(webview)

messageSpy.resetHistory()
mockMessageReceived.fire({
type: MessageFromWebviewType.SELECT_PYTHON_INTERPRETER
type: MessageFromWebviewType.UPDATE_PYTHON_ENVIRONMENT
})

await firstQuickPickEvent

expect(mockExecuteCommand).to.not.be.calledWithExactly(
'python.setInterpreter'
)
expect(mockExecuteCommand).to.not.be.calledWithExactly(
'python.createEnvironment'
)

const secondQuickPickEvent = new Promise(resolve => {
mockPickExtensionAction.onSecondCall().callsFake(() => {
resolve(undefined)
return Promise.resolve(1)
})
})

messageSpy.resetHistory()
mockMessageReceived.fire({
type: MessageFromWebviewType.UPDATE_PYTHON_ENVIRONMENT
})

await secondQuickPickEvent

expect(mockExecuteCommand).to.be.calledWithExactly(
'python.createEnvironment'
)

const thirdQuickPickEvent = new Promise(resolve => {
mockPickExtensionAction.onThirdCall().callsFake(() => {
resolve(undefined)
return Promise.resolve(2)
})
})

messageSpy.resetHistory()
mockMessageReceived.fire({
type: MessageFromWebviewType.UPDATE_PYTHON_ENVIRONMENT
})

expect(mockExecuteCommand).to.be.calledWithExactly(setInterpreterCommand)
await thirdQuickPickEvent

expect(mockExecuteCommand).to.be.calledWithExactly(
'python.setInterpreter'
)
}).timeout(WEBVIEW_TEST_TIMEOUT)

it('should handle a show source control panel message from the webview', async () => {
Expand Down
3 changes: 2 additions & 1 deletion extension/src/vscode/title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export enum Title {
SELECT_TRAINING_SCRIPT = 'Select your Training Script',
SETUP_WORKSPACE = 'Setup the Workspace',
SET_EXPERIMENTS_HEADER_HEIGHT = 'Set the Maximum Experiment Table Header Height',
SET_REMOTE_AS_DEFAULT = 'Set Default Remote'
SET_REMOTE_AS_DEFAULT = 'Set Default Remote',
UPDATE_PYTHON_ENVIRONMENT = 'Update Python Environment (selected with the Python Extension)'
}

export const getEnterValueTitle = (path: string): Title =>
Expand Down
4 changes: 2 additions & 2 deletions extension/src/webview/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export enum MessageFromWebviewType {
SELECT_EXPERIMENTS = 'select-experiments',
SELECT_COLUMNS = 'select-columns',
SELECT_PLOTS = 'select-plots',
SELECT_PYTHON_INTERPRETER = 'select-python-interpreter',
SET_EXPERIMENTS_FOR_PLOTS = 'set-experiments-for-plots',
SET_EXPERIMENTS_AND_OPEN_PLOTS = 'set-experiments-and-open-plots',
SET_STUDIO_SHARE_EXPERIMENTS_LIVE = 'set-studio-share-experiments-live',
Expand All @@ -66,6 +65,7 @@ export enum MessageFromWebviewType {
INITIALIZE_GIT = 'initialize-git',
SHOW_SCM_PANEL = 'show-scm-panel',
INSTALL_DVC = 'install-dvc',
UPDATE_PYTHON_ENVIRONMENT = 'update-python-environment',
UPGRADE_DVC = 'upgrade-dvc',
SETUP_WORKSPACE = 'setup-workspace',
ZOOM_PLOT = 'zoom-plot',
Expand Down Expand Up @@ -197,7 +197,7 @@ export type MessageFromWebview =
}
| { type: MessageFromWebviewType.INITIALIZED }
| { type: MessageFromWebviewType.SELECT_EXPERIMENTS }
| { type: MessageFromWebviewType.SELECT_PYTHON_INTERPRETER }
| { type: MessageFromWebviewType.UPDATE_PYTHON_ENVIRONMENT }
| { type: MessageFromWebviewType.SELECT_PLOTS }
| { type: MessageFromWebviewType.REFRESH_REVISIONS }
| { type: MessageFromWebviewType.SELECT_COLUMNS }
Expand Down
14 changes: 6 additions & 8 deletions webview/src/setup/components/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('App', () => {
})
})

it('should let the user find another Python interpreter to install DVC when the Python extension is installed', () => {
it('should let the user find or create another Python interpreter to install DVC when the Python extension is installed', () => {
renderApp({
cliCompatible: undefined,
dvcCliDetails: {
Expand All @@ -197,11 +197,11 @@ describe('App', () => {
pythonBinPath: 'python'
})

const button = screen.getByText('Configure')
const button = screen.getByText('Update Env')
fireEvent.click(button)

expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.SETUP_WORKSPACE
type: MessageFromWebviewType.UPDATE_PYTHON_ENVIRONMENT
})
})

Expand Down Expand Up @@ -376,7 +376,7 @@ describe('App', () => {
})
})

it('should show the user the command used to run DVC with "Configure" and "Select Python Interpreter" buttons if dvc is installed with the python extension', () => {
it('should show the user the command used to run DVC with "Configure" and "Update Env" buttons if dvc is installed with the python extension', () => {
renderApp({
isPythonExtensionUsed: true
})
Expand All @@ -386,9 +386,7 @@ describe('App', () => {
expect(within(envDetails).getByText('Command:')).toBeInTheDocument()

const configureButton = within(envDetails).getByText('Configure')
const selectButton = within(envDetails).getByText(
'Select Python Interpreter'
)
const selectButton = within(envDetails).getByText('Update Env')

expect(configureButton).toBeInTheDocument()
expect(selectButton).toBeInTheDocument()
Expand All @@ -398,7 +396,7 @@ describe('App', () => {
fireEvent.click(selectButton)

expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.SELECT_PYTHON_INTERPRETER
type: MessageFromWebviewType.UPDATE_PYTHON_ENVIRONMENT
})
})

Expand Down
Loading