diff --git a/internal/sliceutils/sliceutils.go b/internal/sliceutils/sliceutils.go index 133591cb03..67981165d5 100644 --- a/internal/sliceutils/sliceutils.go +++ b/internal/sliceutils/sliceutils.go @@ -2,8 +2,14 @@ package sliceutils import "golang.org/x/text/unicode/norm" -func RemoveFromStrings(slice []string, n int) []string { - return append(slice[:n], slice[n+1:]...) +func RemoveFromStrings(slice []string, indexes ...int) []string { + var out []string + for i, s := range slice { + if !intsContain(indexes, i) { + out = append(out, s) + } + } + return out[:len(out)] } func GetInt(slice []int, index int) (int, bool) { @@ -26,4 +32,13 @@ func IntRangeUncapped(in []int, start, end int) []int { end = len(in) } return in[start:end] -} \ No newline at end of file +} + +func intsContain(ns []int, v int) bool { + for _, n := range ns { + if n == v { + return true + } + } + return false +} diff --git a/internal/sliceutils/sliceutils_test.go b/internal/sliceutils/sliceutils_test.go index 6a4ab67009..36e4c0ed3d 100644 --- a/internal/sliceutils/sliceutils_test.go +++ b/internal/sliceutils/sliceutils_test.go @@ -8,26 +8,59 @@ import ( func TestRemoveFromStrings(t *testing.T) { type args struct { slice []string - n int + ns []int } + + simpleSlice := []string{"0", "1", "2", "3"} + integers := func(ns ...int) []int { return ns } + tests := []struct { name string args args want []string }{ { - "Removes Index", - args{ - []string{"1", "2", "3"}, - 1, - }, - []string{"1", "3"}, + "first index", + args{simpleSlice, integers(0)}, + []string{"1", "2", "3"}, + }, + { + "last index", + args{simpleSlice, integers(len(simpleSlice) - 1)}, + []string{"0", "1", "2"}, + }, + { + "inner index", + args{simpleSlice, integers(1)}, + []string{"0", "2", "3"}, + }, + { + "multiple indexes", + args{simpleSlice, integers(1, len(simpleSlice)-1)}, + []string{"0", "2"}, + }, + { + "index too low", + args{simpleSlice, integers(-1)}, + simpleSlice, + }, + { + "index too high", + args{simpleSlice, integers(42)}, + simpleSlice, + }, + { + "nil indexes", + args{simpleSlice, nil}, + simpleSlice, }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := RemoveFromStrings(tt.args.slice, tt.args.n); !reflect.DeepEqual(got, tt.want) { - t.Errorf("RemoveFromStrings() = %v, want %v", got, tt.want) + got := RemoveFromStrings(tt.args.slice, tt.args.ns...) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("got %v, want %v", got, tt.want) } }) } @@ -148,4 +181,4 @@ func TestRange(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/pkg/projectfile/projectfile.go b/pkg/projectfile/projectfile.go index 8dae6472b4..d8b810552b 100644 --- a/pkg/projectfile/projectfile.go +++ b/pkg/projectfile/projectfile.go @@ -1314,11 +1314,13 @@ func CleanProjectMapping(cfg ConfigGetter) { seen := map[string]bool{} for namespace, paths := range projects { + var removals []int for i, path := range paths { if !fileutils.DirExists(path) { - projects[namespace] = sliceutils.RemoveFromStrings(projects[namespace], i) + removals = append(removals, i) } } + projects[namespace] = sliceutils.RemoveFromStrings(projects[namespace], removals...) if ok, _ := seen[strings.ToLower(namespace)]; ok || len(projects[namespace]) == 0 { delete(projects, namespace) continue