Skip to content

Commit

Permalink
Move init command into extension (#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon authored May 12, 2022
1 parent 1f933ed commit 8c98d02
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 65 deletions.
20 changes: 14 additions & 6 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ExperimentsFilterByTree } from './experiments/model/filterBy/tree'
import { setContextValue } from './vscode/context'
import { OutputChannel } from './vscode/outputChannel'
import {
getFirstWorkspaceFolder,
getWorkspaceFolderCount,
getWorkspaceFolders
} from './vscode/workspaceFolders'
Expand All @@ -34,7 +35,7 @@ import {
sendTelemetryEventAndThrow
} from './telemetry'
import { EventName } from './telemetry/constants'
import { RegisteredCommands } from './commands/external'
import { RegisteredCliCommands, RegisteredCommands } from './commands/external'
import { StopWatch } from './util/time'
import {
registerWalkthroughCommands,
Expand Down Expand Up @@ -171,11 +172,7 @@ export class Extension extends Disposable implements IExtension {
)

this.trackedExplorerTree = this.dispose.track(
new TrackedExplorerTree(
this.internalCommands,
this.workspaceChanged,
this.repositories
)
new TrackedExplorerTree(this.internalCommands, this.repositories)
)

setup(this)
Expand Down Expand Up @@ -260,6 +257,17 @@ export class Extension extends Disposable implements IExtension {
() => outputChannel.show()
)

this.internalCommands.registerExternalCliCommand(
RegisteredCliCommands.INIT,
async () => {
const root = getFirstWorkspaceFolder()
if (root) {
await this.cliExecutor.init(root)
this.workspaceChanged.fire()
}
}
)

registerRepositoryCommands(this.repositories, this.internalCommands)

reRegisterVsCodeCommands(this.internalCommands)
Expand Down
12 changes: 0 additions & 12 deletions extension/src/fileSystem/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import { WorkspaceRepositories } from '../repository/workspace'
import { Repository } from '../repository'
import { dvcDemoPath } from '../test/util'

const mockedWorkspaceChanged = jest.mocked(new EventEmitter<void>())
const mockedWorkspaceChangedFire = jest.fn()
mockedWorkspaceChanged.fire = mockedWorkspaceChangedFire

const mockedTreeDataChanged = jest.mocked(new EventEmitter<void>())
const mockedTreeDataChangedFire = jest.fn()
mockedTreeDataChanged.fire = mockedTreeDataChangedFire
Expand Down Expand Up @@ -67,7 +63,6 @@ describe('TrackedTreeView', () => {
it('should fire the event emitter to reset the data in the view', () => {
const trackedTreeView = new TrackedExplorerTree(
mockedInternalCommands,
mockedWorkspaceChanged,
mockedRepositories
)
trackedTreeView.initialize([dvcDemoPath])
Expand All @@ -83,7 +78,6 @@ describe('TrackedTreeView', () => {

const trackedTreeView = new TrackedExplorerTree(
mockedInternalCommands,
mockedWorkspaceChanged,
mockedRepositories
)
trackedTreeView.initialize(mockedDvcRoots)
Expand Down Expand Up @@ -112,7 +106,6 @@ describe('TrackedTreeView', () => {

const trackedTreeView = new TrackedExplorerTree(
mockedInternalCommands,
mockedWorkspaceChanged,
mockedRepositories
)
trackedTreeView.initialize(mockedDvcRoots)
Expand Down Expand Up @@ -159,7 +152,6 @@ describe('TrackedTreeView', () => {

const trackedTreeView = new TrackedExplorerTree(
mockedInternalCommands,
mockedWorkspaceChanged,
mockedRepositories
)
trackedTreeView.initialize([dvcDemoPath])
Expand Down Expand Up @@ -230,7 +222,6 @@ describe('TrackedTreeView', () => {

const trackedTreeView = new TrackedExplorerTree(
mockedInternalCommands,
mockedWorkspaceChanged,
mockedRepositories
)
trackedTreeView.initialize([dvcDemoPath])
Expand Down Expand Up @@ -259,7 +250,6 @@ describe('TrackedTreeView', () => {

const trackedTreeView = new TrackedExplorerTree(
mockedInternalCommands,
mockedWorkspaceChanged,
mockedRepositories
)
mockedExists.mockReturnValueOnce(false)
Expand Down Expand Up @@ -287,7 +277,6 @@ describe('TrackedTreeView', () => {

const trackedTreeView = new TrackedExplorerTree(
mockedInternalCommands,
mockedWorkspaceChanged,
mockedRepositories
)
mockedExists.mockReturnValueOnce(true).mockReturnValueOnce(true)
Expand Down Expand Up @@ -320,7 +309,6 @@ describe('TrackedTreeView', () => {

const trackedTreeView = new TrackedExplorerTree(
mockedInternalCommands,
mockedWorkspaceChanged,
mockedRepositories
)
mockedExists.mockReturnValueOnce(true)
Expand Down
21 changes: 2 additions & 19 deletions extension/src/fileSystem/tree.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { relative } from 'path'
import {
Event,
EventEmitter,
TreeDataProvider,
TreeItem,
TreeItemCollapsibleState,
Expand All @@ -19,7 +18,6 @@ import {
} from '../commands/internal'
import { tryThenMaybeForce } from '../cli/actions'
import { Flag } from '../cli/constants'
import { getFirstWorkspaceFolder } from '../vscode/workspaceFolders'
import { RegisteredCliCommands, RegisteredCommands } from '../commands/external'
import { sendViewOpenedTelemetryEvent } from '../telemetry'
import { EventName } from '../telemetry/constants'
Expand Down Expand Up @@ -48,14 +46,13 @@ export class TrackedExplorerTree

constructor(
internalCommands: InternalCommands,
workspaceChanged: EventEmitter<void>,
repositories: WorkspaceRepositories
) {
super()

this.internalCommands = internalCommands

this.registerCommands(workspaceChanged)
this.registerCommands()

this.repositories = repositories

Expand Down Expand Up @@ -174,21 +171,7 @@ export class TrackedExplorerTree
})
}

private registerCommands(workspaceChanged: EventEmitter<void>) {
this.internalCommands.registerExternalCliCommand(
RegisteredCliCommands.INIT,
async () => {
const root = getFirstWorkspaceFolder()
if (root) {
await this.internalCommands.executeCommand(
AvailableCommands.INIT,
root
)
workspaceChanged.fire()
}
}
)

private registerCommands() {
this.internalCommands.registerExternalCommand<Resource>(
RegisteredCommands.DELETE_TARGET,
({ resourceUri }) => deleteTarget(resourceUri)
Expand Down
34 changes: 33 additions & 1 deletion extension/src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ import { CliReader, ListOutput, StatusOutput } from '../../cli/reader'
import expShowFixture from '../fixtures/expShow/output'
import plotsDiffFixture from '../fixtures/plotsDiff/output'
import * as Disposer from '../../util/disposable'
import { RegisteredCommands } from '../../commands/external'
import {
RegisteredCliCommands,
RegisteredCommands
} from '../../commands/external'
import * as Setup from '../../setup'
import * as Telemetry from '../../telemetry'
import { EventName } from '../../telemetry/constants'
import { OutputChannel } from '../../vscode/outputChannel'
import { WorkspaceExperiments } from '../../experiments/workspace'
import { QuickPickItemWithValue } from '../../vscode/quickPick'
import { MIN_CLI_VERSION } from '../../cli/constants'
import * as WorkspaceFolders from '../../vscode/workspaceFolders'
import { CliExecutor } from '../../cli/executor'

suite('Extension Test Suite', () => {
const dvcPathOption = 'dvc.dvcPath'
Expand Down Expand Up @@ -393,6 +398,33 @@ suite('Extension Test Suite', () => {
})
})

describe('dvc.init', () => {
it('should be able to run dvc.init without error', async () => {
const mockInit = stub(CliExecutor.prototype, 'init').resolves('')
const mockSetup = stub(Setup, 'setup')
const mockSetupCalled = new Promise(resolve =>
mockSetup.callsFake(() => {
resolve(undefined)
return Promise.resolve(undefined)
})
)

await commands.executeCommand(RegisteredCliCommands.INIT)
await mockSetupCalled
expect(mockInit).to.be.calledOnce
expect(mockSetup).to.be.calledOnce

mockInit.resetHistory()
mockSetup.resetHistory()
stub(WorkspaceFolders, 'getFirstWorkspaceFolder').returns(undefined)

await commands.executeCommand(RegisteredCliCommands.INIT)

expect(mockInit).not.to.be.called
expect(mockSetup).not.to.be.called
})
})

describe('dvc.showCommands', () => {
it('should show all of the dvc commands without error', async () => {
await expect(
Expand Down
27 changes: 0 additions & 27 deletions extension/src/test/suite/fileSystem/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
import { Disposable } from '../../../extension'
import * as Workspace from '../../../fileSystem/workspace'
import { CliExecutor } from '../../../cli/executor'
import * as WorkspaceFolders from '../../../vscode/workspaceFolders'
import * as Setup from '../../../setup'
import {
activeTextEditorChangedEvent,
closeAllEditors,
Expand Down Expand Up @@ -162,31 +160,6 @@ suite('Tracked Explorer Tree Test Suite', () => {
expect(mockRename).not.to.be.called
})

it('should be able to run dvc.init without error', async () => {
const mockInit = stub(CliExecutor.prototype, 'init').resolves('')
const mockSetup = stub(Setup, 'setup')
const mockSetupCalled = new Promise(resolve =>
mockSetup.callsFake(() => {
resolve(undefined)
return Promise.resolve(undefined)
})
)

await commands.executeCommand(RegisteredCliCommands.INIT)
await mockSetupCalled
expect(mockInit).to.be.calledOnce
expect(mockSetup).to.be.calledOnce

mockInit.resetHistory()
mockSetup.resetHistory()
stub(WorkspaceFolders, 'getFirstWorkspaceFolder').returns(undefined)

await commands.executeCommand(RegisteredCliCommands.INIT)

expect(mockInit).not.to.be.called
expect(mockSetup).not.to.be.called
})

it('should be able to open a file', async () => {
const fileToOpen = join(dvcDemoPath, 'train.py')
expect(getActiveTextEditorFilename()).not.to.equal(fileToOpen)
Expand Down

0 comments on commit 8c98d02

Please sign in to comment.