| 8 | ) |
| 9 | |
| 10 | func TestWalk(t *testing.T) { |
| 11 | |
| 12 | // Input yaml |
| 13 | input := ` |
| 14 | test: |
| 15 | image: appendtag |
| 16 | test: [] |
| 17 | test2: |
| 18 | image: dontreplaceme |
| 19 | test3: |
| 20 | - test4: |
| 21 | test5: |
| 22 | image: replaceme |
| 23 | ` |
| 24 | inputObj := make(map[string]interface{}) |
| 25 | err := yaml.Unmarshal([]byte(input), inputObj) |
| 26 | if err != nil { |
| 27 | t.Fatalf("Error parsing input: %v", err) |
| 28 | } |
| 29 | |
| 30 | match := MatchFn(func(key, value string) bool { |
| 31 | return key == "image" && value != "dontreplaceme" |
| 32 | }) |
| 33 | replace := ReplaceFn(func(_, value string) (interface{}, error) { |
| 34 | if value == "appendtag" { |
| 35 | return "appendtag:test", nil |
| 36 | } |
| 37 | |
| 38 | return "replaced", nil |
| 39 | }) |
| 40 | |
| 41 | _ = Walk(inputObj, match, replace) |
| 42 | |
| 43 | output, err := yaml.Marshal(inputObj) |
| 44 | if err != nil { |
| 45 | t.Fatalf("Error parsing output: %v", err) |
| 46 | } |
| 47 | |
| 48 | // Output yaml |
| 49 | expected := `test: |
| 50 | image: appendtag:test |
| 51 | test: [] |
| 52 | test2: |
| 53 | image: dontreplaceme |
| 54 | test3: |
| 55 | - test4: |
| 56 | image: replaced |
| 57 | test5: null |
| 58 | ` |
| 59 | assert.Equal(t, string(output), expected) |
| 60 | } |