From f403f1790b4765902a7cefb9800122129e94d516 Mon Sep 17 00:00:00 2001 From: Dmitry Ledentsov Date: Sun, 31 Dec 2023 15:51:11 +0100 Subject: [PATCH] test passing candidate sources to the results --- common/candidate_source.go | 2 +- common/nix_candidate_source.go | 13 ++++++++++ common/results.go | 46 ++++++++++++++++++++++++---------- common/results_test.go | 38 ++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 common/nix_candidate_source.go diff --git a/common/candidate_source.go b/common/candidate_source.go index f1a06c8..9cbdd21 100644 --- a/common/candidate_source.go +++ b/common/candidate_source.go @@ -11,5 +11,5 @@ type PathSetIn struct { } type CandidateSource interface { - WhereSet(somePath string) PathSetIn + WhereSet(somePath string) *PathSetIn } diff --git a/common/nix_candidate_source.go b/common/nix_candidate_source.go new file mode 100644 index 0000000..0e8e051 --- /dev/null +++ b/common/nix_candidate_source.go @@ -0,0 +1,13 @@ +package common + +type NixCandidateSource struct { + fs Filesystem +} + +func NewNixCandidateSource(fs Filesystem) CandidateSource { + return &NixCandidateSource{fs} +} + +func (s *NixCandidateSource) WhereSet(somePath string) *PathSetIn { + return nil +} diff --git a/common/results.go b/common/results.go index b628519..fa18ee0 100644 --- a/common/results.go +++ b/common/results.go @@ -22,19 +22,25 @@ type positionLookup multimap.MultiMap[string, int] func duplicatePredicate(a, b int) bool { return true } type ResultsCalculator struct { - fs Filesystem - source ValueSource - pathsLookup positionLookup + fs Filesystem + source ValueSource + pathsLookup positionLookup + candidateSource CandidateSource } -func NewResultsCalculator(fs Filesystem, source ValueSource) *ResultsCalculator { +func NewCustomResultsCalculator(fs Filesystem, source ValueSource, candidateSource CandidateSource) *ResultsCalculator { return &ResultsCalculator{ - fs: fs, - source: source, - pathsLookup: multimap.NewMapSet[string](duplicatePredicate), + fs: fs, + source: source, + pathsLookup: multimap.NewMapSet[string](duplicatePredicate), + candidateSource: candidateSource, } } +func NewResultsCalculator(fs Filesystem, source ValueSource) *ResultsCalculator { + return NewCustomResultsCalculator(fs, source, NewNixCandidateSource(fs)) +} + func (r *ResultsCalculator) CalculateResults() ([]ResultRow, error) { if len(r.source.Values()) == 0 { return nil, errors.New(r.source.Source() + " is empty") @@ -59,18 +65,32 @@ func (r *ResultsCalculator) calculateResultRows() []ResultRow { pathKey := r.fs.GetAbsolutePath(path) dup := getDuplicatesOf(r.pathsLookup, pathKey, index) exists, isdir := r.fs.PathStatus(pathKey) + candidateSources := r.getCandidateSourcesFor(path) res = append(res, ResultRow{ - Id: index + 1, - Path: path, - ExpandedPath: pathKey, - Exists: exists, - IsDir: isdir, - Duplicates: dup, + Id: index + 1, + Path: path, + ExpandedPath: pathKey, + Exists: exists, + IsDir: isdir, + Duplicates: dup, + CandidateSources: candidateSources, }) } return res } +func (r *ResultsCalculator) getCandidateSourcesFor(path string) []string { + res := r.candidateSource.WhereSet(path) + if res == nil { + return nil + } + candidateSources := []string{} + for _, source := range res.WhereSet { + candidateSources = append(candidateSources, source.Original) + } + return candidateSources +} + // returns the IDs of the duplicate indices (== index+1) func getDuplicatesOf(pathsLookup multimap.MultiMap[string, int], pathKey string, index int) []int { instances := pathsLookup.Get(pathKey) diff --git a/common/results_test.go b/common/results_test.go index 32dd3a7..bcbf372 100644 --- a/common/results_test.go +++ b/common/results_test.go @@ -1,6 +1,7 @@ package common import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -63,6 +64,34 @@ func Test_no_duplicates(t *testing.T) { mockFs.AssertExpectations(t) } +func Test_candidate_sources_not_empty_when_found(t *testing.T) { + mockFs := new(mockFilesystem) + + // setup expectations + mockFs.On("GetAbsolutePath", "a").Return("a") + mockFs.On("GetAbsolutePath", "b").Return("b") + mockFs.On("PathStatus", mock.Anything).Return(true, true) + + sut := NewCustomResultsCalculator(mockFs, &mockValueSource{values_: []string{ + "a", + "b", + }}, &mockCandidateSource{ + map_: map[string]*PathSetIn{ + // a is found, but not b + "a": { + What: Location{"a", "a"}, + WhereSet: []Location{ + {"a", "a"}, + }, + }, + }, + }) + rows, _ := sut.CalculateResults() + require.Len(t, rows[0].CandidateSources, 1) + assert.Equal(t, "a", rows[0].CandidateSources[0]) + assert.Nil(t, rows[1].CandidateSources) +} + type mockFilesystem struct { mock.Mock } @@ -94,3 +123,12 @@ func (m *mockValueSource) Orig() string { func (m *mockValueSource) Values() []string { return m.values_ } + +type mockCandidateSource struct { + map_ map[string]*PathSetIn +} + +func (s *mockCandidateSource) WhereSet(somePath string) *PathSetIn { + fmt.Println("TRYING", somePath, s.map_[somePath]) + return s.map_[somePath] +}