Skip to content

Commit

Permalink
Merge "[FAB-6930] Permit lists with brackets"
Browse files Browse the repository at this point in the history
  • Loading branch information
mastersingh24 authored and Gerrit Code Review committed Nov 10, 2017
2 parents 328d46c + b5285b5 commit 236ae62
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
5 changes: 5 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,11 @@ func NormalizeStringSlice(slice []string) []string {

if len(slice) > 0 {
for _, item := range slice {
// Remove surrounding brackets "[]" if specified
if strings.HasPrefix(item, "[") && strings.HasSuffix(item, "]") {
item = item[1 : len(item)-1]
}
// Split elements based on comma and add to normalized slice
if strings.Contains(item, ",") {
normalizedSlice = append(normalizedSlice, strings.Split(item, ",")...)
} else {
Expand Down
29 changes: 9 additions & 20 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,29 +514,18 @@ func TestStructToString(t *testing.T) {
assert.NotContains(t, idStr, "barpwd", "Identity password is not masked in the output")
}

// Test file list with multiple and single entries both with and without brackets
func TestNormalizeFileList(t *testing.T) {
var err error
slice := []string{"file1,file2", "file3,file4"}

slice, err = NormalizeFileList(slice, "../testdata")
slice := []string{"[file0,file1]", "file2,file3", "file4", "[file5]"}
slice, err := NormalizeFileList(slice, "../testdata")
if err != nil {
t.Error("Failed to normalize files list, error: ", err)
}

if !strings.Contains(slice[0], "file1") {
t.Error("Failed to correctly normalize files list")
}

if strings.Contains(slice[0], "file2") {
t.Error("Should have failed, first element should not contain 'file2'")
}

if !strings.Contains(slice[1], "file2") {
t.Error("Failed to correctly normalize files list")
t.Fatalf("Failed to normalize files list, error: %s", err)
}

if !strings.Contains(slice[3], "file4") {
t.Error("Failed to correctly normalize files list")
assert.Equal(t, 6, len(slice), "Invalid slice length")
for i := range slice {
if !strings.HasSuffix(slice[i], fmt.Sprintf("file%d", i)) {
t.Errorf("Failed to normalize files list for element %d; found '%s'", i, slice[i])
}
}
}

Expand Down

0 comments on commit 236ae62

Please sign in to comment.