|
| 1 | +import { |
| 2 | + nova, |
| 3 | + Process, |
| 4 | + TreeDataProvider, |
| 5 | + TreeItem, |
| 6 | + TreeItemCollapsibleState, |
| 7 | + TreeView, |
| 8 | +} from "../nova_utils.ts"; |
| 9 | + |
| 10 | +// This is duplicate code. |
| 11 | +interface Element { |
| 12 | + toTreeItem: () => TreeItem; |
| 13 | + children: Element[]; |
| 14 | +} |
| 15 | +// This is also duplicate code. |
| 16 | +class Header implements Element { |
| 17 | + content: string; |
| 18 | + children: []; |
| 19 | + constructor(content: string) { |
| 20 | + this.content = content; |
| 21 | + this.children = []; |
| 22 | + } |
| 23 | + |
| 24 | + toTreeItem() { |
| 25 | + const item = new TreeItem(this.content); |
| 26 | + return item; |
| 27 | + } |
| 28 | +} |
| 29 | +export class PathFile implements Element { |
| 30 | + path: string; |
| 31 | + children: []; |
| 32 | + shouldDisambiguate: boolean; |
| 33 | + |
| 34 | + constructor(path: string, children: [], shouldDisambiguate: boolean) { |
| 35 | + this.path = path; |
| 36 | + this.children = children; |
| 37 | + this.shouldDisambiguate = shouldDisambiguate; |
| 38 | + } |
| 39 | + |
| 40 | + get filename(): string { |
| 41 | + return nova.path.basename(this.path); |
| 42 | + } |
| 43 | + |
| 44 | + get extension(): string { |
| 45 | + return nova.path.extname(this.path); |
| 46 | + } |
| 47 | + |
| 48 | + toTreeItem() { |
| 49 | + const relativePath: string = nova.workspace.relativizePath(this.path); |
| 50 | + const item = new TreeItem( |
| 51 | + this.shouldDisambiguate ? relativePath : this.filename, |
| 52 | + ); |
| 53 | + |
| 54 | + item.image = "__filetype" + this.extension; |
| 55 | + item.collapsibleState = TreeItemCollapsibleState.Expanded; |
| 56 | + item.contextValue = "file"; |
| 57 | + return item; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export default class TestsDataProvider implements TreeDataProvider<Element> { |
| 62 | + // See https://deno.land/manual/testing#running-tests. |
| 63 | + static FILE_REGEXP = /^.*[_.]?test\.(ts|tsx|mts|js|mjs|jsx|cjs|cts)$/; |
| 64 | + treeView: TreeView<Element>; |
| 65 | + files: Element[]; |
| 66 | + |
| 67 | + constructor() { |
| 68 | + this.treeView = new TreeView("co.gwil.deno.sidebars.tests.sections.1", { |
| 69 | + dataProvider: this, |
| 70 | + }); |
| 71 | + this.files = []; |
| 72 | + } |
| 73 | + |
| 74 | + static findTests(directoryPath: string): string[] { |
| 75 | + function isAccessibleDirectory(path: string) { |
| 76 | + try { |
| 77 | + nova.fs.listdir(path); |
| 78 | + } catch { |
| 79 | + return false; |
| 80 | + } |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + const tests: string[] = []; |
| 85 | + for (const name of nova.fs.listdir(directoryPath)) { |
| 86 | + const path = nova.path.join(directoryPath, name); |
| 87 | + if (isAccessibleDirectory(path)) { |
| 88 | + let internalTests = null; |
| 89 | + try { |
| 90 | + internalTests = TestsDataProvider.findTests(path); |
| 91 | + } catch { |
| 92 | + continue; |
| 93 | + } |
| 94 | + tests.push(...internalTests); |
| 95 | + } else if (name.match(TestsDataProvider.FILE_REGEXP)) { |
| 96 | + tests.push(path); |
| 97 | + } |
| 98 | + } |
| 99 | + return tests; |
| 100 | + } |
| 101 | + |
| 102 | + runTests(files?: PathFile[]) { |
| 103 | + files ??= []; |
| 104 | + const paths = files.map((file) => file.path); |
| 105 | + |
| 106 | + const options = { |
| 107 | + args: ["deno", ...paths], |
| 108 | + }; |
| 109 | + const denoProcess = new Process("/usr/bin/env", options); |
| 110 | + denoProcess.onStdout(console.log); |
| 111 | + denoProcess.onStderr(console.log); |
| 112 | + |
| 113 | + denoProcess.start(); |
| 114 | + } |
| 115 | + |
| 116 | + getChildren(element: Element | null): Element[] { |
| 117 | + if (element == null) { |
| 118 | + if (nova.workspace.path) { |
| 119 | + return TestsDataProvider.findTests(nova.workspace.path).map((path) => |
| 120 | + new PathFile(path, [], false) |
| 121 | + ); |
| 122 | + } else { |
| 123 | + return [new Header("Open a Deno project to see test files.")]; |
| 124 | + } |
| 125 | + } |
| 126 | + return element.children; |
| 127 | + } |
| 128 | + |
| 129 | + getTreeItem(element: Element): TreeItem { |
| 130 | + return element.toTreeItem(); |
| 131 | + } |
| 132 | + |
| 133 | + refresh() { |
| 134 | + return this.treeView.reload(); |
| 135 | + } |
| 136 | +} |
0 commit comments