@@ -473,3 +473,144 @@ func TestDockerComposeExtractorWithLineNumbersAndIndices(t *testing.T) {
473473
474474 t .Logf ("Image '%s' found at line %d, indices [%d:%d]" , img .Name , loc .Line , loc .StartIndex , loc .EndIndex )
475475}
476+
477+ // TestExtractFilesWithFullHelmDirectoryFalse tests the ExtractFiles method when isFullHelmDirectory is false
478+ func TestExtractFilesWithFullHelmDirectoryFalse (t * testing.T ) {
479+ extractor := NewImagesExtractor ()
480+
481+ scenarios := []struct {
482+ Name string
483+ InputPath string
484+ ShouldHaveHelm bool
485+ ExpectedErrString string
486+ }{
487+ {
488+ Name : "ValidHelmDirectoryWithFalseFlag" ,
489+ InputPath : "../../test_files/helm-testcases" ,
490+ ShouldHaveHelm : true ,
491+ ExpectedErrString : "" ,
492+ },
493+ {
494+ Name : "ValidHelmDirectoryWithTemplatesSubdirectory" ,
495+ InputPath : "../../test_files/helm-testcases/templates" ,
496+ ShouldHaveHelm : true ,
497+ ExpectedErrString : "" ,
498+ },
499+ {
500+ Name : "NonHelmDirectoryWithFalseFlag" ,
501+ InputPath : "../../test_files/imageExtraction/dockerfiles" ,
502+ ShouldHaveHelm : false ,
503+ ExpectedErrString : "" ,
504+ },
505+ }
506+
507+ // Run test scenarios
508+ for _ , scenario := range scenarios {
509+ t .Run (scenario .Name , func (t * testing.T ) {
510+ // Run the function with isFullHelmDirectory = false
511+ files , _ , _ , err := extractor .ExtractFiles (scenario .InputPath , false )
512+
513+ // Check for errors
514+ if scenario .ExpectedErrString != "" {
515+ if err == nil || ! strings .Contains (err .Error (), scenario .ExpectedErrString ) {
516+ t .Errorf ("Expected error containing '%s' but got '%v'" , scenario .ExpectedErrString , err )
517+ }
518+ } else {
519+ // Check that Helm charts are found when expected
520+ if scenario .ShouldHaveHelm {
521+ if len (files .Helm ) == 0 {
522+ t .Errorf ("Expected Helm charts to be found for scenario '%s'" , scenario .Name )
523+ } else {
524+ // Verify basic structure
525+ chart := files .Helm [0 ]
526+ if chart .Directory != "../../test_files/helm-testcases" && chart .Directory != "..\\ ..\\ test_files\\ helm-testcases" {
527+ t .Errorf ("Expected directory '../../test_files/helm-testcases', got '%s'" , chart .Directory )
528+ }
529+ if chart .ValuesFile != "values-extra.yaml" {
530+ t .Errorf ("Expected values file 'values-extra.yaml', got '%s'" , chart .ValuesFile )
531+ }
532+ if len (chart .TemplateFiles ) == 0 {
533+ t .Errorf ("Expected template files to be found" )
534+ }
535+ }
536+ } else {
537+ if len (files .Helm ) > 0 {
538+ t .Errorf ("Expected no Helm charts for scenario '%s', but found %d" , scenario .Name , len (files .Helm ))
539+ }
540+ }
541+ }
542+ })
543+ }
544+ }
545+
546+ // TestExtractFilesWithFullHelmDirectoryFalseErrorCases tests error scenarios when isFullHelmDirectory is false
547+ func TestExtractFilesWithFullHelmDirectoryFalseErrorCases (t * testing.T ) {
548+ extractor := NewImagesExtractor ()
549+
550+ scenarios := []struct {
551+ Name string
552+ InputPath string
553+ ExpectedErrString string
554+ }{
555+ {
556+ Name : "NonExistentDirectory" ,
557+ InputPath : "../../test_files/non-existent-directory" ,
558+ ExpectedErrString : "unsupported file type" ,
559+ },
560+ {
561+ Name : "FileInsteadOfDirectory" ,
562+ InputPath : "../../test_files/helm-testcases/Chart.yaml" ,
563+ ExpectedErrString : "unsupported file type" ,
564+ },
565+ }
566+
567+ // Run test scenarios
568+ for _ , scenario := range scenarios {
569+ t .Run (scenario .Name , func (t * testing.T ) {
570+ // Run the function with isFullHelmDirectory = false
571+ _ , _ , _ , err := extractor .ExtractFiles (scenario .InputPath , false )
572+
573+ // Check for errors
574+ if err == nil || ! strings .Contains (err .Error (), scenario .ExpectedErrString ) {
575+ t .Errorf ("Expected error containing '%s' but got '%v'" , scenario .ExpectedErrString , err )
576+ }
577+ })
578+ }
579+ }
580+
581+ // TestExtractFilesWithFullHelmDirectoryFalseDefaultBehavior tests that the default behavior (no parameter) is the same as true
582+ func TestExtractFilesWithFullHelmDirectoryFalseDefaultBehavior (t * testing.T ) {
583+ extractor := NewImagesExtractor ()
584+
585+ // Test that calling ExtractFiles without the isFullHelmDirectory parameter
586+ // produces the same result as calling it with true
587+ scanPath := "../../test_files/imageExtraction"
588+
589+ filesDefault , settingsDefault , pathDefault , errDefault := extractor .ExtractFiles (scanPath )
590+ filesTrue , settingsTrue , pathTrue , errTrue := extractor .ExtractFiles (scanPath , true )
591+
592+ // Check that both calls return the same results
593+ if errDefault != nil || errTrue != nil {
594+ t .Errorf ("Unexpected errors: default=%v, true=%v" , errDefault , errTrue )
595+ }
596+
597+ if pathDefault != pathTrue {
598+ t .Errorf ("Expected same path, got default=%s, true=%s" , pathDefault , pathTrue )
599+ }
600+
601+ if ! CompareDockerfiles (filesDefault .Dockerfile , filesTrue .Dockerfile ) {
602+ t .Errorf ("Dockerfiles mismatch between default and true calls" )
603+ }
604+
605+ if ! CompareDockerCompose (filesDefault .DockerCompose , filesTrue .DockerCompose ) {
606+ t .Errorf ("Docker Compose files mismatch between default and true calls" )
607+ }
608+
609+ if ! CompareHelm (filesDefault .Helm , filesTrue .Helm ) {
610+ t .Errorf ("Helm charts mismatch between default and true calls" )
611+ }
612+
613+ if ! CompareSettingsFiles (settingsDefault , settingsTrue ) {
614+ t .Errorf ("Settings files mismatch between default and true calls" )
615+ }
616+ }
0 commit comments