Skip to content

Commit

Permalink
fix(watcher): ignore directories
Browse files Browse the repository at this point in the history
Fixes #399
  • Loading branch information
sheremet-va committed Sep 8, 2024
1 parent cbbb746 commit 714be75
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 40 deletions.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
],
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
{
"name": "Run Extension Browser Sample",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"${workspaceFolder}/samples/browser"
],
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
{
"name": "Run Extension Continuous Sample",
"type": "extensionHost",
Expand Down
49 changes: 9 additions & 40 deletions src/testTree.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as vscode from 'vscode'
import { basename, dirname, normalize } from 'pathe'
import type { RunnerTask, RunnerTestFile } from 'vitest'
import mm from 'micromatch'
import { TestCase, TestFile, TestFolder, TestSuite, getTestData } from './testTreeData'
import { log } from './log'
import type { VitestFolderAPI } from './api'
import { getConfig } from './config'
import { ExtensionWatcher } from './watcher'

// testItem -> vscode.TestItem
// testData -> our wrapper
Expand All @@ -21,7 +20,7 @@ export class TestTree extends vscode.Disposable {
// to store all of them
private testItemsByFile = new Map<string, vscode.TestItem[]>()

private watcherByFolder = new Map<vscode.WorkspaceFolder, vscode.FileSystemWatcher>()
private watcher: ExtensionWatcher

constructor(
private readonly controller: vscode.TestController,
Expand All @@ -32,9 +31,8 @@ export class TestTree extends vscode.Disposable {
this.fileItems.clear()
this.flatTestItems.clear()
this.testItemsByFile.clear()
this.watcherByFolder.forEach(x => x.dispose())
this.watcherByFolder.clear()
})
this.watcher = new ExtensionWatcher(this)
}

public getFileTestItems(fsPath: string) {
Expand All @@ -50,8 +48,7 @@ export class TestTree extends vscode.Disposable {
this.testItemsByFile.clear()
this.fileItems.clear()
this.flatTestItems.clear()
this.watcherByFolder.forEach(x => x.dispose())
this.watcherByFolder.clear()
this.watcher.dispose()

this.loaderItem.busy = true

Expand Down Expand Up @@ -181,40 +178,12 @@ export class TestTree extends vscode.Disposable {

async watchTestFilesInWorkspace(api: VitestFolderAPI, testFiles: [prject: string, file: string][]) {
await this.discoverAllTestFiles(api, testFiles)
this.watcher.watchTestFilesInWorkspace(api)
}

if (this.watcherByFolder.has(api.workspaceFolder))
return

const watcher = vscode.workspace.createFileSystemWatcher(
new vscode.RelativePattern(api.workspaceFolder, getConfig(api.workspaceFolder).filesWatcherInclude),
)
this.watcherByFolder.set(api.workspaceFolder, watcher)

watcher.onDidDelete((file) => {
const items = this.testItemsByFile.get(normalize(file.fsPath))
items?.forEach(item => this.recursiveDelete(item))
})

const ignorePattern = [
'**/.git/**',
'**/*.git',
]

watcher.onDidChange((file) => {
const filepath = normalize(file.fsPath)
if (mm.isMatch(filepath, ignorePattern)) {
return
}
api.onFileChanged(filepath)
})

watcher.onDidCreate((file) => {
const filepath = normalize(file.fsPath)
if (mm.isMatch(filepath, ignorePattern)) {
return
}
api.onFileCreated(filepath)
})
public removeFile(filepath: string) {
const items = this.testItemsByFile.get(normalize(filepath))
items?.forEach(item => this.recursiveDelete(item))
}

private recursiveDelete(item: vscode.TestItem) {
Expand Down
58 changes: 58 additions & 0 deletions src/watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { stat } from 'node:fs/promises'
import * as vscode from 'vscode'
import { normalize } from 'pathe'
import mm from 'micromatch'
import type { TestTree } from './testTree'
import { getConfig } from './config'
import type { VitestFolderAPI } from './api'

export class ExtensionWatcher extends vscode.Disposable {
private watcherByFolder = new Map<vscode.WorkspaceFolder, vscode.FileSystemWatcher>()

private readonly ignorePattern = [
'**/.git/**',
'**/*.git',
]

constructor(private readonly testTree: TestTree) {
super(() => {
this.watcherByFolder.forEach(x => x.dispose())
this.watcherByFolder.clear()
})
}

async watchTestFilesInWorkspace(api: VitestFolderAPI) {
if (this.watcherByFolder.has(api.workspaceFolder))
return

const watcher = vscode.workspace.createFileSystemWatcher(
new vscode.RelativePattern(api.workspaceFolder, getConfig(api.workspaceFolder).filesWatcherInclude),
)
this.watcherByFolder.set(api.workspaceFolder, watcher)

watcher.onDidDelete((file) => {
this.testTree.removeFile(normalize(file.fsPath))
})

watcher.onDidChange(async (file) => {
const filepath = normalize(file.fsPath)
if (await this.shouldIgnoreFile(filepath)) {
return
}
api.onFileChanged(filepath)
})

watcher.onDidCreate(async (file) => {
const filepath = normalize(file.fsPath)
if (await this.shouldIgnoreFile(filepath)) {
return
}
api.onFileCreated(filepath)
})
}

private async shouldIgnoreFile(filepath: string) {
const stats = await stat(filepath).catch(() => null)
return !stats || stats.isDirectory() || mm.isMatch(filepath, this.ignorePattern)
}
}

0 comments on commit 714be75

Please sign in to comment.