Skip to content

Commit

Permalink
Hide remove all buttons from trees when there is nothing to remove
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Aug 2, 2022
1 parent b977501 commit 9857eef
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 15 deletions.
4 changes: 2 additions & 2 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@
},
{
"command": "dvc.views.experimentsSortByTree.removeAllSorts",
"when": "view == dvc.views.experimentsSortByTree",
"when": "view == dvc.views.experimentsSortByTree && dvc.experiments.sorted",
"group": "navigation@2"
},
{
Expand All @@ -1223,7 +1223,7 @@
},
{
"command": "dvc.views.experimentsFilterByTree.removeAllFilters",
"when": "view == dvc.views.experimentsFilterByTree",
"when": "view == dvc.views.experimentsFilterByTree && dvc.experiments.filtered",
"group": "navigation@2"
},
{
Expand Down
52 changes: 44 additions & 8 deletions extension/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Disposable } from './class/dispose'
import { CliRunner } from './cli/runner'
import { Experiments } from './experiments'
import { WorkspaceExperiments } from './experiments/workspace'
import { setContextValue } from './vscode/context'

Expand Down Expand Up @@ -27,23 +28,58 @@ export class Context extends Disposable {

this.dispose.track(
this.experiments.onDidChangeExperiments(() => {
this.setIsExperimentRunning()
const repositories: Experiments[] = []
for (const dvcRoot of this.experiments.getDvcRoots()) {
repositories.push(this.experiments.getRepository(dvcRoot))
}
this.setIsExperimentRunning(repositories)
this.setAreExperimentsFiltered(repositories)
this.setAreExperimentsSorted(repositories)
})
)
}

private setIsExperimentRunning() {
private setIsExperimentRunning(repositories: Experiments[] = []) {
const contextKey = 'dvc.experiment.running'
if (this.cliRunner.isExperimentRunning()) {
setContextValue('dvc.experiment.running', true)
setContextValue(contextKey, true)
return
}

let hasRunning = false
for (const dvcRoot of this.experiments.getDvcRoots()) {
if (this.experiments.getRepository(dvcRoot).hasRunningExperiment()) {
hasRunning = true
this.setContextFromRepositories(
contextKey,
repositories,
(experiments: Experiments) => experiments.hasRunningExperiment()
)
}

private setAreExperimentsFiltered(repositories: Experiments[]) {
return this.setContextFromRepositories(
'dvc.experiments.filtered',
repositories,
(experiments: Experiments) => experiments.getFilters().length > 0
)
}

private setAreExperimentsSorted(repositories: Experiments[]) {
return this.setContextFromRepositories(
'dvc.experiments.sorted',
repositories,
(experiments: Experiments) => experiments.getSorts().length > 0
)
}

private setContextFromRepositories(
contextKey: string,
repositories: Experiments[],
hasRequirement: (experiments: Experiments) => boolean
) {
for (const experiments of repositories) {
if (hasRequirement(experiments)) {
setContextValue(contextKey, true)
return
}
}
setContextValue('dvc.experiment.running', hasRunning)
setContextValue(contextKey, false)
}
}
114 changes: 109 additions & 5 deletions extension/src/test/suite/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { commands, EventEmitter } from 'vscode'
import { CliRunner } from '../../cli/runner'
import { WorkspaceExperiments } from '../../experiments/workspace'
import { Context } from '../../context'
import { FilterDefinition } from '../../experiments/model/filterBy'
import { SortDefinition } from '../../experiments/model/sortBy'

suite('Context Test Suite', () => {
const disposable = Disposable.fn()
Expand Down Expand Up @@ -58,6 +60,18 @@ suite('Context Test Suite', () => {
}
}

const buildMockExperiments = (
filters: FilterDefinition[] = [],
sorts: SortDefinition[] = [],
experimentRunning = false
) => {
return {
getFilters: () => filters,
getSorts: () => sorts,
hasRunningExperiment: () => experimentRunning
}
}

beforeEach(() => {
restore()
})
Expand Down Expand Up @@ -105,10 +119,10 @@ suite('Context Test Suite', () => {
mockGetDvcRoots.returns([mockDvcRoot, mockOtherDvcRoot])
mockGetRepository.callsFake(dvcRoot => {
if (dvcRoot === mockDvcRoot) {
return { hasRunningExperiment: () => true }
return buildMockExperiments([], [], true)
}
if (dvcRoot === mockOtherDvcRoot) {
return { hasRunningExperiment: () => false }
return buildMockExperiments()
}
})

Expand Down Expand Up @@ -137,9 +151,7 @@ suite('Context Test Suite', () => {

const mockDvcRoot = resolve('first', 'root')
mockGetDvcRoots.returns([mockDvcRoot])
mockGetRepository.callsFake(() => {
return { hasRunningExperiment: () => false }
})
mockGetRepository.callsFake(() => buildMockExperiments())

experimentsChanged.fire()
await experimentsChangedEvent
Expand All @@ -150,5 +162,97 @@ suite('Context Test Suite', () => {
false
)
})

it('should set the dvc.experiments.filtered context correctly depending on whether there are filters applied', async () => {
const {
executeCommandSpy,
experimentsChanged,
mockGetDvcRoots,
mockGetRepository,
onDidChangeExperiments
} = buildContext(false)

const firstExperimentsChangedEvent = new Promise(resolve =>
disposable.track(onDidChangeExperiments(() => resolve(undefined)))
)

const mockDvcRoot = resolve('mock', 'root')
mockGetDvcRoots.returns([mockDvcRoot])
mockGetRepository.callsFake(() => buildMockExperiments())

experimentsChanged.fire()
await firstExperimentsChangedEvent

expect(executeCommandSpy).to.be.calledWith(
'setContext',
'dvc.experiments.filtered',
false
)

executeCommandSpy.resetHistory()
mockGetRepository.resetBehavior()
mockGetRepository.callsFake(() =>
buildMockExperiments([{} as FilterDefinition])
)

const secondExperimentsChangedEvent = new Promise(resolve =>
disposable.track(onDidChangeExperiments(() => resolve(undefined)))
)

experimentsChanged.fire()
await secondExperimentsChangedEvent

expect(executeCommandSpy).to.be.calledWith(
'setContext',
'dvc.experiments.filtered',
true
)
})

it('should set the dvc.experiments.sorted context correctly depending on whether there are sorts applied', async () => {
const {
executeCommandSpy,
experimentsChanged,
mockGetDvcRoots,
mockGetRepository,
onDidChangeExperiments
} = buildContext(false)

const firstExperimentsChangedEvent = new Promise(resolve =>
disposable.track(onDidChangeExperiments(() => resolve(undefined)))
)

const mockDvcRoot = resolve('mock', 'root')
mockGetDvcRoots.returns([mockDvcRoot])
mockGetRepository.callsFake(() => buildMockExperiments())

experimentsChanged.fire()
await firstExperimentsChangedEvent

expect(executeCommandSpy).to.be.calledWith(
'setContext',
'dvc.experiments.sorted',
false
)

executeCommandSpy.resetHistory()
mockGetRepository.resetBehavior()
mockGetRepository.callsFake(() =>
buildMockExperiments([], [{} as SortDefinition])
)

const secondExperimentsChangedEvent = new Promise(resolve =>
disposable.track(onDidChangeExperiments(() => resolve(undefined)))
)

experimentsChanged.fire()
await secondExperimentsChangedEvent

expect(executeCommandSpy).to.be.calledWith(
'setContext',
'dvc.experiments.sorted',
true
)
})
})
})

0 comments on commit 9857eef

Please sign in to comment.