@@ -26,6 +26,7 @@ import Test.Effect (expectationFailure', it', shouldBe')
26
26
import Test.Fixtures qualified as Fixtures
27
27
import Test.Hspec (Spec , describe , it , runIO , shouldBe )
28
28
import Test.MockApi (alwaysReturns )
29
+ import Types (GlobFilter (GlobFilter ), LicenseScanPathFilters (.. ))
29
30
30
31
customLicenseLernieMatchData :: LernieMatchData
31
32
customLicenseLernieMatchData =
@@ -259,11 +260,21 @@ keywordSearchGrepEntry =
259
260
, grepEntryName = " Keyword Search"
260
261
}
261
262
263
+ expectedLicenseScanPathFilters :: Maybe LicenseScanPathFilters
264
+ expectedLicenseScanPathFilters =
265
+ Just
266
+ LicenseScanPathFilters
267
+ { licenseScanPathFiltersOnly = [GlobFilter " /*" , GlobFilter " /**" ]
268
+ , licenseScanPathFiltersExclude = []
269
+ , licenseScanPathFilterFileExclude = []
270
+ }
271
+
262
272
expectedLernieConfig :: LernieConfig
263
273
expectedLernieConfig =
264
274
LernieConfig
265
275
{ rootDir = absDir
266
276
, regexes = [customLicenseLernieRegex, keywordSearchLernieRegex]
277
+ , licenseScanPathFilters = expectedLicenseScanPathFilters
267
278
, fullFiles = False
268
279
}
269
280
@@ -307,7 +318,7 @@ spec = do
307
318
308
319
describe " grepOptionsToLernieConfig" $ do
309
320
it " should create a lernie config" $ do
310
- grepOptionsToLernieConfig absDir grepOptions FileUploadMatchData `shouldBe` Just expectedLernieConfig
321
+ grepOptionsToLernieConfig absDir grepOptions expectedLicenseScanPathFilters FileUploadMatchData `shouldBe` Just expectedLernieConfig
311
322
312
323
describe " analyzeWithLernie" $ do
313
324
currDir <- runIO getCurrentDir
@@ -320,7 +331,7 @@ spec = do
320
331
let fixedOnePath = fromMaybe onePath (Text. stripSuffix (toText pathSeparator) onePath)
321
332
322
333
it' " should analyze a directory with the provided config if no API keys are passed in" $ do
323
- result <- ignoreDebug . withoutTelemetry $ analyzeWithLernie scanDir Nothing grepOptions{configFilePath = (Just $ scanDir </> $ (mkRelFile " .fossa.yml" ))}
334
+ result <- ignoreDebug . withoutTelemetry $ analyzeWithLernie scanDir Nothing grepOptions{configFilePath = (Just $ scanDir </> $ (mkRelFile " .fossa.yml" ))} Nothing
324
335
-- Fix the paths in the expected data. We need to do this here because they include the full path to the file
325
336
let actualUnitData =
326
337
expectedUnitData
@@ -352,7 +363,7 @@ spec = do
352
363
353
364
it' " should include the file contents if the org has the full-files flag on" $ do
354
365
GetOrganization `alwaysReturns` Fixtures. organization{orgCustomLicenseScanConfigs = [secondCustomLicenseGrepEntry], orgRequiresFullFileUploads = True }
355
- result <- ignoreDebug . withoutTelemetry $ analyzeWithLernieWithOrgInfo scanDir grepOptions
366
+ result <- ignoreDebug . withoutTelemetry $ analyzeWithLernieWithOrgInfo scanDir grepOptions Nothing
356
367
case result of
357
368
Nothing -> expectationFailure' " analyzeWithLernie should not return Nothing"
358
369
Just res -> do
@@ -375,7 +386,7 @@ spec = do
375
386
376
387
it' " should not include the file contents if the org has the full-files flag off" $ do
377
388
GetOrganization `alwaysReturns` Fixtures. organization{orgCustomLicenseScanConfigs = [secondCustomLicenseGrepEntry], orgRequiresFullFileUploads = False }
378
- result <- ignoreDebug . withoutTelemetry $ analyzeWithLernieWithOrgInfo scanDir grepOptions
389
+ result <- ignoreDebug . withoutTelemetry $ analyzeWithLernieWithOrgInfo scanDir grepOptions Nothing
379
390
case result of
380
391
Nothing -> expectationFailure' " analyzeWithLernie should not return Nothing"
381
392
Just res -> do
@@ -391,10 +402,46 @@ spec = do
391
402
392
403
it' " should merge the config from fossa.yml and the org" $ do
393
404
GetOrganization `alwaysReturns` Fixtures. organization{orgCustomLicenseScanConfigs = [secondCustomLicenseGrepEntry]}
394
- result <- ignoreDebug . withoutTelemetry $ analyzeWithLernieWithOrgInfo scanDir grepOptions
405
+ result <- ignoreDebug . withoutTelemetry $ analyzeWithLernieWithOrgInfo scanDir grepOptions Nothing
395
406
case result of
396
407
Nothing -> expectationFailure' " analyzeWithLernie should not return Nothing"
397
408
Just res -> do
398
409
-- Just assert that we find matches for "Confidential" (from the org API) and "Proprietary License" (from grepOptions)
399
410
let matchNames = lernieMatchDataName <$> concatMap lernieMatchMatches (lernieResultsCustomLicenses res)
400
411
sort (nub matchNames) `shouldBe'` [" Confidential" , " Proprietary License" ]
412
+
413
+ it' " should handle Nothing licenseScanPathFilters without crashing" $ do
414
+ result <- ignoreDebug . withoutTelemetry $ analyzeWithLernie scanDir Nothing grepOptions Nothing
415
+ case result of
416
+ Nothing -> expectationFailure' " analyzeWithLernie should not return Nothing"
417
+ Just _ -> pure ()
418
+
419
+ it' " should apply licenseScanPathFilters' only filter correctly" $ do
420
+ let filters =
421
+ LicenseScanPathFilters
422
+ { licenseScanPathFiltersOnly = [GlobFilter " **/*one.txt" ]
423
+ , licenseScanPathFiltersExclude = [GlobFilter " **/*something.txt" ]
424
+ , licenseScanPathFilterFileExclude = []
425
+ }
426
+
427
+ result <- ignoreDebug . withoutTelemetry $ analyzeWithLernie scanDir Nothing grepOptions (Just filters)
428
+ case result of
429
+ Nothing -> expectationFailure' " analyzeWithLernie should not return Nothing when given valid licenseScanPathFilters"
430
+ Just res -> do
431
+ let matchPaths = sort $ nub $ map lernieMatchPath (lernieResultsCustomLicenses res ++ lernieResultsKeywordSearches res)
432
+ matchPaths `shouldBe'` [fixedOnePath]
433
+
434
+ it' " should apply licenseScanPathFilters' exclude filter correctly" $ do
435
+ let filters =
436
+ LicenseScanPathFilters
437
+ { licenseScanPathFiltersOnly = [GlobFilter " **/*.txt" ]
438
+ , licenseScanPathFiltersExclude = [GlobFilter " **/*something.txt" ]
439
+ , licenseScanPathFilterFileExclude = []
440
+ }
441
+
442
+ result <- ignoreDebug . withoutTelemetry $ analyzeWithLernie scanDir Nothing grepOptions (Just filters)
443
+ case result of
444
+ Nothing -> expectationFailure' " analyzeWithLernie should not return Nothing when given valid licenseScanPathFilters"
445
+ Just res -> do
446
+ let matchPaths = sort $ nub $ map lernieMatchPath (lernieResultsCustomLicenses res ++ lernieResultsKeywordSearches res)
447
+ matchPaths `shouldBe'` [fixedOnePath]
0 commit comments