From d75bce22db40b17424f578e70edaef6e73935a1a Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Thu, 27 Jun 2024 09:34:39 +0600 Subject: [PATCH] refactor: use early return --- .../language/conda/environment/environment.go | 37 ++++++++++--------- .../conda/environment/environment_test.go | 4 ++ .../conda/environment/testdata/empty.yaml | 5 +++ 3 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 pkg/fanal/analyzer/language/conda/environment/testdata/empty.yaml diff --git a/pkg/fanal/analyzer/language/conda/environment/environment.go b/pkg/fanal/analyzer/language/conda/environment/environment.go index cd0465412ade..e20d20b10fe5 100644 --- a/pkg/fanal/analyzer/language/conda/environment/environment.go +++ b/pkg/fanal/analyzer/language/conda/environment/environment.go @@ -32,25 +32,28 @@ func (a environmentAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisI return nil, xerrors.Errorf("unable to parse environment.yaml: %w", err) } - if res != nil && len(res.Applications) > 0 { - once := sync.Once{} - // For `environment.yaml` Applications always contains only 1 Application - for i, pkg := range res.Applications[0].Packages { - // Skip packages without a version, because in this case we will not be able to get the correct file name. - if pkg.Version != "" { - licenses, err := findLicenseFromEnvDir(pkg) - if err != nil { - // Show log once per file - once.Do(func() { - log.WithPrefix("conda").Debug("License not found. For more information, see https://aquasecurity.github.io/trivy/latest/docs/coverage/os/conda/#licenses", - log.String("file", input.FilePath), log.String("pkg", pkg.Name), log.Err(err)) - }) - } - pkg.Licenses = licenses + if res == nil { + return nil, nil + } + + once := sync.Once{} + // res always contains only 1 Application + // cf. https://github.com/aquasecurity/trivy/blob/0ccdbfbb6598a52de7cda603ab22e794f710e86c/pkg/fanal/analyzer/language/analyze.go#L32 + for i, pkg := range res.Applications[0].Packages { + // Skip packages without a version, because in this case we will not be able to get the correct file name. + if pkg.Version != "" { + licenses, err := findLicenseFromEnvDir(pkg) + if err != nil { + // Show log once per file + once.Do(func() { + log.WithPrefix("conda").Debug("License not found. For more information, see https://aquasecurity.github.io/trivy/latest/docs/coverage/os/conda/#licenses", + log.String("file", input.FilePath), log.String("pkg", pkg.Name), log.Err(err)) + }) } - pkg.FilePath = "" // remove `prefix` from FilePath - res.Applications[0].Packages[i] = pkg + pkg.Licenses = licenses } + pkg.FilePath = "" // remove `prefix` from FilePath + res.Applications[0].Packages[i] = pkg } diff --git a/pkg/fanal/analyzer/language/conda/environment/environment_test.go b/pkg/fanal/analyzer/language/conda/environment/environment_test.go index a312ceb1198e..02a188a87f2a 100644 --- a/pkg/fanal/analyzer/language/conda/environment/environment_test.go +++ b/pkg/fanal/analyzer/language/conda/environment/environment_test.go @@ -131,6 +131,10 @@ func Test_environmentAnalyzer_Analyze(t *testing.T) { }, }, }, + { + name: "empty", + inputFile: "testdata/empty.yaml", + }, { name: "invalid", inputFile: "testdata/invalid.yaml", diff --git a/pkg/fanal/analyzer/language/conda/environment/testdata/empty.yaml b/pkg/fanal/analyzer/language/conda/environment/testdata/empty.yaml new file mode 100644 index 000000000000..f9622b8a1030 --- /dev/null +++ b/pkg/fanal/analyzer/language/conda/environment/testdata/empty.yaml @@ -0,0 +1,5 @@ +name: test-env +channels: + - defaults +dependencies: +prefix: /opt/conda/envs/test-env