From 45a297a8e526094e8fce6e2c5c0fd89b381d1765 Mon Sep 17 00:00:00 2001 From: ewy Date: Tue, 14 Apr 2026 16:37:17 +0200 Subject: i have to commit at some point! --- search/search.go | 86 ++++++++++++++++++++++++++++++++++++++ search/search_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 search/search.go create mode 100644 search/search_test.go (limited to 'search') diff --git a/search/search.go b/search/search.go new file mode 100644 index 0000000..5b227b2 --- /dev/null +++ b/search/search.go @@ -0,0 +1,86 @@ +package search + +import ( + "pik/model" + "slices" +) + +func Search(s *model.State, args ...string) (model.Target, *model.Source, bool, []string, []string) { + var target model.Target + var suspect model.Target + var suspectSource *model.Source + var targetSource *model.Source + var forward []string + var subdir []string + confirm := false + +args_loop: + for _, a := range args { + for _, src := range s.Sources { + + if targetSource == nil { + if src.Is(a) { + targetSource = src + for _, t := range targetSource.Targets { + if t.Matches(a) { + target = t + continue args_loop + } + } + continue args_loop + } + } + + if target == nil && targetSource == nil { + for _, t := range src.Targets { + if t.Matches(a) { + target = t + targetSource = src + continue args_loop + } + } + } else if target == nil { // && targetSource == nil (but it is always true) + for _, t := range targetSource.Targets { + if t.Matches(a) { + target = t + continue args_loop + } + } + // if we find the right target + for _, t := range src.Targets { + if t.Matches(a) { + confirm = true + suspect = t + suspectSource = src + continue args_loop + } + } + } + + } + + if target == nil { + subdir = append(subdir, a) + continue args_loop + } else if targetSource != nil { + forward = append(forward, a) + continue args_loop + } + } + + if suspect != nil && target == nil { + target = suspect + targetSource = suspectSource + confirm = true + } + + if target != nil && target.Sub() != nil && subdir != nil && !slices.Equal(target.Sub(), subdir) { + confirm = true + } + + if target == nil { + forward = args + } + + return target, targetSource, confirm, subdir, forward +} diff --git a/search/search_test.go b/search/search_test.go new file mode 100644 index 0000000..59edef1 --- /dev/null +++ b/search/search_test.go @@ -0,0 +1,111 @@ +package search + +import ( + "github.com/stretchr/testify/assert" + "pik/testx" + "testing" +) + +func TestSearch_TargetOnly(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "def")) + target, source, _, _, _ := Search(st, "def") + testx.AssertSourceIs(t, "src", source) + testx.AssertTargetIs(t, "def", target) +} + +func TestSearch_TargetAndSource(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "def")) + target, source, _, _, _ := Search(st, "src", "def") + testx.AssertSourceIs(t, "src", source) + testx.AssertTargetIs(t, "def", target) +} + +func TestSearch_SourceDefaultTarget(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "src")) + target, src, _, _, _ := Search(st, "src") + testx.AssertSourceIs(t, "src", src) + assert.NotNil(t, target) +} + +func TestSearch_SubdirWrong(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "src")) + st.Sources[0].Targets = append(st.Sources[0].Targets, testx.TestTarget{ + Identifier: "script", + SubValue: []string{"subdir"}, + }) + target, src, confirm, sd, _ := Search(st, "wrong", "script") + testx.AssertSourceIs(t, "src", src) + testx.AssertTargetIs(t, "script", target) + assert.Equal(t, sd, []string{"wrong"}) + assert.NotNil(t, target) + assert.True(t, confirm) +} + +func TestSearch_SubdirMissing(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "src")) + st.Sources[0].Targets = append(st.Sources[0].Targets, testx.TestTarget{ + Identifier: "script", + SubValue: []string{"subdir"}, + }) + target, src, confirm, sd, _ := Search(st, "script") + testx.AssertSourceIs(t, "src", src) + testx.AssertTargetIs(t, "script", target) + assert.Nil(t, sd) + assert.NotNil(t, target) + assert.False(t, confirm) +} + +func TestSearch_Args(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "def")) + target, source, _, _, args := Search(st, "def", "a1", "a2") + testx.AssertSourceIs(t, "src", source) + testx.AssertTargetIs(t, "def", target) + assert.Equal(t, []string{"a1", "a2"}, args) +} + +func TestSearch_Args_SubdirMissing(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "src")) + st.Sources[0].Targets = append(st.Sources[0].Targets, testx.TestTarget{ + Identifier: "script", + SubValue: []string{"subdir"}, + }) + target, src, _, _, args := Search(st, "script", "a1", "a2") + testx.AssertSourceIs(t, "src", src) + testx.AssertTargetIs(t, "script", target) + assert.Equal(t, []string{"a1", "a2"}, args) +} + +func TestSearch_Args_SubdirPresent(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "src")) + st.Sources[0].Targets = append(st.Sources[0].Targets, testx.TestTarget{ + Identifier: "script", + SubValue: []string{"subdir"}, + }) + target, src, _, _, args := Search(st, "subdir", "script", "a1", "a2") + testx.AssertSourceIs(t, "src", src) + testx.AssertTargetIs(t, "script", target) + assert.Equal(t, []string{"a1", "a2"}, args) +} + +func TestSearch_SecondarySource(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "def"), testx.TSource("aaa", "hjkl")) + target, source, _, _, _ := Search(st, "aaa", "hjkl") + testx.AssertSourceIs(t, "aaa", source) + testx.AssertTargetIs(t, "hjkl", target) +} + +func TestSearch_SecondarySource_DuplicateTargetName(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc", "def"), testx.TSource("aaa", "abc")) + target, source, confirm, _, _ := Search(st, "aaa", "def") + testx.AssertSourceIs(t, "src", source) + testx.AssertTargetIs(t, "def", target) + assert.True(t, confirm) +} + +func TestSearch_SourceTargetMixup(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc"), testx.TSource("aaa", "ccc")) + target, source, confirm, _, _ := Search(st, "src", "ccc") + testx.AssertSourceIs(t, "aaa", source) + testx.AssertTargetIs(t, "ccc", target) + assert.True(t, confirm) +} -- cgit v1.3