| 1 |
// Copyright 2020 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package generators_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
. "sigs.k8s.io/kustomize/api/internal/generators" |
| 11 |
) |
| 12 |
|
| 13 |
func TestParseFileSource(t *testing.T) { |
| 14 |
tests := map[string]*struct { |
| 15 |
Input string |
| 16 |
Error string |
| 17 |
Key string |
| 18 |
Filename string |
| 19 |
}{ |
| 20 |
"filename only": { |
| 21 |
Input: "./path/myfile", |
| 22 |
Key: "myfile", |
| 23 |
Filename: "./path/myfile", |
| 24 |
}, |
| 25 |
"key and filename": { |
| 26 |
Input: "newName.ini=oldName", |
| 27 |
Key: "newName.ini", |
| 28 |
Filename: "oldName", |
| 29 |
}, |
| 30 |
"multiple =": { |
| 31 |
Input: "newName.ini==oldName", |
| 32 |
Error: `source "newName.ini==oldName" key name or file path contains '='`, |
| 33 |
}, |
| 34 |
"missing key": { |
| 35 |
Input: "=myfile", |
| 36 |
Error: `missing key name for file path "myfile" in source "=myfile"`, |
| 37 |
}, |
| 38 |
} |
| 39 |
for name, test := range tests { |
| 40 |
t.Run(name, func(t *testing.T) { |
| 41 |
key, file, err := ParseFileSource(test.Input) |
| 42 |
if test.Error != "" { |
| 43 |
require.EqualError(t, err, test.Error) |
| 44 |
} else { |
| 45 |
require.NoError(t, err) |
| 46 |
require.Equal(t, test.Key, key) |
| 47 |
require.Equal(t, test.Filename, file) |
| 48 |
} |
| 49 |
}) |
| 50 |
} |
| 51 |
} |