@@ -7,14 +7,21 @@ import { Uri, workspace } from "vscode";
77import { getErrorMessage } from "../../../src/common/helpers-pure" ;
88import * as tmp from "tmp" ;
99import { mockedObject } from "../utils/mocking.helpers" ;
10+ import { ensureDir , readFile } from "fs-extra" ;
11+ import { load } from "js-yaml" ;
12+ import { QlPackFile } from "../../../src/packaging/qlpack-file" ;
1013
1114describe ( "QlPackGenerator" , ( ) => {
1215 let packFolderPath : string ;
1316 let qlPackYamlFilePath : string ;
1417 let exampleQlFilePath : string ;
1518 let language : string ;
1619 let generator : QlPackGenerator ;
17- let packAddSpy : jest . Mock < any , [ ] > ;
20+ let packAddSpy : jest . MockedFunction < typeof CodeQLCliServer . prototype . packAdd > ;
21+ let resolveQlpacksSpy : jest . MockedFunction <
22+ typeof CodeQLCliServer . prototype . resolveQlpacks
23+ > ;
24+ let mockCli : CodeQLCliServer ;
1825 let dir : tmp . DirResult ;
1926
2027 beforeEach ( async ( ) => {
@@ -29,8 +36,10 @@ describe("QlPackGenerator", () => {
2936 exampleQlFilePath = join ( packFolderPath , "example.ql" ) ;
3037
3138 packAddSpy = jest . fn ( ) ;
32- const mockCli = mockedObject < CodeQLCliServer > ( {
39+ resolveQlpacksSpy = jest . fn ( ) . mockResolvedValue ( { } ) ;
40+ mockCli = mockedObject < CodeQLCliServer > ( {
3341 packAdd : packAddSpy ,
42+ resolveQlpacks : resolveQlpacksSpy ,
3443 } ) ;
3544
3645 generator = new QlPackGenerator (
@@ -71,5 +80,137 @@ describe("QlPackGenerator", () => {
7180 expect ( existsSync ( exampleQlFilePath ) ) . toBe ( true ) ;
7281
7382 expect ( packAddSpy ) . toHaveBeenCalledWith ( packFolderPath , language ) ;
83+
84+ const qlpack = load (
85+ await readFile ( qlPackYamlFilePath , "utf8" ) ,
86+ ) as QlPackFile ;
87+ expect ( qlpack ) . toEqual (
88+ expect . objectContaining ( {
89+ name : "getting-started/codeql-extra-queries-ruby" ,
90+ } ) ,
91+ ) ;
92+ } ) ;
93+
94+ describe ( "when a pack with the same name already exists" , ( ) => {
95+ beforeEach ( ( ) => {
96+ resolveQlpacksSpy . mockResolvedValue ( {
97+ "getting-started/codeql-extra-queries-ruby" : [ "/path/to/pack" ] ,
98+ } ) ;
99+ } ) ;
100+
101+ it ( "should change the name of the pack" , async ( ) => {
102+ await generator . generate ( ) ;
103+
104+ const qlpack = load (
105+ await readFile ( qlPackYamlFilePath , "utf8" ) ,
106+ ) as QlPackFile ;
107+ expect ( qlpack ) . toEqual (
108+ expect . objectContaining ( {
109+ name : "getting-started/codeql-extra-queries-ruby-1" ,
110+ } ) ,
111+ ) ;
112+ } ) ;
113+ } ) ;
114+
115+ describe ( "when the folder name is included in the pack name" , ( ) => {
116+ beforeEach ( async ( ) => {
117+ const parentFolderPath = join ( dir . name , "my-folder" ) ;
118+
119+ packFolderPath = Uri . file (
120+ join ( parentFolderPath , `test-ql-pack-${ language } ` ) ,
121+ ) . fsPath ;
122+ await ensureDir ( parentFolderPath ) ;
123+
124+ qlPackYamlFilePath = join ( packFolderPath , "codeql-pack.yml" ) ;
125+ exampleQlFilePath = join ( packFolderPath , "example.ql" ) ;
126+
127+ generator = new QlPackGenerator (
128+ language as QueryLanguage ,
129+ mockCli ,
130+ packFolderPath ,
131+ true ,
132+ ) ;
133+ } ) ;
134+
135+ it ( "should set the name of the pack" , async ( ) => {
136+ await generator . generate ( ) ;
137+
138+ const qlpack = load (
139+ await readFile ( qlPackYamlFilePath , "utf8" ) ,
140+ ) as QlPackFile ;
141+ expect ( qlpack ) . toEqual (
142+ expect . objectContaining ( {
143+ name : "getting-started/codeql-extra-queries-my-folder-ruby" ,
144+ } ) ,
145+ ) ;
146+ } ) ;
147+
148+ describe ( "when the folder name includes codeql" , ( ) => {
149+ beforeEach ( async ( ) => {
150+ const parentFolderPath = join ( dir . name , "my-codeql" ) ;
151+
152+ packFolderPath = Uri . file (
153+ join ( parentFolderPath , `test-ql-pack-${ language } ` ) ,
154+ ) . fsPath ;
155+ await ensureDir ( parentFolderPath ) ;
156+
157+ qlPackYamlFilePath = join ( packFolderPath , "codeql-pack.yml" ) ;
158+ exampleQlFilePath = join ( packFolderPath , "example.ql" ) ;
159+
160+ generator = new QlPackGenerator (
161+ language as QueryLanguage ,
162+ mockCli ,
163+ packFolderPath ,
164+ true ,
165+ ) ;
166+ } ) ;
167+
168+ it ( "should set the name of the pack" , async ( ) => {
169+ await generator . generate ( ) ;
170+
171+ const qlpack = load (
172+ await readFile ( qlPackYamlFilePath , "utf8" ) ,
173+ ) as QlPackFile ;
174+ expect ( qlpack ) . toEqual (
175+ expect . objectContaining ( {
176+ name : "getting-started/my-codeql-ruby" ,
177+ } ) ,
178+ ) ;
179+ } ) ;
180+ } ) ;
181+
182+ describe ( "when the folder name includes queries" , ( ) => {
183+ beforeEach ( async ( ) => {
184+ const parentFolderPath = join ( dir . name , "my-queries" ) ;
185+
186+ packFolderPath = Uri . file (
187+ join ( parentFolderPath , `test-ql-pack-${ language } ` ) ,
188+ ) . fsPath ;
189+ await ensureDir ( parentFolderPath ) ;
190+
191+ qlPackYamlFilePath = join ( packFolderPath , "codeql-pack.yml" ) ;
192+ exampleQlFilePath = join ( packFolderPath , "example.ql" ) ;
193+
194+ generator = new QlPackGenerator (
195+ language as QueryLanguage ,
196+ mockCli ,
197+ packFolderPath ,
198+ true ,
199+ ) ;
200+ } ) ;
201+
202+ it ( "should set the name of the pack" , async ( ) => {
203+ await generator . generate ( ) ;
204+
205+ const qlpack = load (
206+ await readFile ( qlPackYamlFilePath , "utf8" ) ,
207+ ) as QlPackFile ;
208+ expect ( qlpack ) . toEqual (
209+ expect . objectContaining ( {
210+ name : "getting-started/my-queries-ruby" ,
211+ } ) ,
212+ ) ;
213+ } ) ;
214+ } ) ;
74215 } ) ;
75216} ) ;
0 commit comments