Move isStringInSlice to common.IsStringInSlice

This commit is contained in:
Brian C. Lane 2021-04-19 11:44:53 -07:00 committed by Ondřej Budai
parent 1abdd9a1f7
commit b476078570
4 changed files with 24 additions and 14 deletions

View file

@ -16,6 +16,8 @@ import (
"strings"
"testing"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -606,7 +608,7 @@ func TestBumpBlueprintVersionV0(t *testing.T) {
// If the blueprint already exists it needs to be deleted to start from a known state
sorted := sort.StringSlice(list)
if isStringInSlice(sorted, "test-bump-blueprint-1-v0") {
if common.IsStringInSortedSlice(sorted, "test-bump-blueprint-1-v0") {
// Delete this blueprint if it already exists
resp, err := DeleteBlueprintV0(testState.socket, "test-bump-blueprint-1-v0")
require.NoError(t, err, "DELETE blueprint failed with a client error")

View file

@ -9,7 +9,6 @@ import (
"fmt"
"net"
"net/http"
"sort"
"strconv"
)
@ -21,17 +20,6 @@ type TestState struct {
imageTypeName string
}
// isStringInSlice returns true if the string is present, false if not
// slice must be sorted
// TODO decide if this belongs in a more widely useful package location
func isStringInSlice(slice []string, s string) bool {
i := sort.SearchStrings(slice, s)
if i < len(slice) && slice[i] == s {
return true
}
return false
}
func setUpTestState(socketPath string, imageTypeName string, unitTest bool) (*TestState, error) {
state := TestState{imageTypeName: imageTypeName, unitTest: unitTest}

View file

@ -1,6 +1,9 @@
package common
import "runtime"
import (
"runtime"
"sort"
)
var RuntimeGOARCH = runtime.GOARCH
@ -23,3 +26,13 @@ func PanicOnError(err error) {
panic(err)
}
}
// IsStringInSortedSlice returns true if the string is present, false if not
// slice must be sorted
func IsStringInSortedSlice(slice []string, s string) bool {
i := sort.SearchStrings(slice, s)
if i < len(slice) && slice[i] == s {
return true
}
return false
}

View file

@ -45,3 +45,10 @@ func TestPanicOnError(t *testing.T) {
err := errors.New("Error message")
assert.PanicsWithValue(t, err, func() { PanicOnError(err) })
}
func TestIsStringInSortedSlice(t *testing.T) {
assert.True(t, IsStringInSortedSlice([]string{"bart", "homer", "lisa", "marge"}, "homer"))
assert.False(t, IsStringInSortedSlice([]string{"bart", "lisa", "marge"}, "homer"))
assert.False(t, IsStringInSortedSlice([]string{"bart", "lisa", "marge"}, ""))
assert.False(t, IsStringInSortedSlice([]string{}, "homer"))
}