@@ -4,7 +4,8 @@ import * as path from "path";
44import test from "ava" ;
55import * as sinon from "sinon" ;
66
7- import { runQueries } from "./analyze" ;
7+ import * as actionsUtil from "./actions-util" ;
8+ import { exportedForTesting , runQueries } from "./analyze" ;
89import { setCodeQL } from "./codeql" ;
910import { Feature } from "./feature-flags" ;
1011import { Language } from "./languages" ;
@@ -119,3 +120,190 @@ test("status report fields", async (t) => {
119120 }
120121 } ) ;
121122} ) ;
123+
124+ function runGetDiffRanges ( changes : number , patch : string [ ] | undefined ) : any {
125+ sinon
126+ . stub ( actionsUtil , "getRequiredInput" )
127+ . withArgs ( "checkout_path" )
128+ . returns ( "/checkout/path" ) ;
129+ return exportedForTesting . getDiffRanges (
130+ {
131+ filename : "test.txt" ,
132+ changes,
133+ patch : patch ?. join ( "\n" ) ,
134+ } ,
135+ getRunnerLogger ( true ) ,
136+ ) ;
137+ }
138+
139+ test ( "getDiffRanges: file unchanged" , async ( t ) => {
140+ const diffRanges = runGetDiffRanges ( 0 , undefined ) ;
141+ t . deepEqual ( diffRanges , [ ] ) ;
142+ } ) ;
143+
144+ test ( "getDiffRanges: file diff too large" , async ( t ) => {
145+ const diffRanges = runGetDiffRanges ( 1000000 , undefined ) ;
146+ t . deepEqual ( diffRanges , undefined ) ;
147+ } ) ;
148+
149+ test ( "getDiffRanges: diff thunk with single addition range" , async ( t ) => {
150+ const diffRanges = runGetDiffRanges ( 2 , [
151+ "@@ -30,6 +50,8 @@" ,
152+ " a" ,
153+ " b" ,
154+ " c" ,
155+ "+1" ,
156+ "+2" ,
157+ " d" ,
158+ " e" ,
159+ " f" ,
160+ ] ) ;
161+ t . deepEqual ( diffRanges , [
162+ {
163+ path : "/checkout/path/test.txt" ,
164+ startLine : 53 ,
165+ endLine : 54 ,
166+ } ,
167+ ] ) ;
168+ } ) ;
169+
170+ test ( "getDiffRanges: diff thunk with single deletion range" , async ( t ) => {
171+ const diffRanges = runGetDiffRanges ( 2 , [
172+ "@@ -30,8 +50,6 @@" ,
173+ " a" ,
174+ " b" ,
175+ " c" ,
176+ "-1" ,
177+ "-2" ,
178+ " d" ,
179+ " e" ,
180+ " f" ,
181+ ] ) ;
182+ t . deepEqual ( diffRanges , [ ] ) ;
183+ } ) ;
184+
185+ test ( "getDiffRanges: diff thunk with single update range" , async ( t ) => {
186+ const diffRanges = runGetDiffRanges ( 2 , [
187+ "@@ -30,7 +50,7 @@" ,
188+ " a" ,
189+ " b" ,
190+ " c" ,
191+ "-1" ,
192+ "+2" ,
193+ " d" ,
194+ " e" ,
195+ " f" ,
196+ ] ) ;
197+ t . deepEqual ( diffRanges , [
198+ {
199+ path : "/checkout/path/test.txt" ,
200+ startLine : 53 ,
201+ endLine : 53 ,
202+ } ,
203+ ] ) ;
204+ } ) ;
205+
206+ test ( "getDiffRanges: diff thunk with addition ranges" , async ( t ) => {
207+ const diffRanges = runGetDiffRanges ( 2 , [
208+ "@@ -30,7 +50,9 @@" ,
209+ " a" ,
210+ " b" ,
211+ " c" ,
212+ "+1" ,
213+ " c" ,
214+ "+2" ,
215+ " d" ,
216+ " e" ,
217+ " f" ,
218+ ] ) ;
219+ t . deepEqual ( diffRanges , [
220+ {
221+ path : "/checkout/path/test.txt" ,
222+ startLine : 53 ,
223+ endLine : 53 ,
224+ } ,
225+ {
226+ path : "/checkout/path/test.txt" ,
227+ startLine : 55 ,
228+ endLine : 55 ,
229+ } ,
230+ ] ) ;
231+ } ) ;
232+
233+ test ( "getDiffRanges: diff thunk with mixed ranges" , async ( t ) => {
234+ const diffRanges = runGetDiffRanges ( 2 , [
235+ "@@ -30,7 +50,7 @@" ,
236+ " a" ,
237+ " b" ,
238+ " c" ,
239+ "-1" ,
240+ " d" ,
241+ "-2" ,
242+ "+3" ,
243+ " e" ,
244+ " f" ,
245+ "+4" ,
246+ "+5" ,
247+ " g" ,
248+ " h" ,
249+ " i" ,
250+ ] ) ;
251+ t . deepEqual ( diffRanges , [
252+ {
253+ path : "/checkout/path/test.txt" ,
254+ startLine : 54 ,
255+ endLine : 54 ,
256+ } ,
257+ {
258+ path : "/checkout/path/test.txt" ,
259+ startLine : 57 ,
260+ endLine : 58 ,
261+ } ,
262+ ] ) ;
263+ } ) ;
264+
265+ test ( "getDiffRanges: multiple diff thunks" , async ( t ) => {
266+ const diffRanges = runGetDiffRanges ( 2 , [
267+ "@@ -30,6 +50,8 @@" ,
268+ " a" ,
269+ " b" ,
270+ " c" ,
271+ "+1" ,
272+ "+2" ,
273+ " d" ,
274+ " e" ,
275+ " f" ,
276+ "@@ -130,6 +150,8 @@" ,
277+ " a" ,
278+ " b" ,
279+ " c" ,
280+ "+1" ,
281+ "+2" ,
282+ " d" ,
283+ " e" ,
284+ " f" ,
285+ ] ) ;
286+ t . deepEqual ( diffRanges , [
287+ {
288+ path : "/checkout/path/test.txt" ,
289+ startLine : 53 ,
290+ endLine : 54 ,
291+ } ,
292+ {
293+ path : "/checkout/path/test.txt" ,
294+ startLine : 153 ,
295+ endLine : 154 ,
296+ } ,
297+ ] ) ;
298+ } ) ;
299+
300+ test ( "getDiffRanges: no diff context lines" , async ( t ) => {
301+ const diffRanges = runGetDiffRanges ( 2 , [ "@@ -30 +50,2 @@" , "+1" , "+2" ] ) ;
302+ t . deepEqual ( diffRanges , [
303+ {
304+ path : "/checkout/path/test.txt" ,
305+ startLine : 50 ,
306+ endLine : 51 ,
307+ } ,
308+ ] ) ;
309+ } ) ;
0 commit comments