From dac777b75211f7426a644ef35aa5f588f7062734 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Thu, 30 May 2024 12:00:20 +0200 Subject: [PATCH] refactor(cmd): improve error message when resource file is not referenced Signed-off-by: Xavier Coulon --- pkg/validation/components.go | 5 ++--- pkg/validation/components_test.go | 2 +- pkg/validation/resources.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/validation/components.go b/pkg/validation/components.go index 1513c0e..302542e 100644 --- a/pkg/validation/components.go +++ b/pkg/validation/components.go @@ -1,7 +1,6 @@ package validation import ( - "fmt" "io/fs" "path/filepath" @@ -31,12 +30,12 @@ func CheckComponents(logger Logger, afs afero.Afero, baseDir string, components // look for a Kustomization file in the directory if kp, found := lookupKustomizationFile(logger, afs, path); found { if err := checkKustomizeResources(logger, afs, kp); err != nil { - return fmt.Errorf("invalid resources at %s: %w", path, err) + return err } if d.Name() != "base" { logger.Debug("checking Kustomization build ", "path", path) if err := checkBuild(logger, fsys, path); err != nil { - return fmt.Errorf("invalid resources at %s: %w", path, err) + return err } } } diff --git a/pkg/validation/components_test.go b/pkg/validation/components_test.go index a2baa53..3ad2560 100644 --- a/pkg/validation/components_test.go +++ b/pkg/validation/components_test.go @@ -330,7 +330,7 @@ data: err = validation.CheckComponents(logger, afs, "/path/to", "components") // then - require.Error(t, err, "resource is not referenced: /path/to/components/configmap2.yaml") + require.EqualError(t, err, "resource is not referenced in /path/to/components/kustomization.yaml: configmap2.yaml") assert.Empty(t, logger.Errors()) assert.Empty(t, logger.Warnings()) }) diff --git a/pkg/validation/resources.go b/pkg/validation/resources.go index 7666fae..7891495 100644 --- a/pkg/validation/resources.go +++ b/pkg/validation/resources.go @@ -86,7 +86,7 @@ entries: continue entries } } - return fmt.Errorf("resource is not referenced: %s", filepath.Join(path, e.Name())) + return fmt.Errorf("resource is not referenced in %s: %s", path, e.Name()) } return nil }