|
1 | | -import { Uri, WorkspaceFolder } from "vscode"; |
| 1 | +import { |
| 2 | + CancellationTokenSource, |
| 3 | + Range, |
| 4 | + TestItem, |
| 5 | + TestItemCollection, |
| 6 | + TestRun, |
| 7 | + TestRunRequest, |
| 8 | + Uri, |
| 9 | + WorkspaceFolder, |
| 10 | + tests, |
| 11 | +} from "vscode"; |
2 | 12 |
|
3 | 13 | import { QLTestAdapter } from "../../../src/test-adapter"; |
4 | 14 | import { CodeQLCliServer } from "../../../src/cli"; |
5 | | -import { |
6 | | - DatabaseItem, |
7 | | - DatabaseItemImpl, |
8 | | - DatabaseManager, |
9 | | - FullDatabaseOptions, |
10 | | -} from "../../../src/local-databases"; |
| 15 | +import { DatabaseManager } from "../../../src/local-databases"; |
11 | 16 | import { mockedObject } from "../utils/mocking.helpers"; |
12 | 17 | import { TestRunner } from "../../../src/test-runner"; |
| 18 | +import { |
| 19 | + createMockCliServerForTestRun, |
| 20 | + mockEmptyDatabaseManager, |
| 21 | + mockTestsInfo, |
| 22 | +} from "./test-runner-helpers"; |
| 23 | +import { TestManager } from "../../../src/test-manager"; |
| 24 | +import { createMockApp } from "../../__mocks__/appMock"; |
13 | 25 |
|
14 | | -jest.mock("fs-extra", () => { |
15 | | - const original = jest.requireActual("fs-extra"); |
16 | | - return { |
17 | | - ...original, |
18 | | - access: jest.fn(), |
19 | | - }; |
20 | | -}); |
| 26 | +type IdTestItemPair = [id: string, testItem: TestItem]; |
21 | 27 |
|
22 | 28 | describe("test-adapter", () => { |
23 | 29 | let testRunner: TestRunner; |
24 | | - let adapter: QLTestAdapter; |
25 | 30 | let fakeDatabaseManager: DatabaseManager; |
26 | 31 | let fakeCliServer: CodeQLCliServer; |
27 | | - let currentDatabaseItem: DatabaseItem | undefined; |
28 | | - let databaseItems: DatabaseItem[] = []; |
29 | | - const openDatabaseSpy = jest.fn(); |
30 | | - const removeDatabaseItemSpy = jest.fn(); |
31 | | - const renameDatabaseItemSpy = jest.fn(); |
32 | | - const setCurrentDatabaseItemSpy = jest.fn(); |
33 | | - const runTestsSpy = jest.fn(); |
34 | | - const resolveTestsSpy = jest.fn(); |
35 | | - const resolveQlpacksSpy = jest.fn(); |
36 | | - |
37 | | - const preTestDatabaseItem = new DatabaseItemImpl( |
38 | | - Uri.file("/path/to/test/dir/dir.testproj"), |
39 | | - undefined, |
40 | | - mockedObject<FullDatabaseOptions>({ displayName: "custom display name" }), |
41 | | - (_) => { |
42 | | - /* no change event listener */ |
43 | | - }, |
44 | | - ); |
45 | | - const postTestDatabaseItem = new DatabaseItemImpl( |
46 | | - Uri.file("/path/to/test/dir/dir.testproj"), |
47 | | - undefined, |
48 | | - mockedObject<FullDatabaseOptions>({ displayName: "default name" }), |
49 | | - (_) => { |
50 | | - /* no change event listener */ |
51 | | - }, |
52 | | - ); |
53 | 32 |
|
54 | 33 | beforeEach(() => { |
55 | | - mockRunTests(); |
56 | | - openDatabaseSpy.mockResolvedValue(postTestDatabaseItem); |
57 | | - removeDatabaseItemSpy.mockResolvedValue(undefined); |
58 | | - renameDatabaseItemSpy.mockResolvedValue(undefined); |
59 | | - setCurrentDatabaseItemSpy.mockResolvedValue(undefined); |
60 | | - resolveQlpacksSpy.mockResolvedValue({}); |
61 | | - resolveTestsSpy.mockResolvedValue([]); |
62 | | - fakeDatabaseManager = mockedObject<DatabaseManager>( |
63 | | - { |
64 | | - openDatabase: openDatabaseSpy, |
65 | | - removeDatabaseItem: removeDatabaseItemSpy, |
66 | | - renameDatabaseItem: renameDatabaseItemSpy, |
67 | | - setCurrentDatabaseItem: setCurrentDatabaseItemSpy, |
68 | | - }, |
69 | | - { |
70 | | - dynamicProperties: { |
71 | | - currentDatabaseItem: () => currentDatabaseItem, |
72 | | - databaseItems: () => databaseItems, |
73 | | - }, |
74 | | - }, |
75 | | - ); |
| 34 | + fakeDatabaseManager = mockEmptyDatabaseManager(); |
76 | 35 |
|
77 | | - jest.spyOn(preTestDatabaseItem, "isAffectedByTest").mockResolvedValue(true); |
78 | | - |
79 | | - fakeCliServer = mockedObject<CodeQLCliServer>({ |
80 | | - runTests: runTestsSpy, |
81 | | - resolveQlpacks: resolveQlpacksSpy, |
82 | | - resolveTests: resolveTestsSpy, |
83 | | - }); |
| 36 | + const mockCli = createMockCliServerForTestRun(); |
| 37 | + fakeCliServer = mockCli.cliServer; |
84 | 38 |
|
85 | 39 | testRunner = new TestRunner(fakeDatabaseManager, fakeCliServer); |
| 40 | + }); |
86 | 41 |
|
87 | | - adapter = new QLTestAdapter( |
| 42 | + it("legacy test adapter should run some tests", async () => { |
| 43 | + const adapter = new QLTestAdapter( |
88 | 44 | mockedObject<WorkspaceFolder>({ |
89 | 45 | name: "ABC", |
90 | 46 | uri: Uri.parse("file:/ab/c"), |
91 | 47 | }), |
92 | 48 | testRunner, |
93 | 49 | fakeCliServer, |
94 | 50 | ); |
95 | | - }); |
96 | 51 |
|
97 | | - it("should run some tests", async () => { |
98 | 52 | const listenerSpy = jest.fn(); |
99 | 53 | adapter.testStates(listenerSpy); |
100 | | - const testsPath = Uri.parse("file:/ab/c").fsPath; |
101 | | - const dPath = Uri.parse("file:/ab/c/d.ql").fsPath; |
102 | | - const gPath = Uri.parse("file:/ab/c/e/f/g.ql").fsPath; |
103 | | - const hPath = Uri.parse("file:/ab/c/e/f/h.ql").fsPath; |
104 | | - |
105 | | - await adapter.run([testsPath]); |
| 54 | + await adapter.run([mockTestsInfo.testsPath]); |
106 | 55 |
|
107 | 56 | expect(listenerSpy).toBeCalledTimes(5); |
108 | 57 |
|
109 | 58 | expect(listenerSpy).toHaveBeenNthCalledWith(1, { |
110 | 59 | type: "started", |
111 | | - tests: [testsPath], |
| 60 | + tests: [mockTestsInfo.testsPath], |
112 | 61 | }); |
113 | 62 | expect(listenerSpy).toHaveBeenNthCalledWith(2, { |
114 | 63 | type: "test", |
115 | 64 | state: "passed", |
116 | | - test: dPath, |
| 65 | + test: mockTestsInfo.dPath, |
117 | 66 | message: undefined, |
118 | 67 | decorations: [], |
119 | 68 | }); |
120 | 69 | expect(listenerSpy).toHaveBeenNthCalledWith(3, { |
121 | 70 | type: "test", |
122 | 71 | state: "errored", |
123 | | - test: gPath, |
124 | | - message: `\ncompilation error: ${gPath}\nERROR: abc\n`, |
| 72 | + test: mockTestsInfo.gPath, |
| 73 | + message: `\ncompilation error: ${mockTestsInfo.gPath}\nERROR: abc\n`, |
125 | 74 | decorations: [{ line: 1, message: "abc" }], |
126 | 75 | }); |
127 | 76 | expect(listenerSpy).toHaveBeenNthCalledWith(4, { |
128 | 77 | type: "test", |
129 | 78 | state: "failed", |
130 | | - test: hPath, |
131 | | - message: `\nfailed: ${hPath}\njkh\ntuv\n`, |
| 79 | + test: mockTestsInfo.hPath, |
| 80 | + message: `\nfailed: ${mockTestsInfo.hPath}\njkh\ntuv\n`, |
132 | 81 | decorations: [], |
133 | 82 | }); |
134 | 83 | expect(listenerSpy).toHaveBeenNthCalledWith(5, { type: "finished" }); |
135 | 84 | }); |
136 | 85 |
|
137 | | - it("should reregister testproj databases around test run", async () => { |
138 | | - currentDatabaseItem = preTestDatabaseItem; |
139 | | - databaseItems = [preTestDatabaseItem]; |
140 | | - await adapter.run(["/path/to/test/dir"]); |
141 | | - |
142 | | - expect(removeDatabaseItemSpy.mock.invocationCallOrder[0]).toBeLessThan( |
143 | | - runTestsSpy.mock.invocationCallOrder[0], |
144 | | - ); |
145 | | - expect(openDatabaseSpy.mock.invocationCallOrder[0]).toBeGreaterThan( |
146 | | - runTestsSpy.mock.invocationCallOrder[0], |
147 | | - ); |
148 | | - expect(renameDatabaseItemSpy.mock.invocationCallOrder[0]).toBeGreaterThan( |
149 | | - openDatabaseSpy.mock.invocationCallOrder[0], |
150 | | - ); |
151 | | - expect( |
152 | | - setCurrentDatabaseItemSpy.mock.invocationCallOrder[0], |
153 | | - ).toBeGreaterThan(openDatabaseSpy.mock.invocationCallOrder[0]); |
154 | | - |
155 | | - expect(removeDatabaseItemSpy).toBeCalledTimes(1); |
156 | | - expect(removeDatabaseItemSpy).toBeCalledWith( |
157 | | - expect.anything(), |
158 | | - expect.anything(), |
159 | | - preTestDatabaseItem, |
| 86 | + it("native test manager should run some tests", async () => { |
| 87 | + const enqueuedSpy = jest.fn(); |
| 88 | + const passedSpy = jest.fn(); |
| 89 | + const erroredSpy = jest.fn(); |
| 90 | + const failedSpy = jest.fn(); |
| 91 | + const endSpy = jest.fn(); |
| 92 | + |
| 93 | + const testController = tests.createTestController("codeql", "CodeQL Tests"); |
| 94 | + testController.createTestRun = jest.fn().mockImplementation(() => |
| 95 | + mockedObject<TestRun>({ |
| 96 | + enqueued: enqueuedSpy, |
| 97 | + passed: passedSpy, |
| 98 | + errored: erroredSpy, |
| 99 | + failed: failedSpy, |
| 100 | + end: endSpy, |
| 101 | + }), |
160 | 102 | ); |
161 | | - |
162 | | - expect(openDatabaseSpy).toBeCalledTimes(1); |
163 | | - expect(openDatabaseSpy).toBeCalledWith( |
164 | | - expect.anything(), |
165 | | - expect.anything(), |
166 | | - preTestDatabaseItem.databaseUri, |
| 103 | + const testManager = new TestManager( |
| 104 | + createMockApp({}), |
| 105 | + testRunner, |
| 106 | + fakeCliServer, |
| 107 | + testController, |
167 | 108 | ); |
168 | 109 |
|
169 | | - expect(renameDatabaseItemSpy).toBeCalledTimes(1); |
170 | | - expect(renameDatabaseItemSpy).toBeCalledWith( |
171 | | - postTestDatabaseItem, |
172 | | - preTestDatabaseItem.name, |
| 110 | + const childItems: TestItem[] = [ |
| 111 | + { |
| 112 | + children: { size: 0 } as TestItemCollection, |
| 113 | + id: `test ${mockTestsInfo.dPath}`, |
| 114 | + uri: Uri.file(mockTestsInfo.dPath), |
| 115 | + } as TestItem, |
| 116 | + { |
| 117 | + children: { size: 0 } as TestItemCollection, |
| 118 | + id: `test ${mockTestsInfo.gPath}`, |
| 119 | + uri: Uri.file(mockTestsInfo.gPath), |
| 120 | + } as TestItem, |
| 121 | + { |
| 122 | + children: { size: 0 } as TestItemCollection, |
| 123 | + id: `test ${mockTestsInfo.hPath}`, |
| 124 | + uri: Uri.file(mockTestsInfo.hPath), |
| 125 | + } as TestItem, |
| 126 | + ]; |
| 127 | + const childElements: IdTestItemPair[] = childItems.map((childItem) => [ |
| 128 | + childItem.id, |
| 129 | + childItem, |
| 130 | + ]); |
| 131 | + const childIteratorFunc: () => Iterator<IdTestItemPair> = () => |
| 132 | + childElements[Symbol.iterator](); |
| 133 | + |
| 134 | + const rootItem = { |
| 135 | + id: `dir ${mockTestsInfo.testsPath}`, |
| 136 | + uri: Uri.file(mockTestsInfo.testsPath), |
| 137 | + children: { |
| 138 | + size: 3, |
| 139 | + [Symbol.iterator]: childIteratorFunc, |
| 140 | + } as TestItemCollection, |
| 141 | + } as TestItem; |
| 142 | + |
| 143 | + const request = new TestRunRequest([rootItem]); |
| 144 | + await testManager.run(request, new CancellationTokenSource().token); |
| 145 | + |
| 146 | + expect(enqueuedSpy).toBeCalledTimes(3); |
| 147 | + expect(passedSpy).toBeCalledTimes(1); |
| 148 | + expect(passedSpy).toHaveBeenCalledWith(childItems[0], 3000); |
| 149 | + expect(erroredSpy).toHaveBeenCalledTimes(1); |
| 150 | + expect(erroredSpy).toHaveBeenCalledWith( |
| 151 | + childItems[1], |
| 152 | + [ |
| 153 | + { |
| 154 | + location: { |
| 155 | + range: new Range(0, 0, 1, 1), |
| 156 | + uri: Uri.file(mockTestsInfo.gPath), |
| 157 | + }, |
| 158 | + message: "abc", |
| 159 | + }, |
| 160 | + ], |
| 161 | + 4000, |
173 | 162 | ); |
174 | | - |
175 | | - expect(setCurrentDatabaseItemSpy).toBeCalledTimes(1); |
176 | | - expect(setCurrentDatabaseItemSpy).toBeCalledWith( |
177 | | - postTestDatabaseItem, |
178 | | - true, |
| 163 | + expect(failedSpy).toHaveBeenCalledWith( |
| 164 | + childItems[2], |
| 165 | + [ |
| 166 | + { |
| 167 | + message: "Test failed", |
| 168 | + }, |
| 169 | + ], |
| 170 | + 11000, |
179 | 171 | ); |
| 172 | + expect(failedSpy).toBeCalledTimes(1); |
| 173 | + expect(endSpy).toBeCalledTimes(1); |
180 | 174 | }); |
181 | | - |
182 | | - function mockRunTests() { |
183 | | - // runTests is an async generator function. This is not directly supported in sinon |
184 | | - // However, we can pretend the same thing by just returning an async array. |
185 | | - runTestsSpy.mockReturnValue( |
186 | | - (async function* () { |
187 | | - yield Promise.resolve({ |
188 | | - test: Uri.parse("file:/ab/c/d.ql").fsPath, |
189 | | - pass: true, |
190 | | - messages: [], |
191 | | - }); |
192 | | - yield Promise.resolve({ |
193 | | - test: Uri.parse("file:/ab/c/e/f/g.ql").fsPath, |
194 | | - pass: false, |
195 | | - diff: ["pqr", "xyz"], |
196 | | - // a compile error |
197 | | - failureStage: "COMPILATION", |
198 | | - messages: [ |
199 | | - { position: { line: 1 }, message: "abc", severity: "ERROR" }, |
200 | | - ], |
201 | | - }); |
202 | | - yield Promise.resolve({ |
203 | | - test: Uri.parse("file:/ab/c/e/f/h.ql").fsPath, |
204 | | - pass: false, |
205 | | - diff: ["jkh", "tuv"], |
206 | | - failureStage: "RESULT", |
207 | | - messages: [], |
208 | | - }); |
209 | | - })(), |
210 | | - ); |
211 | | - } |
212 | 175 | }); |
0 commit comments