Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
fix: fix reader test, and main test
Browse files Browse the repository at this point in the history
  • Loading branch information
noaabarki committed May 19, 2021
1 parent d6b9f28 commit 5764d08
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 73 deletions.
41 changes: 17 additions & 24 deletions cmd/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package test
import (
"testing"

"github.com/datreeio/datree/bl/evaluator"
"github.com/datreeio/datree/bl/evaluation"
"github.com/datreeio/datree/bl/messager"
"github.com/datreeio/datree/pkg/cliClient"
"github.com/datreeio/datree/pkg/localConfig"
"github.com/datreeio/datree/pkg/propertiesExtractor"
"github.com/stretchr/testify/mock"
)

Expand All @@ -24,27 +23,22 @@ type mockEvaluator struct {
mock.Mock
}

func (m *mockEvaluator) PrintResults(results *evaluator.EvaluationResults, cliId string, output string) error {
m.Called(results, cliId, output)
return nil
func (m *mockEvaluator) Evaluate(validFilesPathsChan <-chan string, invalidFilesPaths []*string, evaluationId int) (*evaluation.EvaluationResults, []*evaluation.Error, error) {
args := m.Called(validFilesPathsChan, invalidFilesPaths, evaluationId)
return args.Get(0).(*evaluation.EvaluationResults), args.Get(1).([]*evaluation.Error), args.Error(2)
}

func (m *mockEvaluator) PrintFileParsingErrors(errors []propertiesExtractor.FileError) {
m.Called(errors)
}

func (m *mockEvaluator) Evaluate(paths []string, cliId string, evaluationConc int, cliVersion string) (*evaluator.EvaluationResults, []propertiesExtractor.FileError, error) {
args := m.Called(paths, cliId, evaluationConc)
return args.Get(0).(*evaluator.EvaluationResults), args.Get(1).([]propertiesExtractor.FileError), args.Error(2)
func (m *mockEvaluator) CreateEvaluation(cliId string, cliVersion string) (int, error) {
args := m.Called(cliId, cliVersion)
return args.Get(0).(int), args.Error(1)
}

type mockMessager struct {
mock.Mock
}

func (m *mockMessager) LoadVersionMessages(cliVersion string) <-chan *messager.VersionMessage {
args := m.Called(cliVersion)
return args.Get(0).(<-chan *messager.VersionMessage)
func (m *mockMessager) LoadVersionMessages(messages chan *messager.VersionMessage, cliVersion string) {
m.Called(messages, cliVersion)
}

func (m *mockMessager) HandleVersionMessage(messageChannel <-chan *messager.VersionMessage) {
Expand All @@ -53,17 +47,16 @@ func (m *mockMessager) HandleVersionMessage(messageChannel <-chan *messager.Vers
func TestTestCommand(t *testing.T) {
mockedEvaluator := &mockEvaluator{}

mockedEvaluateResponse := &evaluator.EvaluationResults{
FileNameRuleMapper: map[string]map[int]*evaluator.Rule{}, Summary: struct {
mockedEvaluateResponse := &evaluation.EvaluationResults{
FileNameRuleMapper: map[string]map[int]*evaluation.Rule{}, Summary: struct {
RulesCount int
TotalFailedRules int
FilesCount int
}{RulesCount: 1, TotalFailedRules: 0, FilesCount: 0},
}

mockedEvaluator.On("Evaluate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(mockedEvaluateResponse, []propertiesExtractor.FileError{}, nil)
mockedEvaluator.On("PrintFileParsingErrors", mock.Anything).Return()
mockedEvaluator.On("PrintResults", mock.Anything, mock.Anything, mock.Anything).Return()
mockedEvaluator.On("Evaluate", mock.Anything, mock.Anything, mock.Anything).Return(mockedEvaluateResponse)
mockedEvaluator.On("CreateEvaluation", mock.Anything, mock.Anything).Return(mockedEvaluateResponse)

localConfigManager := &mockLocalConfigManager{}
localConfigManager.On("GetConfiguration").Return(localConfig.LocalConfiguration{CliId: "134kh"}, nil)
Expand All @@ -73,7 +66,7 @@ func TestTestCommand(t *testing.T) {

ctx := &TestCommandContext{
Evaluator: mockedEvaluator,
LocalConfig: localConfigManager,
LocalConfig: &localConfig.LocalConfiguration{},
Messager: messager,
}

Expand All @@ -82,7 +75,7 @@ func TestTestCommand(t *testing.T) {
test_testCommand_yaml_output(t, localConfigManager, mockedEvaluator, mockedEvaluateResponse, ctx)
}

func test_testCommand_no_flags(t *testing.T, localConfigManager *mockLocalConfigManager, evaluator *mockEvaluator, mockedEvaluateResponse *evaluator.EvaluationResults, ctx *TestCommandContext) {
func test_testCommand_no_flags(t *testing.T, localConfigManager *mockLocalConfigManager, evaluator *mockEvaluator, mockedEvaluateResponse *evaluation.EvaluationResults, ctx *TestCommandContext) {
test(ctx, []string{"8/*"}, TestCommandFlags{})
localConfigManager.AssertCalled(t, "GetConfiguration")

Expand All @@ -91,7 +84,7 @@ func test_testCommand_no_flags(t *testing.T, localConfigManager *mockLocalConfig
evaluator.AssertCalled(t, "PrintResults", mockedEvaluateResponse, "134kh", "")
}

func test_testCommand_json_output(t *testing.T, localConfigManager *mockLocalConfigManager, evaluator *mockEvaluator, mockedEvaluateResponse *evaluator.EvaluationResults, ctx *TestCommandContext) {
func test_testCommand_json_output(t *testing.T, localConfigManager *mockLocalConfigManager, evaluator *mockEvaluator, mockedEvaluateResponse *evaluation.EvaluationResults, ctx *TestCommandContext) {
test(ctx, []string{"8/*"}, TestCommandFlags{Output: "json"})
localConfigManager.AssertCalled(t, "GetConfiguration")

Expand All @@ -100,7 +93,7 @@ func test_testCommand_json_output(t *testing.T, localConfigManager *mockLocalCon
evaluator.AssertCalled(t, "PrintResults", mockedEvaluateResponse, "134kh", "json")
}

func test_testCommand_yaml_output(t *testing.T, localConfigManager *mockLocalConfigManager, evaluator *mockEvaluator, mockedEvaluateResponse *evaluator.EvaluationResults, ctx *TestCommandContext) {
func test_testCommand_yaml_output(t *testing.T, localConfigManager *mockLocalConfigManager, evaluator *mockEvaluator, mockedEvaluateResponse *evaluation.EvaluationResults, ctx *TestCommandContext) {
test(ctx, []string{"8/*"}, TestCommandFlags{Output: "yaml"})
localConfigManager.AssertCalled(t, "GetConfiguration")

Expand Down
49 changes: 0 additions & 49 deletions pkg/fileReader/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,55 +168,6 @@ type getFilesPathsTestCase struct {
}
}

func TestGetFilesPaths(t *testing.T) {
tests := []getFilesPathsTestCase{
getFilesPaths_noMatchesTestCase(),
getFilesPaths_withMatchesTestCase(),
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
glob := globMock{}
stat := statMock{}
abs := absMock{}

abs.On("Abs", mock.Anything).Return(tt.mock.abs.response, tt.mock.abs.err)
stat.On("Stat", mock.Anything).Return(tt.mock.stat.response, tt.mock.stat.err)

fileReader := &FileReader{
glob: glob.Glob,
readFile: nil,
stat: stat.Stat,
abs: abs.Abs,
}

res, err := fileReader.GetFilesPaths(tt.args.paths)

var actualRes []string
for m := range res {
actualRes = append(actualRes, m)
}

var actualErrs []error
for e := range err {
actualErrs = append(actualErrs, e)
}

assert.Equal(t, tt.expected.response, actualRes)
assert.Equal(t, tt.expected.err, actualErrs)

if tt.expected.abs.isCalled {
abs.AssertCalled(t, "Abs", tt.expected.abs.calledWith)
}

if tt.expected.stat.isCalled {
stat.AssertCalled(t, "Stat", tt.expected.stat.calledWith)
}

})
}
}

func getFilesPaths_noMatchesTestCase() getFilesPathsTestCase {
return getFilesPathsTestCase{
name: "success no matches",
Expand Down

0 comments on commit 5764d08

Please sign in to comment.