Skip to content

Commit 9bc5344

Browse files
committed
Fix tests for creating new model file
1 parent 2c31335 commit 9bc5344

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

extensions/ql-vscode/test/vscode-tests/no-workspace/data-extensions-editor/extension-pack-picker.test.ts

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { CancellationTokenSource, QuickPickItem, window } from "vscode";
2+
import { dump as dumpYaml } from "js-yaml";
3+
import { outputFile } from "fs-extra";
4+
import { join } from "path";
5+
import { dir } from "tmp-promise";
26

37
import { pickExtensionPackModelFile } from "../../../../src/data-extensions-editor/extension-pack-picker";
48
import { QlpacksInfo, ResolveExtensionsResult } from "../../../../src/cli";
@@ -30,11 +34,15 @@ describe("pickExtensionPackModelFile", () => {
3034

3135
const progress = jest.fn();
3236
let showQuickPickSpy: jest.SpiedFunction<typeof window.showQuickPick>;
37+
let showInputBoxSpy: jest.SpiedFunction<typeof window.showInputBox>;
3338

3439
beforeEach(() => {
3540
showQuickPickSpy = jest
3641
.spyOn(window, "showQuickPick")
3742
.mockRejectedValue(new Error("Unexpected call to showQuickPick"));
43+
showInputBoxSpy = jest
44+
.spyOn(window, "showInputBox")
45+
.mockRejectedValue(new Error("Unexpected call to showInputBox"));
3846
});
3947

4048
it("allows choosing an existing extension pack and model file", async () => {
@@ -72,17 +80,23 @@ describe("pickExtensionPackModelFile", () => {
7280
{
7381
title: expect.any(String),
7482
},
83+
token,
7584
);
7685
expect(showQuickPickSpy).toHaveBeenCalledWith(
7786
[
7887
{
7988
label: "models/model.yml",
8089
file: "/a/b/c/my-extension-pack/models/model.yml",
8190
},
91+
{
92+
label: expect.stringMatching(/create/i),
93+
file: null,
94+
},
8295
],
8396
{
8497
title: expect.any(String),
8598
},
99+
token,
86100
);
87101
expect(cliServer.resolveQlpacks).toHaveBeenCalledTimes(1);
88102
expect(cliServer.resolveQlpacks).toHaveBeenCalledWith([], true);
@@ -110,7 +124,7 @@ describe("pickExtensionPackModelFile", () => {
110124
expect(cliServer.resolveExtensions).not.toHaveBeenCalled();
111125
});
112126

113-
it("does not show any options when there are no extension packs", async () => {
127+
it("shows create option when there are no extension packs", async () => {
114128
const cliServer = mockCliServer({}, { models: [], data: {} });
115129

116130
showQuickPickSpy.mockResolvedValueOnce(undefined);
@@ -124,9 +138,13 @@ describe("pickExtensionPackModelFile", () => {
124138
),
125139
).toEqual(undefined);
126140
expect(showQuickPickSpy).toHaveBeenCalledTimes(1);
127-
expect(showQuickPickSpy).toHaveBeenCalledWith([], {
128-
title: expect.any(String),
129-
});
141+
expect(showQuickPickSpy).toHaveBeenCalledWith(
142+
[],
143+
{
144+
title: expect.any(String),
145+
},
146+
token,
147+
);
130148
expect(cliServer.resolveQlpacks).toHaveBeenCalled();
131149
expect(cliServer.resolveExtensions).not.toHaveBeenCalled();
132150
});
@@ -191,14 +209,37 @@ describe("pickExtensionPackModelFile", () => {
191209
expect(cliServer.resolveExtensions).toHaveBeenCalled();
192210
});
193211

194-
it("does not show any options when there are no model files", async () => {
195-
const cliServer = mockCliServer(qlPacks, { models: [], data: {} });
212+
it("shows create input box when there are no model files", async () => {
213+
const tmpDir = await dir({
214+
unsafeCleanup: true,
215+
});
216+
217+
const cliServer = mockCliServer(
218+
{
219+
"my-extension-pack": [tmpDir.path],
220+
},
221+
{ models: [], data: {} },
222+
);
223+
224+
await outputFile(
225+
join(tmpDir.path, "codeql-pack.yml"),
226+
dumpYaml({
227+
name: "my-extension-pack",
228+
version: "0.0.0",
229+
library: true,
230+
extensionTargets: {
231+
"codeql/java-all": "*",
232+
},
233+
dataExtensions: ["models/**/*.yml"],
234+
}),
235+
);
196236

197237
showQuickPickSpy.mockResolvedValueOnce({
198238
label: "my-extension-pack",
199239
extensionPack: "my-extension-pack",
200240
} as QuickPickItem);
201241
showQuickPickSpy.mockResolvedValueOnce(undefined);
242+
showInputBoxSpy.mockResolvedValue("models/my-model.yml");
202243

203244
expect(
204245
await pickExtensionPackModelFile(
@@ -207,26 +248,16 @@ describe("pickExtensionPackModelFile", () => {
207248
progress,
208249
token,
209250
),
210-
).toEqual(undefined);
211-
expect(showQuickPickSpy).toHaveBeenCalledTimes(2);
212-
expect(showQuickPickSpy).toHaveBeenCalledWith(
213-
[
214-
{
215-
label: "my-extension-pack",
216-
extensionPack: "my-extension-pack",
217-
},
218-
{
219-
label: "another-extension-pack",
220-
extensionPack: "another-extension-pack",
221-
},
222-
],
251+
).toEqual(join(tmpDir.path, "models/my-model.yml"));
252+
expect(showQuickPickSpy).toHaveBeenCalledTimes(1);
253+
expect(showInputBoxSpy).toHaveBeenCalledWith(
223254
{
224255
title: expect.any(String),
256+
value: "models/github.vscode-codeql.model.yml",
257+
validateInput: expect.any(Function),
225258
},
259+
token,
226260
);
227-
expect(showQuickPickSpy).toHaveBeenCalledWith([], {
228-
title: expect.any(String),
229-
});
230261
expect(cliServer.resolveQlpacks).toHaveBeenCalled();
231262
expect(cliServer.resolveExtensions).toHaveBeenCalled();
232263
});

0 commit comments

Comments
 (0)