Skip to content

Commit f3ec528

Browse files
authored
Update data when an event is fired for a parent directory (events grouped in Codespaces) (#2892)
1 parent af8ee49 commit f3ec528

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

extension/src/data/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { relative } from 'path'
12
import { EventEmitter, Event } from 'vscode'
23
import {
34
createFileSystemWatcher,
@@ -8,6 +9,7 @@ import { InternalCommands } from '../commands/internal'
89
import { ExperimentsOutput, PlotsOutputOrError } from '../cli/dvc/contract'
910
import { uniqueValues } from '../util/array'
1011
import { DeferredDisposable } from '../class/deferred'
12+
import { isSameOrChild } from '../fileSystem'
1113

1214
export abstract class BaseData<
1315
T extends { data: PlotsOutputOrError; revs: string[] } | ExperimentsOutput
@@ -68,9 +70,12 @@ export abstract class BaseData<
6870
private watchFiles() {
6971
return this.dispose.track(
7072
createFileSystemWatcher(getRelativePattern(this.dvcRoot, '**'), path => {
73+
const relPath = relative(this.dvcRoot, path)
7174
if (
72-
this.getWatchedFiles().some(watchedRelPath =>
73-
path.endsWith(watchedRelPath)
75+
this.getWatchedFiles().some(
76+
watchedRelPath =>
77+
path.endsWith(watchedRelPath) ||
78+
isSameOrChild(relPath, watchedRelPath)
7479
)
7580
) {
7681
this.managedUpdate(path)

extension/src/fileSystem/index.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join, resolve } from 'path'
1+
import { join, relative, resolve } from 'path'
22
import { ensureDirSync, remove } from 'fs-extra'
33
import * as FileSystem from '.'
44
import { dvcDemoPath } from '../test/util'
@@ -108,6 +108,21 @@ describe('isSameOrChild', () => {
108108
false
109109
)
110110
})
111+
112+
it('should work for relative paths', () => {
113+
const relPath = relative(
114+
'/workspaces/magnetic-tiles-defect',
115+
'/workspaces/magnetic-tiles-defect/training/plots'
116+
)
117+
118+
expect(
119+
isSameOrChild(relPath, 'training/plots/metrics/train/loss.tsv')
120+
).toBe(true)
121+
122+
expect(
123+
isSameOrChild(relPath, './training/plots/metrics/train/loss.tsv')
124+
).toBe(true)
125+
})
111126
})
112127

113128
describe('getModifiedTime', () => {

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
InternalCommands
1919
} from '../../../../commands/internal'
2020
import { fireWatcher } from '../../../../fileSystem/watcher'
21+
import { getProcessPlatform } from '../../../../env'
22+
import { removeDir } from '../../../../fileSystem'
2123

2224
suite('Plots Data Test Suite', () => {
2325
const disposable = Disposable.fn()
@@ -140,7 +142,8 @@ suite('Plots Data Test Suite', () => {
140142

141143
it('should collect files and watch them for updates', async () => {
142144
const mockNow = getMockNow()
143-
const watchedFile = join('training', 'metrics.json')
145+
const parentDirectory = 'training'
146+
const watchedFile = join(parentDirectory, 'metrics.json')
144147

145148
const mockExecuteCommand = (command: CommandId) => {
146149
if (command === AvailableCommands.IS_EXPERIMENT_RUNNING) {
@@ -211,13 +214,38 @@ suite('Plots Data Test Suite', () => {
211214

212215
const managedUpdateSpy = spy(data, 'managedUpdate')
213216
const dataUpdatedEvent = new Promise(resolve =>
214-
data.onDidUpdate(() => resolve(undefined))
217+
disposable.track(data.onDidUpdate(() => resolve(undefined)))
215218
)
216219

217220
await fireWatcher(join(dvcDemoPath, watchedFile))
218221
await dataUpdatedEvent
219222

220-
expect(managedUpdateSpy).to.be.called
223+
expect(
224+
managedUpdateSpy,
225+
'should update data when an event is fired for a watched file'
226+
).to.be.called
227+
managedUpdateSpy.resetHistory()
228+
229+
bypassProcessManagerDebounce(mockNow, 2)
230+
231+
const secondDataUpdatedEvent = new Promise(resolve =>
232+
disposable.track(data.onDidUpdate(() => resolve(undefined)))
233+
)
234+
235+
const absParentDirectory = join(dvcDemoPath, parentDirectory)
236+
237+
if (getProcessPlatform() === 'win32') {
238+
removeDir(absParentDirectory)
239+
} else {
240+
await fireWatcher(absParentDirectory)
241+
}
242+
243+
await secondDataUpdatedEvent
244+
245+
expect(
246+
managedUpdateSpy,
247+
'should update data when an event is fired for a parent directory'
248+
).to.be.called
221249
})
222250
})
223251
})

0 commit comments

Comments
 (0)