From b476078570db9d656744497afce04e5f8fa8b1b8 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Mon, 19 Apr 2021 11:44:53 -0700 Subject: [PATCH] Move isStringInSlice to common.IsStringInSlice --- internal/client/blueprints_test.go | 4 +++- internal/client/utils.go | 12 ------------ internal/common/helpers.go | 15 ++++++++++++++- internal/common/helpers_test.go | 7 +++++++ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/internal/client/blueprints_test.go b/internal/client/blueprints_test.go index 67c635b0d..75e795916 100644 --- a/internal/client/blueprints_test.go +++ b/internal/client/blueprints_test.go @@ -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") diff --git a/internal/client/utils.go b/internal/client/utils.go index 6b0eccc4f..ed0ad83ac 100644 --- a/internal/client/utils.go +++ b/internal/client/utils.go @@ -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} diff --git a/internal/common/helpers.go b/internal/common/helpers.go index 1c05aa9f1..04e8a291a 100644 --- a/internal/common/helpers.go +++ b/internal/common/helpers.go @@ -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 +} diff --git a/internal/common/helpers_test.go b/internal/common/helpers_test.go index 2ec687676..467093221 100644 --- a/internal/common/helpers_test.go +++ b/internal/common/helpers_test.go @@ -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")) +}