github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/swift/cocoapods/cocoapods_test.go (about)

     1  package cocoapods
     2  
     3  import (
     4  	"os"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/devseccon/trivy/pkg/fanal/analyzer"
    12  	"github.com/devseccon/trivy/pkg/fanal/types"
    13  )
    14  
    15  func Test_cocoaPodsLockAnalyzer_Analyze(t *testing.T) {
    16  	tests := []struct {
    17  		name      string
    18  		inputFile string
    19  		want      *analyzer.AnalysisResult
    20  	}{
    21  		{
    22  			name:      "happy path",
    23  			inputFile: "testdata/happy.lock",
    24  			want: &analyzer.AnalysisResult{
    25  				Applications: []types.Application{
    26  					{
    27  						Type:     types.Cocoapods,
    28  						FilePath: "testdata/happy.lock",
    29  						Libraries: types.Packages{
    30  							{
    31  								ID:      "AppCenter@4.2.0",
    32  								Name:    "AppCenter",
    33  								Version: "4.2.0",
    34  								DependsOn: []string{
    35  									"AppCenter/Analytics@4.2.0",
    36  									"AppCenter/Crashes@4.2.0",
    37  								},
    38  							},
    39  							{
    40  								ID:      "AppCenter/Analytics@4.2.0",
    41  								Name:    "AppCenter/Analytics",
    42  								Version: "4.2.0",
    43  								DependsOn: []string{
    44  									"AppCenter/Core@4.2.0",
    45  								},
    46  							},
    47  							{
    48  								ID:      "AppCenter/Core@4.2.0",
    49  								Name:    "AppCenter/Core",
    50  								Version: "4.2.0",
    51  							},
    52  							{
    53  								ID:      "AppCenter/Crashes@4.2.0",
    54  								Name:    "AppCenter/Crashes",
    55  								Version: "4.2.0",
    56  								DependsOn: []string{
    57  									"AppCenter/Core@4.2.0",
    58  								},
    59  							},
    60  							{
    61  								ID:      "KeychainAccess@4.2.1",
    62  								Name:    "KeychainAccess",
    63  								Version: "4.2.1",
    64  							},
    65  						},
    66  					},
    67  				},
    68  			},
    69  		},
    70  		{
    71  			name:      "empty file",
    72  			inputFile: "testdata/empty.lock",
    73  		},
    74  	}
    75  
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			f, err := os.Open(tt.inputFile)
    79  			require.NoError(t, err)
    80  			defer f.Close()
    81  
    82  			a := cocoaPodsLockAnalyzer{}
    83  			got, err := a.Analyze(nil, analyzer.AnalysisInput{
    84  				FilePath: tt.inputFile,
    85  				Content:  f,
    86  			})
    87  
    88  			if got != nil {
    89  				for _, app := range got.Applications {
    90  					sort.Sort(app.Libraries)
    91  				}
    92  			}
    93  
    94  			assert.NoError(t, err)
    95  			assert.Equal(t, tt.want, got)
    96  		})
    97  	}
    98  }