Skip to content

Commit

Permalink
Follow directions in the VS Code API docs for relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Oct 14, 2022
1 parent 350d9b3 commit 9b458fb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
17 changes: 15 additions & 2 deletions extension/src/fileSystem/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { join, relative } from 'path'
import { utimes } from 'fs-extra'
import { GlobPattern, RelativePattern, Uri, workspace } from 'vscode'
import { Disposable } from '@hediet/std/disposable'
import { isDirectory } from '.'
import { isDirectory, isSameOrChild } from '.'

export const getRelativePattern = (
path: string,
pattern: string
): RelativePattern => new RelativePattern(Uri.file(path), pattern)
): RelativePattern => {
for (const workspaceFolder of workspace.workspaceFolders || []) {
const workspaceFolderPath = workspaceFolder.uri.fsPath
if (isSameOrChild(workspaceFolderPath, path)) {
return new RelativePattern(
workspaceFolder,
join(relative(workspaceFolderPath, path), pattern)
)
}
}

return new RelativePattern(Uri.file(path), pattern)
}

export const fireWatcher = (path: string): Promise<void> => {
const now = Date.now() / 1000
Expand Down
10 changes: 5 additions & 5 deletions extension/src/test/suite/experiments/data/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join, resolve, sep } from 'path'
import { afterEach, beforeEach, describe, it, suite } from 'mocha'
import { EventEmitter, FileSystemWatcher, RelativePattern, Uri } from 'vscode'
import { EventEmitter, FileSystemWatcher, RelativePattern } from 'vscode'
import { expect } from 'chai'
import { stub, restore, spy } from 'sinon'
import { Disposable } from '../../../../extension'
Expand All @@ -11,7 +11,7 @@ import {
getMockNow,
stubPrivateMemberMethod
} from '../../util'
import { dvcDemoPath } from '../../../util'
import { dvcDemoPath, getTestWorkspaceFolder } from '../../../util'
import {
ExperimentsData,
QUEUED_EXPERIMENT_PATH
Expand Down Expand Up @@ -65,7 +65,7 @@ suite('Experiments Data Test Suite', () => {

expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 0)).to.deep.equal(
new RelativePattern(
Uri.file(dvcDemoPath),
getTestWorkspaceFolder(),
join(
'**',
`{dvc.lock,dvc.yaml,params.yaml,*.dvc,nested${sep}params.yaml,summary.json}`
Expand Down Expand Up @@ -139,7 +139,7 @@ suite('Experiments Data Test Suite', () => {
expect(mockDispose).to.be.calledOnce
expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 0)).to.deep.equal(
new RelativePattern(
Uri.file(dvcDemoPath),
getTestWorkspaceFolder(),
join(
'**',
`{dvc.lock,dvc.yaml,params.yaml,*.dvc,nested${sep}params.yaml,summary.json}`
Expand All @@ -148,7 +148,7 @@ suite('Experiments Data Test Suite', () => {
)
expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 1)).to.deep.equal(
new RelativePattern(
Uri.file(dvcDemoPath),
getTestWorkspaceFolder(),
join(
'**',
`{dvc.lock,dvc.yaml,params.yaml,*.dvc,nested${sep}params.yaml,new_params.yml,new_summary.json,summary.json}`
Expand Down
6 changes: 3 additions & 3 deletions extension/src/test/suite/fileSystem/data/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { afterEach, beforeEach, describe, it, suite } from 'mocha'
import { stub, restore } from 'sinon'
import { expect } from 'chai'
import { RelativePattern, Uri } from 'vscode'
import { RelativePattern } from 'vscode'
import { Disposable } from '../../../../extension'
import { FileSystemData } from '../../../../fileSystem/data'
import { dvcDemoPath } from '../../../util'
import { dvcDemoPath, getTestWorkspaceFolder } from '../../../util'
import * as FileSystem from '../../../../fileSystem'
import * as Watcher from '../../../../fileSystem/watcher'
import { getFirstArgOfCall, mockDisposable } from '../../util'
Expand Down Expand Up @@ -48,7 +48,7 @@ suite('File System Data Test Suite', () => {

expect(mockCreateFileSystemWatcher).to.be.calledOnce
expect(getFirstArgOfCall(mockCreateFileSystemWatcher, 0)).to.deep.equal(
new RelativePattern(Uri.file(dvcDemoPath), join('**', 'dvc.yaml'))
new RelativePattern(getTestWorkspaceFolder(), join('**', 'dvc.yaml'))
)

await data.isReady()
Expand Down
40 changes: 40 additions & 0 deletions extension/src/test/suite/fileSystem/watcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { join, resolve } from 'path'
import { afterEach, beforeEach, describe, it, suite } from 'mocha'
import { expect } from 'chai'
import { Uri } from 'vscode'
import { Disposable } from '@hediet/std/disposable'
import { restore } from 'sinon'
import { getRelativePattern } from '../../../fileSystem/watcher'
import { dvcDemoPath } from '../../util'

suite('File System Watcher Test Suite', () => {
const disposable = Disposable.fn()

beforeEach(() => {
restore()
})

afterEach(() => {
disposable.dispose()
})

describe('getRelativePattern', () => {
it('should return the expected relative pattern with a path inside of the workspace', () => {
const relativePattern = getRelativePattern(
join(dvcDemoPath, '.git'),
'**'
)
expect(relativePattern.baseUri.path).to.deep.equal(
Uri.file(dvcDemoPath).path
)
expect(relativePattern.pattern).equal(join('.git', '**'))
})

it('should return the expected relative pattern for a path outside of the workspace', () => {
const path = resolve(dvcDemoPath, '..', '.git')
const relativePattern = getRelativePattern(path, '**')
expect(relativePattern.baseUri.path).to.deep.equal(Uri.file(path).path)
expect(relativePattern.pattern).equal('**')
})
})
})
5 changes: 4 additions & 1 deletion extension/src/test/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { resolve } from 'path'
import { Memento, Uri } from 'vscode'
import { Memento, Uri, workspace, WorkspaceFolder } from 'vscode'

const dvcRoot = resolve(__dirname, '..', '..', '..', '..', 'demo')
export const dvcDemoPath = Uri.file(dvcRoot).fsPath
export const basePlotsUrl = Uri.file(
resolve(__dirname, '..', 'fixtures', 'plotsDiff', 'staticImages')
).fsPath

export const getTestWorkspaceFolder = (): WorkspaceFolder =>
(workspace.workspaceFolders as WorkspaceFolder[])[0]

export const buildMockMemento = (
values: Record<string, unknown> = {}
): Memento =>
Expand Down

0 comments on commit 9b458fb

Please sign in to comment.