diff options
| author | ewy <ewy0@protonmail.com> | 2026-04-22 18:29:37 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-04-22 18:29:37 +0200 |
| commit | 374ed5e1a4dc635c42e33e4133729d40cf3e0e35 (patch) | |
| tree | 01f12ca3eea0154b61afde3d1ee27df2094644e1 /search | |
| parent | ee481fa406bb685a16d8b568fca0c9f592c06180 (diff) | |
work on tests
also replace the annoying search return with a struct
Diffstat (limited to 'search')
| -rw-r--r-- | search/search.go | 66 | ||||
| -rw-r--r-- | search/search_test.go | 117 |
2 files changed, 109 insertions, 74 deletions
diff --git a/search/search.go b/search/search.go index 5b227b2..009dd49 100644 --- a/search/search.go +++ b/search/search.go @@ -5,50 +5,70 @@ import ( "slices" ) -func Search(s *model.State, args ...string) (model.Target, *model.Source, bool, []string, []string) { +type Result struct { + Target model.Target + Source *model.Source + NeedsConfirmation bool + Overridden bool + Sub []string + Args []string +} + +// Search is the meat of pik +func Search(s *model.State, args ...string) *Result { var target model.Target - var suspect model.Target - var suspectSource *model.Source var targetSource *model.Source - var forward []string + var confirm bool + var overridden bool var subdir []string - confirm := false + var forward []string + var suspect model.Target + var suspectSource *model.Source args_loop: - for _, a := range args { + for _, arg := range args { for _, src := range s.Sources { if targetSource == nil { - if src.Is(a) { + if src.Is(arg) { targetSource = src + + // try to look for arg target with the same name as the source + // "default target" of sorts for _, t := range targetSource.Targets { - if t.Matches(a) { + if t.Matches(arg) { target = t continue args_loop } } + continue args_loop } } if target == nil && targetSource == nil { + + // uncertain about source, check ours to see if any match for _, t := range src.Targets { - if t.Matches(a) { + if t.Matches(arg) { target = t targetSource = src continue args_loop } } + } else if target == nil { // && targetSource == nil (but it is always true) + + // source located, for _, t := range targetSource.Targets { - if t.Matches(a) { + if t.Matches(arg) { target = t continue args_loop } } // if we find the right target for _, t := range src.Targets { - if t.Matches(a) { + if t.Matches(arg) { confirm = true suspect = t suspectSource = src @@ -60,10 +80,10 @@ args_loop: } if target == nil { - subdir = append(subdir, a) + subdir = append(subdir, arg) continue args_loop } else if targetSource != nil { - forward = append(forward, a) + forward = append(forward, arg) continue args_loop } } @@ -82,5 +102,23 @@ args_loop: forward = args } - return target, targetSource, confirm, subdir, forward + if target != nil && targetSource != nil { + for _, t := range targetSource.Targets { + if slices.Equal(t.Invocation(targetSource), target.Invocation(targetSource)) { + if t.Tags().Has(model.Override) { + overridden = true + target = t + } + } + } + } + + return &Result{ + Target: target, + Source: targetSource, + NeedsConfirmation: confirm, + Overridden: overridden, + Sub: subdir, + Args: forward, + } } diff --git a/search/search_test.go b/search/search_test.go index 59edef1..b5fc835 100644 --- a/search/search_test.go +++ b/search/search_test.go @@ -1,3 +1,5 @@ +//go:build test + package search import ( @@ -8,104 +10,99 @@ import ( 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) + res := Search(st, "def") + testx.AssertSourceIs(t, "src", res.Source) + testx.AssertTargetIs(t, "def", res.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) + res := Search(st, "src", "def") + testx.AssertSourceIs(t, "src", res.Source) + testx.AssertTargetIs(t, "def", res.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) + res := Search(st, "src") + testx.AssertSourceIs(t, "src", res.Source) + assert.NotNil(t, res.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) + st.Sources[0].Targets = append(st.Sources[0].Targets, testx.TTarget("script", "subdir")) + res := Search(st, "wrong", "script") + testx.AssertSourceIs(t, "src", res.Source) + testx.AssertTargetIs(t, "script", res.Target) + assert.Equal(t, []string{"wrong"}, res.Sub) + assert.NotNil(t, res.Target) + assert.True(t, res.NeedsConfirmation) } 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) + st.Sources[0].Targets = append(st.Sources[0].Targets, testx.TTarget("script", "subdir")) + res := Search(st, "script") + testx.AssertSourceIs(t, "src", res.Source) + testx.AssertTargetIs(t, "script", res.Target) + assert.Nil(t, res.Sub) + assert.NotNil(t, res.Target) + assert.False(t, res.NeedsConfirmation) } 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) + res := Search(st, "def", "a1", "a2") + testx.AssertSourceIs(t, "src", res.Source) + testx.AssertTargetIs(t, "def", res.Target) + assert.Equal(t, []string{"a1", "a2"}, res.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) + st.Sources[0].Targets = append(st.Sources[0].Targets, testx.TTarget("script", "subdir")) + res := Search(st, "script", "a1", "a2") + testx.AssertSourceIs(t, "src", res.Source) + testx.AssertTargetIs(t, "script", res.Target) + assert.Equal(t, []string{"a1", "a2"}, res.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) + st.Sources[0].Targets = append(st.Sources[0].Targets, testx.TTarget("script", "subdir")) + res := Search(st, "subdir", "script", "a1", "a2") + testx.AssertSourceIs(t, "src", res.Source) + testx.AssertTargetIs(t, "script", res.Target) + assert.Equal(t, []string{"a1", "a2"}, res.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) + res := Search(st, "aaa", "hjkl") + testx.AssertSourceIs(t, "aaa", res.Source) + testx.AssertTargetIs(t, "hjkl", res.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) + res := Search(st, "aaa", "def") + testx.AssertSourceIs(t, "src", res.Source) + testx.AssertTargetIs(t, "def", res.Target) + assert.True(t, res.NeedsConfirmation) } 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) + res := Search(st, "src", "ccc") + testx.AssertSourceIs(t, "aaa", res.Source) + testx.AssertTargetIs(t, "ccc", res.Target) + assert.True(t, res.NeedsConfirmation) +} + +func TestSearch_Override(t *testing.T) { + st := testx.TState(testx.TSource("src", "abc.override.sh", "abc.sh")) + res := Search(st, "src", "abc") + assert.Equal(t, "abc.override.sh", res.Target.(*testx.TestTarget).Id.Full) + assert.False(t, res.NeedsConfirmation) } |
