|
| 1 | +import { Uri, workspace } from "vscode"; |
| 2 | +import { QueryPackDiscovery } from "../../../../src/queries-panel/query-pack-discovery"; |
| 3 | +import * as tmp from "tmp"; |
| 4 | +import { dirname, join } from "path"; |
| 5 | +import { CodeQLCliServer, QuerySetup } from "../../../../src/codeql-cli/cli"; |
| 6 | +import { mockedObject } from "../../utils/mocking.helpers"; |
| 7 | +import { mkdirSync, writeFileSync } from "fs"; |
| 8 | + |
| 9 | +describe("Query pack discovery", () => { |
| 10 | + let tmpDir: string; |
| 11 | + let tmpDirRemoveCallback: (() => void) | undefined; |
| 12 | + |
| 13 | + let workspacePath: string; |
| 14 | + |
| 15 | + let resolveLibraryPath: jest.SpiedFunction< |
| 16 | + typeof CodeQLCliServer.prototype.resolveLibraryPath |
| 17 | + >; |
| 18 | + let discovery: QueryPackDiscovery; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + const t = tmp.dirSync(); |
| 22 | + tmpDir = t.name; |
| 23 | + tmpDirRemoveCallback = t.removeCallback; |
| 24 | + |
| 25 | + const workspaceFolder = { |
| 26 | + uri: Uri.file(join(tmpDir, "workspace")), |
| 27 | + name: "workspace", |
| 28 | + index: 0, |
| 29 | + }; |
| 30 | + workspacePath = workspaceFolder.uri.fsPath; |
| 31 | + jest |
| 32 | + .spyOn(workspace, "workspaceFolders", "get") |
| 33 | + .mockReturnValue([workspaceFolder]); |
| 34 | + |
| 35 | + const mockResolveLibraryPathValue: QuerySetup = { |
| 36 | + libraryPath: [], |
| 37 | + dbscheme: "/ql/java/ql/lib/config/semmlecode.dbscheme", |
| 38 | + }; |
| 39 | + resolveLibraryPath = jest |
| 40 | + .fn() |
| 41 | + .mockResolvedValue(mockResolveLibraryPathValue); |
| 42 | + const mockCliServer = mockedObject<CodeQLCliServer>({ resolveLibraryPath }); |
| 43 | + discovery = new QueryPackDiscovery(mockCliServer); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + tmpDirRemoveCallback?.(); |
| 48 | + discovery.dispose(); |
| 49 | + }); |
| 50 | + |
| 51 | + describe("findQueryPack", () => { |
| 52 | + it("returns undefined when there are no query packs", async () => { |
| 53 | + await discovery.initialRefresh(); |
| 54 | + |
| 55 | + expect( |
| 56 | + discovery.getLanguageForQueryFile(join(workspacePath, "query.ql")), |
| 57 | + ).toEqual(undefined); |
| 58 | + }); |
| 59 | + |
| 60 | + it("locates a query pack in the same directory", async () => { |
| 61 | + makeTestFile(join(workspacePath, "qlpack.yml")); |
| 62 | + |
| 63 | + await discovery.initialRefresh(); |
| 64 | + |
| 65 | + expect( |
| 66 | + discovery.getLanguageForQueryFile(join(workspacePath, "query.ql")), |
| 67 | + ).toEqual("java"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("locates a query pack using the old pack name", async () => { |
| 71 | + makeTestFile(join(workspacePath, "codeql-pack.yml")); |
| 72 | + |
| 73 | + await discovery.initialRefresh(); |
| 74 | + |
| 75 | + expect( |
| 76 | + discovery.getLanguageForQueryFile(join(workspacePath, "query.ql")), |
| 77 | + ).toEqual("java"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("locates a query pack in a higher directory", async () => { |
| 81 | + makeTestFile(join(workspacePath, "qlpack.yml")); |
| 82 | + |
| 83 | + await discovery.initialRefresh(); |
| 84 | + |
| 85 | + expect( |
| 86 | + discovery.getLanguageForQueryFile( |
| 87 | + join(workspacePath, "foo", "bar", "query.ql"), |
| 88 | + ), |
| 89 | + ).toEqual("java"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("doesn't recognise a query pack in a sibling directory", async () => { |
| 93 | + makeTestFile(join(workspacePath, "foo", "qlpack.yml")); |
| 94 | + |
| 95 | + await discovery.initialRefresh(); |
| 96 | + |
| 97 | + expect( |
| 98 | + discovery.getLanguageForQueryFile( |
| 99 | + join(workspacePath, "foo", "query.ql"), |
| 100 | + ), |
| 101 | + ).toEqual("java"); |
| 102 | + expect( |
| 103 | + discovery.getLanguageForQueryFile( |
| 104 | + join(workspacePath, "bar", "query.ql"), |
| 105 | + ), |
| 106 | + ).toEqual(undefined); |
| 107 | + }); |
| 108 | + |
| 109 | + it("query packs override those from parent directories", async () => { |
| 110 | + makeTestFile(join(workspacePath, "qlpack.yml")); |
| 111 | + makeTestFile(join(workspacePath, "foo", "qlpack.yml")); |
| 112 | + |
| 113 | + resolveLibraryPath.mockImplementation(async (_workspaces, queryPath) => { |
| 114 | + if (queryPath === join(workspacePath, "qlpack.yml")) { |
| 115 | + return { |
| 116 | + libraryPath: [], |
| 117 | + dbscheme: "/ql/java/ql/lib/config/semmlecode.dbscheme", |
| 118 | + }; |
| 119 | + } |
| 120 | + if (queryPath === join(workspacePath, "foo", "qlpack.yml")) { |
| 121 | + return { |
| 122 | + libraryPath: [], |
| 123 | + dbscheme: "/ql/cpp/ql/lib/semmlecode.cpp.dbscheme", |
| 124 | + }; |
| 125 | + } |
| 126 | + throw new Error(`Unknown query pack: ${queryPath}`); |
| 127 | + }); |
| 128 | + |
| 129 | + await discovery.initialRefresh(); |
| 130 | + |
| 131 | + expect( |
| 132 | + discovery.getLanguageForQueryFile(join(workspacePath, "query.ql")), |
| 133 | + ).toEqual("java"); |
| 134 | + expect( |
| 135 | + discovery.getLanguageForQueryFile( |
| 136 | + join(workspacePath, "foo", "query.ql"), |
| 137 | + ), |
| 138 | + ).toEqual("cpp"); |
| 139 | + }); |
| 140 | + |
| 141 | + it("prefers a query pack called qlpack.yml", async () => { |
| 142 | + makeTestFile(join(workspacePath, "qlpack.yml")); |
| 143 | + makeTestFile(join(workspacePath, "codeql-pack.yml")); |
| 144 | + |
| 145 | + resolveLibraryPath.mockImplementation(async (_workspaces, queryPath) => { |
| 146 | + if (queryPath === join(workspacePath, "qlpack.yml")) { |
| 147 | + return { |
| 148 | + libraryPath: [], |
| 149 | + dbscheme: "/ql/cpp/ql/lib/semmlecode.cpp.dbscheme", |
| 150 | + }; |
| 151 | + } |
| 152 | + if (queryPath === join(workspacePath, "codeql-pack.yml")) { |
| 153 | + return { |
| 154 | + libraryPath: [], |
| 155 | + dbscheme: "/ql/java/ql/lib/config/semmlecode.dbscheme", |
| 156 | + }; |
| 157 | + } |
| 158 | + throw new Error(`Unknown query pack: ${queryPath}`); |
| 159 | + }); |
| 160 | + |
| 161 | + await discovery.initialRefresh(); |
| 162 | + |
| 163 | + expect( |
| 164 | + discovery.getLanguageForQueryFile(join(workspacePath, "query.ql")), |
| 165 | + ).toEqual("cpp"); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
| 169 | + |
| 170 | +function makeTestFile(path: string) { |
| 171 | + mkdirSync(dirname(path), { recursive: true }); |
| 172 | + writeFileSync(path, ""); |
| 173 | +} |
0 commit comments