Skip to content

Commit 58c57e9

Browse files
authored
Follow directions in the VS Code API docs for relative paths (#2590)
1 parent 1987d9d commit 58c57e9

File tree

5 files changed

+72
-11
lines changed

5 files changed

+72
-11
lines changed

extension/src/fileSystem/watcher.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1+
import { join, relative } from 'path'
12
import { utimes } from 'fs-extra'
23
import { GlobPattern, RelativePattern, Uri, workspace } from 'vscode'
34
import { Disposable } from '@hediet/std/disposable'
4-
import { isDirectory } from '.'
5+
import { isDirectory, isSameOrChild } from '.'
6+
7+
const getRelativePatternForOutsideWorkspace = (
8+
uri: Uri,
9+
pattern: string
10+
): RelativePattern => new RelativePattern(uri, pattern)
511

612
export const getRelativePattern = (
713
path: string,
814
pattern: string
9-
): RelativePattern => new RelativePattern(Uri.file(path), pattern)
15+
): RelativePattern => {
16+
for (const workspaceFolder of workspace.workspaceFolders || []) {
17+
const workspaceFolderPath = workspaceFolder.uri.fsPath
18+
if (isSameOrChild(workspaceFolderPath, path)) {
19+
return new RelativePattern(
20+
workspaceFolder,
21+
join(relative(workspaceFolderPath, path), pattern)
22+
)
23+
}
24+
}
25+
26+
return getRelativePatternForOutsideWorkspace(Uri.file(path), pattern)
27+
}
1028

1129
export const fireWatcher = (path: string): Promise<void> => {
1230
const now = Date.now() / 1000

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join, resolve, sep } from 'path'
22
import { afterEach, beforeEach, describe, it, suite } from 'mocha'
3-
import { EventEmitter, FileSystemWatcher, RelativePattern, Uri } from 'vscode'
3+
import { EventEmitter, FileSystemWatcher, RelativePattern } from 'vscode'
44
import { expect } from 'chai'
55
import { stub, restore, spy } from 'sinon'
66
import { Disposable } from '../../../../extension'
@@ -11,7 +11,7 @@ import {
1111
getMockNow,
1212
stubPrivateMemberMethod
1313
} from '../../util'
14-
import { dvcDemoPath } from '../../../util'
14+
import { dvcDemoPath, getTestWorkspaceFolder } from '../../../util'
1515
import {
1616
ExperimentsData,
1717
QUEUED_EXPERIMENT_PATH
@@ -65,7 +65,7 @@ suite('Experiments Data Test Suite', () => {
6565

6666
expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 0)).to.deep.equal(
6767
new RelativePattern(
68-
Uri.file(dvcDemoPath),
68+
getTestWorkspaceFolder(),
6969
join(
7070
'**',
7171
`{dvc.lock,dvc.yaml,params.yaml,*.dvc,nested${sep}params.yaml,summary.json}`
@@ -139,7 +139,7 @@ suite('Experiments Data Test Suite', () => {
139139
expect(mockDispose).to.be.calledOnce
140140
expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 0)).to.deep.equal(
141141
new RelativePattern(
142-
Uri.file(dvcDemoPath),
142+
getTestWorkspaceFolder(),
143143
join(
144144
'**',
145145
`{dvc.lock,dvc.yaml,params.yaml,*.dvc,nested${sep}params.yaml,summary.json}`
@@ -148,7 +148,7 @@ suite('Experiments Data Test Suite', () => {
148148
)
149149
expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 1)).to.deep.equal(
150150
new RelativePattern(
151-
Uri.file(dvcDemoPath),
151+
getTestWorkspaceFolder(),
152152
join(
153153
'**',
154154
`{dvc.lock,dvc.yaml,params.yaml,*.dvc,nested${sep}params.yaml,new_params.yml,new_summary.json,summary.json}`

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { afterEach, beforeEach, describe, it, suite } from 'mocha'
22
import { stub, restore } from 'sinon'
33
import { expect } from 'chai'
4-
import { RelativePattern, Uri } from 'vscode'
4+
import { RelativePattern } from 'vscode'
55
import { Disposable } from '../../../../extension'
66
import { FileSystemData } from '../../../../fileSystem/data'
7-
import { dvcDemoPath } from '../../../util'
7+
import { dvcDemoPath, getTestWorkspaceFolder } from '../../../util'
88
import * as FileSystem from '../../../../fileSystem'
99
import * as Watcher from '../../../../fileSystem/watcher'
1010
import { getFirstArgOfCall, mockDisposable } from '../../util'
@@ -48,7 +48,7 @@ suite('File System Data Test Suite', () => {
4848

4949
expect(mockCreateFileSystemWatcher).to.be.calledOnce
5050
expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 0)).to.deep.equal(
51-
new RelativePattern(Uri.file(dvcDemoPath), join('**', 'dvc.yaml'))
51+
new RelativePattern(getTestWorkspaceFolder(), join('**', 'dvc.yaml'))
5252
)
5353

5454
await data.isReady()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { join, resolve } from 'path'
2+
import { afterEach, beforeEach, describe, it, suite } from 'mocha'
3+
import { expect } from 'chai'
4+
import { Uri } from 'vscode'
5+
import { Disposable } from '@hediet/std/disposable'
6+
import { restore } from 'sinon'
7+
import { getRelativePattern } from '../../../fileSystem/watcher'
8+
import { dvcDemoPath, getTestWorkspaceFolder } from '../../util'
9+
10+
suite('File System Watcher Test Suite', () => {
11+
const disposable = Disposable.fn()
12+
13+
beforeEach(() => {
14+
restore()
15+
})
16+
17+
afterEach(() => {
18+
disposable.dispose()
19+
})
20+
21+
describe('getRelativePattern', () => {
22+
it('should return the expected relative pattern with a path inside of the workspace', () => {
23+
const relativePattern = getRelativePattern(
24+
join(dvcDemoPath, '.git'),
25+
'**'
26+
)
27+
expect(relativePattern.baseUri).to.deep.equal(
28+
getTestWorkspaceFolder().uri
29+
)
30+
expect(relativePattern.pattern).equal(join('.git', '**'))
31+
})
32+
33+
it('should return the expected relative pattern for a path outside of the workspace', () => {
34+
const path = resolve(dvcDemoPath, '..', '.git')
35+
const relativePattern = getRelativePattern(path, '**')
36+
expect(relativePattern.baseUri.path).to.equal(Uri.file(path).path)
37+
expect(relativePattern.pattern).to.equal('**')
38+
})
39+
})
40+
})

extension/src/test/util/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { resolve } from 'path'
2-
import { Memento, Uri } from 'vscode'
2+
import { Memento, Uri, workspace, WorkspaceFolder } from 'vscode'
33

44
const dvcRoot = resolve(__dirname, '..', '..', '..', '..', 'demo')
55
export const dvcDemoPath = Uri.file(dvcRoot).fsPath
66
export const basePlotsUrl = Uri.file(
77
resolve(__dirname, '..', 'fixtures', 'plotsDiff', 'staticImages')
88
).fsPath
99

10+
export const getTestWorkspaceFolder = (): WorkspaceFolder =>
11+
(workspace.workspaceFolders as WorkspaceFolder[])[0]
12+
1013
export const buildMockMemento = (
1114
values: Record<string, unknown> = {}
1215
): Memento =>

0 commit comments

Comments
 (0)