11import {
22 CancellationTokenSource ,
3+ ConfigurationScope ,
34 QuickPickItem ,
45 Uri ,
56 window ,
67 workspace ,
8+ WorkspaceConfiguration as VSCodeWorkspaceConfiguration ,
79 WorkspaceFolder ,
810} from "vscode" ;
911import { dump as dumpYaml , load as loadYaml } from "js-yaml" ;
@@ -17,6 +19,7 @@ import * as config from "../../../../src/config";
1719import { pickExtensionPack } from "../../../../src/data-extensions-editor/extension-pack-picker" ;
1820import { ExtensionPack } from "../../../../src/data-extensions-editor/shared/extension-pack" ;
1921import { createMockLogger } from "../../../__mocks__/loggerMock" ;
22+ import { vscodeGetConfigurationMock } from "../../test-config" ;
2023
2124describe ( "pickExtensionPack" , ( ) => {
2225 let tmpDir : string ;
@@ -153,6 +156,31 @@ describe("pickExtensionPack", () => {
153156
154157 it ( "automatically selects an extension pack" , async ( ) => {
155158 disableAutoNameExtensionPackSpy . mockReturnValue ( false ) ;
159+ vscodeGetConfigurationMock . mockImplementation (
160+ (
161+ section ?: string ,
162+ scope ?: ConfigurationScope | null ,
163+ ) : VSCodeWorkspaceConfiguration => {
164+ expect ( section ) . toEqual ( "codeQL.dataExtensions" ) ;
165+ expect ( ( scope as any ) ?. languageId ) . toEqual ( "java" ) ;
166+
167+ return {
168+ get : ( key : string ) => {
169+ expect ( key ) . toEqual ( "extensionsDirectory" ) ;
170+ return undefined ;
171+ } ,
172+ has : ( key : string ) => {
173+ return key === "extensionsDirectory" ;
174+ } ,
175+ inspect : ( ) => {
176+ throw new Error ( "inspect not implemented" ) ;
177+ } ,
178+ update : ( ) => {
179+ throw new Error ( "update not implemented" ) ;
180+ } ,
181+ } ;
182+ } ,
183+ ) ;
156184
157185 const cliServer = mockCliServer ( qlPacks ) ;
158186
@@ -167,8 +195,33 @@ describe("pickExtensionPack", () => {
167195 ) ;
168196 } ) ;
169197
170- it ( "automatically creates an extension pack" , async ( ) => {
198+ it ( "automatically creates an extension pack and selects an extensions directory " , async ( ) => {
171199 disableAutoNameExtensionPackSpy . mockReturnValue ( false ) ;
200+ vscodeGetConfigurationMock . mockImplementation (
201+ (
202+ section ?: string ,
203+ scope ?: ConfigurationScope | null ,
204+ ) : VSCodeWorkspaceConfiguration => {
205+ expect ( section ) . toEqual ( "codeQL.dataExtensions" ) ;
206+ expect ( ( scope as any ) ?. languageId ) . toEqual ( "java" ) ;
207+
208+ return {
209+ get : ( key : string ) => {
210+ expect ( key ) . toEqual ( "extensionsDirectory" ) ;
211+ return undefined ;
212+ } ,
213+ has : ( key : string ) => {
214+ return key === "extensionsDirectory" ;
215+ } ,
216+ inspect : ( ) => {
217+ throw new Error ( "inspect not implemented" ) ;
218+ } ,
219+ update : ( ) => {
220+ throw new Error ( "update not implemented" ) ;
221+ } ,
222+ } ;
223+ } ,
224+ ) ;
172225
173226 const tmpDir = await dir ( {
174227 unsafeCleanup : true ,
@@ -237,6 +290,77 @@ describe("pickExtensionPack", () => {
237290 } ) ;
238291 } ) ;
239292
293+ it ( "automatically creates an extension pack when extensions directory is set in config" , async ( ) => {
294+ disableAutoNameExtensionPackSpy . mockReturnValue ( false ) ;
295+
296+ const tmpDir = await dir ( {
297+ unsafeCleanup : true ,
298+ } ) ;
299+
300+ const configExtensionsDir = join (
301+ Uri . file ( tmpDir . path ) . fsPath ,
302+ "my-custom-extensions-directory" ,
303+ ) ;
304+
305+ vscodeGetConfigurationMock . mockImplementation (
306+ (
307+ section ?: string ,
308+ scope ?: ConfigurationScope | null ,
309+ ) : VSCodeWorkspaceConfiguration => {
310+ expect ( section ) . toEqual ( "codeQL.dataExtensions" ) ;
311+ expect ( ( scope as any ) ?. languageId ) . toEqual ( "java" ) ;
312+
313+ return {
314+ get : ( key : string ) => {
315+ expect ( key ) . toEqual ( "extensionsDirectory" ) ;
316+ return configExtensionsDir ;
317+ } ,
318+ has : ( key : string ) => {
319+ return key === "extensionsDirectory" ;
320+ } ,
321+ inspect : ( ) => {
322+ throw new Error ( "inspect not implemented" ) ;
323+ } ,
324+ update : ( ) => {
325+ throw new Error ( "update not implemented" ) ;
326+ } ,
327+ } ;
328+ } ,
329+ ) ;
330+
331+ const newPackDir = join ( configExtensionsDir , "vscode-codeql-java" ) ;
332+
333+ const cliServer = mockCliServer ( { } ) ;
334+
335+ expect (
336+ await pickExtensionPack ( cliServer , databaseItem , logger , progress , token ) ,
337+ ) . toEqual ( {
338+ path : newPackDir ,
339+ yamlPath : join ( newPackDir , "codeql-pack.yml" ) ,
340+ name : "github/vscode-codeql-java" ,
341+ version : "0.0.0" ,
342+ extensionTargets : {
343+ "codeql/java-all" : "*" ,
344+ } ,
345+ dataExtensions : [ "models/**/*.yml" ] ,
346+ } ) ;
347+ expect ( showQuickPickSpy ) . not . toHaveBeenCalled ( ) ;
348+ expect ( showInputBoxSpy ) . not . toHaveBeenCalled ( ) ;
349+ expect ( cliServer . resolveQlpacks ) . toHaveBeenCalled ( ) ;
350+
351+ expect (
352+ loadYaml ( await readFile ( join ( newPackDir , "codeql-pack.yml" ) , "utf8" ) ) ,
353+ ) . toEqual ( {
354+ name : "github/vscode-codeql-java" ,
355+ version : "0.0.0" ,
356+ library : true ,
357+ extensionTargets : {
358+ "codeql/java-all" : "*" ,
359+ } ,
360+ dataExtensions : [ "models/**/*.yml" ] ,
361+ } ) ;
362+ } ) ;
363+
240364 it ( "allows cancelling the prompt" , async ( ) => {
241365 const cliServer = mockCliServer ( qlPacks ) ;
242366
0 commit comments