Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
adding utils unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hoang <mhoang@redhat.com>
  • Loading branch information
mike-hoang committed Jun 1, 2023
1 parent 3593452 commit 919dfaf
Show file tree
Hide file tree
Showing 10 changed files with 1,545 additions and 2 deletions.
4 changes: 3 additions & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
golang.org/x/mod v0.10.0
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -19,6 +20,7 @@ require (
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/cloudflare/circl v1.3.2 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
Expand All @@ -30,10 +32,10 @@ require (
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pjbgf/sha1cd v0.2.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/net v0.9.0 // indirect
Expand Down
48 changes: 48 additions & 0 deletions go/pkg/utils/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils

import (
"context"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetCachedFilePathsFromRoot(t *testing.T) {
missingPathErr := "no such file or directory"

tests := []struct {
name string
root string
ctx context.Context
expectedFilePaths []string
expectedError *string
}{
{
name: "Case 1: Cached file paths exist",
root: "path/to/root",
ctx: context.WithValue(context.Background(), key("mapFilePathsFromRoot"), map[string][]string{
"path/to/root": {"f1.txt", "f2.txt"},
}),
expectedFilePaths: []string{"f1.txt", "f2.txt"},
expectedError: nil,
},
{
name: "Case 2: Invalid file path",
root: "invalid/path",
ctx: context.Background(),
expectedFilePaths: []string{},
expectedError: &missingPathErr,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filePaths, err := GetCachedFilePathsFromRoot(tt.root, &tt.ctx)

if err != nil {
assert.Regexp(t, *tt.expectedError, err.Error(), "Error message should match")
}

assert.EqualValues(t, tt.expectedFilePaths, filePaths)
})
}
}
3 changes: 2 additions & 1 deletion go/pkg/utils/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ func GetStringValueFromEnvFile(root string, regex string) string {
return ""
}

func getEnvFileContent(root string) (string, error) {
// getEnvFileContent is exposed as a global variable for the purpose of running mock tests
var getEnvFileContent = func(root string) (string, error) {
envPath := filepath.Join(root, ".env")
bytes, err := os.ReadFile(envPath)
if err != nil {
Expand Down
Loading

0 comments on commit 919dfaf

Please sign in to comment.