|
| 1 | +import { Uri, workspace } from "vscode"; |
| 2 | +import * as tmp from "tmp"; |
| 3 | +import { CodeQLCliServer } from "../../../../src/codeql-cli/cli"; |
| 4 | +import { getActivatedExtension } from "../../global.helper"; |
| 5 | +import { mkdirSync, writeFileSync } from "fs"; |
| 6 | +import { listModelFiles } from "../../../../src/data-extensions-editor/modeled-method-fs"; |
| 7 | +import { join } from "path"; |
| 8 | + |
| 9 | +const dummyExtensionPackContents = ` |
| 10 | +name: dummy/pack |
| 11 | +version: 0.0.0 |
| 12 | +library: true |
| 13 | +extensionTargets: |
| 14 | + codeql/java-all: '*' |
| 15 | +dataExtensions: |
| 16 | + - models/**/*.yml |
| 17 | +`; |
| 18 | + |
| 19 | +const dummyModelContents = ` |
| 20 | +extensions: |
| 21 | + - addsTo: |
| 22 | + pack: codeql/java-all |
| 23 | + extensible: sourceModel |
| 24 | + data: [] |
| 25 | +
|
| 26 | + - addsTo: |
| 27 | + pack: codeql/java-all |
| 28 | + extensible: sinkModel |
| 29 | + data: [] |
| 30 | +
|
| 31 | + - addsTo: |
| 32 | + pack: codeql/java-all |
| 33 | + extensible: summaryModel |
| 34 | + data: [] |
| 35 | +
|
| 36 | + - addsTo: |
| 37 | + pack: codeql/java-all |
| 38 | + extensible: neutralModel |
| 39 | + data: [] |
| 40 | +`; |
| 41 | + |
| 42 | +describe("listModelFiles", () => { |
| 43 | + let tmpDir: string; |
| 44 | + let tmpDirRemoveCallback: (() => void) | undefined; |
| 45 | + let workspacePath: string; |
| 46 | + let cli: CodeQLCliServer; |
| 47 | + |
| 48 | + beforeEach(async () => { |
| 49 | + const t = tmp.dirSync(); |
| 50 | + tmpDir = t.name; |
| 51 | + tmpDirRemoveCallback = t.removeCallback; |
| 52 | + |
| 53 | + const workspaceFolder = { |
| 54 | + uri: Uri.file(join(tmpDir, "workspace")), |
| 55 | + name: "workspace", |
| 56 | + index: 0, |
| 57 | + }; |
| 58 | + workspacePath = workspaceFolder.uri.fsPath; |
| 59 | + mkdirSync(workspacePath); |
| 60 | + jest |
| 61 | + .spyOn(workspace, "workspaceFolders", "get") |
| 62 | + .mockReturnValue([workspaceFolder]); |
| 63 | + |
| 64 | + const extension = await getActivatedExtension(); |
| 65 | + cli = extension.cliServer; |
| 66 | + }); |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + tmpDirRemoveCallback?.(); |
| 70 | + }); |
| 71 | + |
| 72 | + function makeExtensionPack( |
| 73 | + extensionPackName: string, |
| 74 | + modelFileNames: string[], |
| 75 | + ): string { |
| 76 | + const extensionPackPath = join(workspacePath, extensionPackName); |
| 77 | + mkdirSync(extensionPackPath); |
| 78 | + |
| 79 | + writeFileSync( |
| 80 | + join(extensionPackPath, "codeql-pack.yml"), |
| 81 | + dummyExtensionPackContents, |
| 82 | + ); |
| 83 | + |
| 84 | + mkdirSync(join(extensionPackPath, "models")); |
| 85 | + for (const filename of modelFileNames) { |
| 86 | + writeFileSync( |
| 87 | + join(extensionPackPath, "models", filename), |
| 88 | + dummyModelContents, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + return extensionPackPath; |
| 93 | + } |
| 94 | + |
| 95 | + it("should return the empty set when the extension pack is empty", async () => { |
| 96 | + const extensionPackPath = makeExtensionPack("extension-pack", []); |
| 97 | + |
| 98 | + const modelFiles = await listModelFiles(extensionPackPath, cli); |
| 99 | + expect(modelFiles).toEqual(new Set()); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should find all model files", async () => { |
| 103 | + const extensionPackPath = makeExtensionPack("extension-pack", [ |
| 104 | + "library1.model.yml", |
| 105 | + "library2.model.yml", |
| 106 | + ]); |
| 107 | + |
| 108 | + const modelFiles = await listModelFiles(extensionPackPath, cli); |
| 109 | + expect(modelFiles).toEqual( |
| 110 | + new Set([ |
| 111 | + join(extensionPackPath, "models", "library1.model.yml"), |
| 112 | + join(extensionPackPath, "models", "library2.model.yml"), |
| 113 | + ]), |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should ignore model files from other extension packs", async () => { |
| 118 | + const extensionPackPath = makeExtensionPack("extension-pack", [ |
| 119 | + "library1.model.yml", |
| 120 | + ]); |
| 121 | + makeExtensionPack("another-extension-pack", ["library2.model.yml"]); |
| 122 | + |
| 123 | + const modelFiles = await listModelFiles(extensionPackPath, cli); |
| 124 | + expect(modelFiles).toEqual( |
| 125 | + new Set([join(extensionPackPath, "models", "library1.model.yml")]), |
| 126 | + ); |
| 127 | + }); |
| 128 | +}); |
0 commit comments