Skip to content

Commit 91ac183

Browse files
authored
Cleanup old branches before showing the experiments table (#3917)
* Cleanup old branches before showing * Fix and add tests
1 parent 9984f04 commit 91ac183

File tree

6 files changed

+92
-19
lines changed

6 files changed

+92
-19
lines changed

extension/src/experiments/data/index.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ export class ExperimentsData extends BaseData<ExpShowOutput> {
4141
}
4242

4343
public async update(): Promise<void> {
44-
void this.updateAvailableBranchesToSelect()
44+
const allBranches = await this.internalCommands.executeCommand<string[]>(
45+
AvailableCommands.GIT_GET_BRANCHES,
46+
this.dvcRoot
47+
)
48+
49+
void this.updateAvailableBranchesToSelect(allBranches)
4550
const data: ExpShowOutput = []
4651

47-
const { branches, currentBranch } =
48-
await this.getBranchesToShowWithCurrent()
52+
const { branches, currentBranch } = await this.getBranchesToShowWithCurrent(
53+
allBranches
54+
)
4955

5056
await Promise.all(
5157
branches.map(async branch => {
@@ -55,6 +61,7 @@ export class ExperimentsData extends BaseData<ExpShowOutput> {
5561
ExperimentFlag.NUM_COMMIT,
5662
this.experiments.getNbOfCommitsToShow(branch).toString()
5763
]
64+
5865
const output = (await this.expShow(
5966
branchFlags,
6067
branch
@@ -79,12 +86,14 @@ export class ExperimentsData extends BaseData<ExpShowOutput> {
7986
this.collectedFiles = collectFiles(data, this.collectedFiles)
8087
}
8188

82-
private async getBranchesToShowWithCurrent() {
89+
private async getBranchesToShowWithCurrent(allBranches: string[]) {
8390
const currentBranch = await this.internalCommands.executeCommand<string>(
8491
AvailableCommands.GIT_GET_CURRENT_BRANCH,
8592
this.dvcRoot
8693
)
8794

95+
this.experiments.pruneBranchesToShow(allBranches)
96+
8897
const branches = this.experiments.getBranchesToShow()
8998

9099
if (!branches.includes(currentBranch)) {
@@ -103,11 +112,13 @@ export class ExperimentsData extends BaseData<ExpShowOutput> {
103112
return data.map(exp => ({ ...exp, branch }))
104113
}
105114

106-
private async updateAvailableBranchesToSelect() {
107-
const allBranches = await this.internalCommands.executeCommand<string[]>(
108-
AvailableCommands.GIT_GET_BRANCHES,
109-
this.dvcRoot
110-
)
115+
private async updateAvailableBranchesToSelect(branches?: string[]) {
116+
const allBranches =
117+
branches ||
118+
(await this.internalCommands.executeCommand<string[]>(
119+
AvailableCommands.GIT_GET_BRANCHES,
120+
this.dvcRoot
121+
))
111122
this.experiments.setAvailableBranchesToShow(allBranches)
112123
}
113124

extension/src/experiments/model/index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,27 @@ describe('ExperimentsModel', () => {
335335
}
336336
)
337337
})
338+
339+
it('should remove outdated branches to show when calling pruneBranchesToShow', () => {
340+
const model = new ExperimentsModel('', buildMockMemento())
341+
342+
model.setBranchesToShow(['one', 'old', 'two', 'three', 'older'])
343+
model.pruneBranchesToShow(['one', 'two', 'three', 'four', 'five', 'six'])
344+
345+
expect(model.getBranchesToShow()).toStrictEqual(['one', 'two', 'three'])
346+
})
347+
348+
it('should persist the branches to show when calling pruneBranchesToShow', () => {
349+
const memento = buildMockMemento()
350+
const model = new ExperimentsModel('', memento)
351+
352+
model.setBranchesToShow(['one', 'old', 'two', 'three', 'older'])
353+
model.pruneBranchesToShow(['one', 'two', 'three', 'four', 'five', 'six'])
354+
355+
expect(memento.get(PersistenceKey.EXPERIMENTS_BRANCHES)).toStrictEqual([
356+
'one',
357+
'two',
358+
'three'
359+
])
360+
})
338361
})

extension/src/experiments/model/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ export class ExperimentsModel extends ModelWithPersistence {
429429
this.persistBranchesToShow()
430430
}
431431

432+
public pruneBranchesToShow(branches: string[]) {
433+
this.branchesToShow = this.branchesToShow.filter(branch =>
434+
branches.includes(branch)
435+
)
436+
this.persistBranchesToShow()
437+
}
438+
432439
public getBranchesToShow() {
433440
return this.branchesToShow
434441
}

extension/src/test/suite/experiments/data/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ suite('Experiments Data Test Suite', () => {
4747

4848
it('should debounce all calls to update that are made within 200ms', async () => {
4949
const { data, mockExpShow } = buildExperimentsData(disposable)
50+
await data.isReady()
5051

5152
await Promise.all([
5253
data.managedUpdate(),
@@ -97,6 +98,7 @@ suite('Experiments Data Test Suite', () => {
9798
getNbOfCommitsToShow: () => ({
9899
main: DEFAULT_NUM_OF_COMMITS_TO_SHOW
99100
}),
101+
pruneBranchesToShow: stub(),
100102
setAvailableBranchesToShow: stub(),
101103
setBranchesToShow: stub()
102104
} as unknown as ExperimentsModel
@@ -156,6 +158,7 @@ suite('Experiments Data Test Suite', () => {
156158
getNbOfCommitsToShow: () => ({
157159
main: DEFAULT_NUM_OF_COMMITS_TO_SHOW
158160
}),
161+
pruneBranchesToShow: stub(),
159162
setAvailableBranchesToShow: stub(),
160163
setBranchesToShow: stub()
161164
} as unknown as ExperimentsModel
@@ -199,6 +202,23 @@ suite('Experiments Data Test Suite', () => {
199202
expect(mockExpShow).to.have.callCount(branchesToShow.length)
200203
})
201204

205+
it('should prune any old branches to show before calling exp show on them', async () => {
206+
stub(ExperimentsData.prototype, 'managedUpdate').resolves()
207+
const branchesToShow = [
208+
'main',
209+
'my-other-branch',
210+
'secret-branch',
211+
'old-branch'
212+
]
213+
const { data, mockPruneBranchesToShow, mockGetBranchesToShow } =
214+
buildExperimentsData(disposable)
215+
mockGetBranchesToShow.returns(branchesToShow)
216+
217+
await data.update()
218+
219+
expect(mockPruneBranchesToShow).to.be.calledOnce
220+
})
221+
202222
it('should add the current branch to the exp show output', async () => {
203223
stub(ExperimentsData.prototype, 'managedUpdate').resolves()
204224
const { data, mockExpShow, mockGetBranchesToShow } = buildExperimentsData(

extension/src/test/suite/experiments/util.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { ExperimentsModel } from '../../../experiments/model'
1616
import { ColumnsModel } from '../../../experiments/columns/model'
1717
import { DEFAULT_NUM_OF_COMMITS_TO_SHOW } from '../../../cli/dvc/constants'
1818
import { PersistenceKey } from '../../../persistence/constants'
19-
import { GitReader } from '../../../cli/git/reader'
2019

2120
export const buildExperiments = (
2221
disposer: Disposer,
@@ -142,27 +141,38 @@ const buildExperimentsDataDependencies = (disposer: Disposer) => {
142141
'createFileSystemWatcher'
143142
).returns(undefined)
144143

145-
const { dvcReader, internalCommands } = buildInternalCommands(disposer)
144+
const { dvcReader, gitReader, internalCommands } =
145+
buildInternalCommands(disposer)
146146
const mockExpShow = stub(dvcReader, 'expShow').resolves(expShowFixture)
147-
return { internalCommands, mockCreateFileSystemWatcher, mockExpShow }
147+
return {
148+
gitReader,
149+
internalCommands,
150+
mockCreateFileSystemWatcher,
151+
mockExpShow
152+
}
148153
}
149154

150155
export const buildExperimentsData = (
151156
disposer: SafeWatcherDisposer,
152157
currentBranch = 'main'
153158
) => {
154-
stub(GitReader.prototype, 'getCurrentBranch').resolves(currentBranch)
159+
const {
160+
internalCommands,
161+
mockExpShow,
162+
mockCreateFileSystemWatcher,
163+
gitReader
164+
} = buildExperimentsDataDependencies(disposer)
155165

156-
const { internalCommands, mockExpShow, mockCreateFileSystemWatcher } =
157-
buildExperimentsDataDependencies(disposer)
166+
stub(gitReader, 'getCurrentBranch').resolves(currentBranch)
167+
stub(gitReader, 'getBranches').resolves(['one'])
158168

159169
const mockGetBranchesToShow = stub().returns(['main'])
170+
const mockPruneBranchesToShow = stub()
160171
const data = disposer.track(
161172
new ExperimentsData(dvcDemoPath, internalCommands, {
162173
getBranchesToShow: mockGetBranchesToShow,
163-
getNbOfCommitsToShow: () => ({
164-
main: DEFAULT_NUM_OF_COMMITS_TO_SHOW
165-
}),
174+
getNbOfCommitsToShow: () => DEFAULT_NUM_OF_COMMITS_TO_SHOW,
175+
pruneBranchesToShow: mockPruneBranchesToShow,
166176
setAvailableBranchesToShow: stub(),
167177
setBranchesToShow: stub()
168178
} as unknown as ExperimentsModel)
@@ -172,7 +182,8 @@ export const buildExperimentsData = (
172182
data,
173183
mockCreateFileSystemWatcher,
174184
mockExpShow,
175-
mockGetBranchesToShow
185+
mockGetBranchesToShow,
186+
mockPruneBranchesToShow
176187
}
177188
}
178189

extension/src/test/suite/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export const buildMockExperimentsData = (update = stub()) =>
188188
getCurrentBranchesToShow: () => ['current'],
189189
onDidChangeDvcYaml: stub(),
190190
onDidUpdate: stub(),
191+
pruneBranchesToShow: stub(),
191192
setCurrentBranchesToShow: stub(),
192193
update
193194
} as unknown as ExperimentsData)

0 commit comments

Comments
 (0)