forked from jetify-com/devbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
devbox: fix deduping packages and remove lo (jetify-com#37)
devbox: fix deduping packages and remove lo Fix Devbox.Add to deduplicate packages instead of removing the ones with duplicates. Extract the two functions that we were using in lo to remove it as a dependency.
- Loading branch information
Showing
5 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package devbox | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"golang.org/x/exp/slices" | ||
) | ||
|
||
func TestUnique(t *testing.T) { | ||
cases := []struct{ in, out []string }{ | ||
{ | ||
in: []string{"a", "b", "b", "c"}, | ||
out: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
in: []string{}, | ||
out: []string{}, | ||
}, | ||
{ | ||
in: []string{"a", "b", "c"}, | ||
out: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
in: []string{"a", "a"}, | ||
out: []string{"a"}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(fmt.Sprintf("{%s}", strings.Join(tc.in, ",")), func(t *testing.T) { | ||
got := unique(tc.in) | ||
if !slices.Equal(got, tc.out) { | ||
t.Errorf("Got slice %v, want %v.", got, tc.out) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestExclude(t *testing.T) { | ||
cases := []struct{ in, exclude, out []string }{ | ||
{ | ||
in: []string{}, | ||
exclude: []string{}, | ||
out: []string{}, | ||
}, | ||
{ | ||
in: []string{}, | ||
exclude: []string{"a"}, | ||
out: []string{}, | ||
}, | ||
{ | ||
in: []string{"a"}, | ||
exclude: []string{"a"}, | ||
out: []string{}, | ||
}, | ||
{ | ||
in: []string{"a", "b", "c"}, | ||
exclude: []string{"b"}, | ||
out: []string{"a", "c"}, | ||
}, | ||
{ | ||
in: []string{"a", "b", "c"}, | ||
exclude: []string{"a", "b"}, | ||
out: []string{"c"}, | ||
}, | ||
{ | ||
in: []string{"a", "b", "c"}, | ||
exclude: []string{"a", "d"}, | ||
out: []string{"b", "c"}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
name := fmt.Sprintf("{%s}-{%s}", | ||
strings.Join(tc.in, ","), | ||
strings.Join(tc.exclude, ",")) | ||
|
||
t.Run(name, func(t *testing.T) { | ||
got := exclude(tc.in, tc.exclude) | ||
if !slices.Equal(got, tc.out) { | ||
t.Errorf("Got slice %v, want %v.", got, tc.out) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package devbox | ||
|
||
func unique(s []string) []string { | ||
deduped := make([]string, 0, len(s)) | ||
seen := make(map[string]bool, len(s)) | ||
for _, str := range s { | ||
if !seen[str] { | ||
deduped = append(deduped, str) | ||
} | ||
seen[str] = true | ||
} | ||
return deduped | ||
} | ||
|
||
func exclude(s []string, elems []string) []string { | ||
excluded := make(map[string]bool, len(elems)) | ||
for _, ex := range elems { | ||
excluded[ex] = true | ||
} | ||
|
||
filtered := make([]string, 0, len(s)) | ||
for _, str := range s { | ||
if !excluded[str] { | ||
filtered = append(filtered, str) | ||
} | ||
} | ||
return filtered | ||
} |