| 1 | package yamltransformer |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/DefangLabs/secret-detector/pkg/transformers/tests" |
| 7 | ) |
| 8 | |
| 9 | var testCases = []tests.TestCase{ |
| 10 | {"empty input", "", map[string]string{}}, |
| 11 | {"illegal structure - text", "this is not a yaml file", nil}, |
| 12 | {"illegal structure - xml", "<elem attr=val />", nil}, |
| 13 | {"illegal structure - ini", "key = value", nil}, |
| 14 | {"illegal structure - tab indentation", "key1:\n\t'a: value", nil}, |
| 15 | {"simple keys", |
| 16 | ` |
| 17 | key1: val1 |
| 18 | key2: "" |
| 19 | key3: `, |
| 20 | map[string]string{"key1": "val1", "key2": "", "key3": "<nil>"}}, |
| 21 | {"nesting keys", |
| 22 | ` |
| 23 | key1: |
| 24 | a: |
| 25 | a1: val1 |
| 26 | a2: val2 |
| 27 | b: val3 |
| 28 | key2: val4`, |
| 29 | map[string]string{"key1.a.a1": "val1", "key1.a.a2": "val2", "key1.b": "val3", "key2": "val4"}}, |
| 30 | {"special characters", |
| 31 | ` |
| 32 | s p a c e s: y e s |
| 33 | sla/sh: ye/s |
| 34 | d.o.t.s: y.e.s |
| 35 | sepa:rator: yes |
| 36 | multiline1: |
| 37 | hello |
| 38 | world! |
| 39 | multiline2: | |
| 40 | hello |
| 41 | world!`, |
| 42 | map[string]string{ |
| 43 | "s p a c e s": "y e s", |
| 44 | "sla/sh": "ye/s", |
| 45 | "d.o.t.s": "y.e.s", |
| 46 | "sepa:rator": "yes", |
| 47 | "multiline1": "hello world!", |
| 48 | "multiline2": "hello\nworld!", |
| 49 | }}, |
| 50 | {"yaml with CR/LF", |
| 51 | "key1: val1\r\nkey2:\r\n a: val2", |
| 52 | map[string]string{"key1": "val1", "key2.a": "val2"}}, |
| 53 | {"quote signs", |
| 54 | ` |
| 55 | "quotes": |
| 56 | 'single': 'ignored' |
| 57 | 'double': "ignored" |
| 58 | `, |
| 59 | map[string]string{"quotes.single": "ignored", "quotes.double": "ignored"}}, |
| 60 | {"arrays", |
| 61 | ` |
| 62 | arrInLine: [a,b,c] |
| 63 | arr1: |
| 64 | - 100 |
| 65 | - 200 |
| 66 | arr2: |
| 67 | - k1: v1 |
| 68 | k2: v2 |
| 69 | - k3: v3 |
| 70 | - k4: |
| 71 | k5: v5 |
| 72 | - v6 |
| 73 | `, |
| 74 | map[string]string{ |
| 75 | "arrInLine[0]": "a", |
| 76 | "arrInLine[1]": "b", |
| 77 | "arrInLine[2]": "c", |
| 78 | "arr1[0]": "100", |
| 79 | "arr1[1]": "200", |
| 80 | "arr2[0].k1": "v1", |
| 81 | "arr2[0].k2": "v2", |
| 82 | "arr2[1].k3": "v3", |
| 83 | "arr2[2].k4.k5": "v5", |
| 84 | "arr2[3]": "v6", |
| 85 | }}, |
| 86 | } |
| 87 | |
| 88 | var yamlTransformer = NewTransformer() |
| 89 | |
| 90 | func TestTransform(t *testing.T) { |
| 91 | tests.TestTransform(t, yamlTransformer, testCases) |
| 92 | } |