Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KICS not rendering Chart bug #2761 #2762

Merged
merged 1 commit into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/engine/provider/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (s *FileSystemSourceProvider) checkConditions(info os.FileInfo, extensions
if err != nil {
return true, nil
}
return false, nil
}

if f, ok := s.excludes[info.Name()]; ok && containsFile(f, info) {
Expand Down
75 changes: 63 additions & 12 deletions pkg/engine/provider/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package provider
import (
"context"
"io"
"io/fs"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/Checkmarx/kics/pkg/model"
dockerParser "github.com/Checkmarx/kics/pkg/parser/docker"
"github.com/Checkmarx/kics/test"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -185,7 +187,16 @@ func TestFileSystemSourceProvider_GetSources(t *testing.T) { //nolint

// TestFileSystemSourceProvider_checkConditions tests the functions [checkConditions()] and all the methods called by them
func TestFileSystemSourceProvider_checkConditions(t *testing.T) {
infoFile, _ := os.Stat("../../../assets/queries")
if err := test.ChangeCurrentDir("kics"); err != nil {
t.Errorf("failed to change dir: %s", err)
}
infoFile, err := os.Stat(filepath.FromSlash("assets/queries"))
checkStatErr(t, err)
fileInfoSlice := []fs.FileInfo{
infoFile,
}
infoHelm, errHelm := os.Stat(filepath.FromSlash("test/fixtures/test_helm"))
checkStatErr(t, errHelm)
type fields struct {
path string
excludes map[string][]os.FileInfo
Expand All @@ -195,36 +206,70 @@ func TestFileSystemSourceProvider_checkConditions(t *testing.T) {
extensions model.Extensions
path string
}
type want struct {
got bool
err error
}
tests := []struct {
name string
fields fields
args args
want struct {
got bool
err error
}
want want
}{
{
name: "check_conditions",
fields: fields{
path: "../../assets/queries",
path: filepath.FromSlash("assets/queries"),
excludes: nil,
},
args: args{
info: infoFile,
extensions: model.Extensions{
".dockerfile": dockerParser.Parser{},
},
path: "../../assets/queries",
path: filepath.FromSlash("assets/queries"),
},
want: struct {
got bool
err error
}{
want: want{
got: true,
err: nil,
},
},
{
name: "check_conditions_chart",
fields: fields{
path: filepath.FromSlash("test/fixtures/test_helm"),
excludes: nil,
},
args: args{
info: infoHelm,
extensions: model.Extensions{},
path: filepath.FromSlash("test/fixtures/test_helm"),
},
want: want{
got: false,
err: nil,
},
},
{
name: "should_skip_folder",
fields: fields{
path: filepath.FromSlash("assets/queries"),
excludes: map[string][]fs.FileInfo{
"queries": fileInfoSlice,
},
},
args: args{
info: infoFile,
extensions: model.Extensions{
".dockerfile": dockerParser.Parser{},
},
path: filepath.FromSlash("assets/queries"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -233,7 +278,7 @@ func TestFileSystemSourceProvider_checkConditions(t *testing.T) {
excludes: tt.fields.excludes,
}
if got, err := s.checkConditions(tt.args.info, tt.args.extensions, tt.args.path); got != tt.want.got || err != tt.want.err {
t.Errorf("FileSystemSourceProvider.checkConditions() = %v, want %v", got, tt.want)
t.Errorf("FileSystemSourceProvider.checkConditions() = %v, want %v", err, tt.want)
}
})
}
Expand All @@ -254,3 +299,9 @@ var mockResolverSink = func(ctx context.Context, filename string) error {
var mockErrResolverSink = func(ctx context.Context, filename string) error {
return errors.New("")
}

func checkStatErr(t *testing.T, err error) {
if err != nil {
t.Errorf("failed to get info: %s", err)
}
}