From 49f453a195b1f9cbfb63ba9b2e108026a403064e Mon Sep 17 00:00:00 2001 From: Jacopo Andrea Giola Date: Fri, 21 Jun 2024 12:34:44 +0200 Subject: [PATCH] feat: improve code coverage --- pkg/cmd/root.go | 6 ----- pkg/cmd/util/util_test.go | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 9a6367b..3e40e45 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -17,7 +17,6 @@ package cmd import ( "context" - "flag" "fmt" "log" "runtime" @@ -93,10 +92,5 @@ func versionString() string { version = fmt.Sprintf("%s (%s)", version, BuildDate) } - // don't return GoVersion during a test run for consistent test output - if flag.Lookup("test.v") != nil { - return version - } - return fmt.Sprintf("%s, Go Version: %s", version, runtime.Version()) } diff --git a/pkg/cmd/util/util_test.go b/pkg/cmd/util/util_test.go index 272e8e9..ebab4df 100644 --- a/pkg/cmd/util/util_test.go +++ b/pkg/cmd/util/util_test.go @@ -17,11 +17,13 @@ package util import ( "bytes" + "os" "path/filepath" "testing" "github.com/mia-platform/vab/pkg/apis/vab.mia-platform.eu/v1alpha1" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestWriteKustomizationData(t *testing.T) { @@ -114,3 +116,47 @@ func TestGroupFromConfig(t *testing.T) { }) } } + +func TestValidateContextPath(t *testing.T) { + t.Parallel() + + tempDir := t.TempDir() + tests := map[string]struct { + path string + expectedPath string + expectedError string + }{ + "valid path": { + path: tempDir, + expectedPath: tempDir, + }, + "path is not a directory": { + path: func() string { + filePath := filepath.Join(tempDir, "file") + _, err := os.Create(filepath.Join(tempDir, "file")) + require.NoError(t, err) + return filePath + }(), + expectedPath: filepath.Join(tempDir, "file"), + expectedError: "is not a directory", + }, + "path don't exists": { + path: filepath.Join("/", "invalid", "path"), + expectedPath: filepath.Join("/", "invalid", "path"), + expectedError: "no such file or directory", + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + resultPath, err := ValidateContextPath(test.path) + switch len(test.expectedError) { + case 0: + assert.NoError(t, err) + default: + assert.Error(t, err) + } + assert.Equal(t, test.expectedPath, resultPath) + }) + } +}