-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add watch support that tracks tests
- Loading branch information
1 parent
c380e6f
commit cbb50ea
Showing
6 changed files
with
144 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { CancellationToken, TestRunRequest } from 'vscode'; | ||
import type { WorkspaceChange } from './workspaceObserver'; | ||
import type { TestProject } from './testModel'; | ||
import * as vscodeTypes from './vscodeTypes'; | ||
|
||
export type Watch = { | ||
testDirFsPath: string; | ||
project: TestProject; | ||
request: TestRunRequest; | ||
}; | ||
|
||
export class WatchSupport { | ||
private _watches = new Set<Watch>(); | ||
|
||
constructor(private vscode: vscodeTypes.VSCode, public onWatchesTriggered: (watches: Watch[]) => void) {} | ||
|
||
addToWatch(project: TestProject, request: TestRunRequest, cancellationToken: CancellationToken) { | ||
const watch: Watch = { | ||
testDirFsPath: this.vscode.Uri.file(project.testDir).fsPath, | ||
project, | ||
request, | ||
}; | ||
this._watches.add(watch); | ||
cancellationToken.onCancellationRequested(() => { | ||
this._watches.delete(watch); | ||
// Watch contract has a bug in 1.86 - when outer non-global watch is disabled, it assumes that inner watches are | ||
// discarded as well without issuing the token cancelation. | ||
if (!request.include) | ||
return; | ||
for (const include of request.include) { | ||
for (const watch of this._watches) { | ||
for (const i of watch.request.include || []) { | ||
if (isAncestorOf(include, i)) { | ||
this._watches.delete(watch); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
workspaceChanged(change: WorkspaceChange) { | ||
// Collapse watches in the same project to the outermost | ||
if (!this._watches) | ||
return; | ||
const watchesToRun = new Set<Watch>(); | ||
for (const entry of change.changed) { | ||
for (const watch of this._watches) { | ||
if (!watch.request.include) { | ||
// Entire project is watched. | ||
if (entry.startsWith(watch.project.testDir)) | ||
watchesToRun.add(watch); | ||
continue; | ||
} | ||
|
||
for (const include of watch.request.include) { | ||
if (!include.uri) | ||
continue; | ||
if (entry.startsWith(include.uri.fsPath)) | ||
watchesToRun.add(watch); | ||
} | ||
} | ||
} | ||
if (watchesToRun.size) | ||
this.onWatchesTriggered([...watchesToRun]); | ||
} | ||
} | ||
function isAncestorOf(root: vscodeTypes.TestItem, descendent: vscodeTypes.TestItem) { | ||
while (descendent.parent) { | ||
if (descendent.parent === root) | ||
return true; | ||
descendent = descendent.parent; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters