Skip to content

Commit

Permalink
feat: improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiola committed Jun 21, 2024
1 parent ed6e341 commit 49f453a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
6 changes: 0 additions & 6 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"flag"
"fmt"
"log"
"runtime"
Expand Down Expand Up @@ -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())
}
46 changes: 46 additions & 0 deletions pkg/cmd/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 49f453a

Please sign in to comment.