github.com/actions-on-google/gactions@v3.2.0+incompatible/api/yamlutils_test.go (about) 1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yamlutils 16 17 import ( 18 "testing" 19 20 "github.com/protolambda/messagediff" 21 "gopkg.in/yaml.v2" 22 ) 23 24 func TestUnmarshalYamlToJSONRecover(t *testing.T) { 25 originalUnmarshal := unmarshal 26 unmarshal = func([]byte, interface{}) error { 27 panic("This is a panic") 28 } 29 defer func() { unmarshal = originalUnmarshal }() 30 got, err := UnmarshalYAMLToMap([]byte("ignored_yaml")) 31 if got != nil { 32 t.Errorf("got %v, want err", got) 33 } 34 if "panic caught: invalid yaml file" != err.Error() { 35 t.Errorf("got %v, want \"invalid yaml file\"", err) 36 } 37 } 38 39 func TestUnmarshalYamlToMap(t *testing.T) { 40 in, err := yaml.Marshal(map[string]interface{}{ 41 "snake_case": 3, 42 "foo_bar": map[string]interface{}{ 43 "the_answer": 42, 44 "foo": "remains the same", 45 }, 46 }) 47 if err != nil { 48 t.Fatalf("Can not parse map into yaml: %v", err) 49 } 50 got, err := UnmarshalYAMLToMap(in) 51 want := map[string]interface{}{ 52 "snake_case": 3, 53 "foo_bar": map[string]interface{}{ 54 "the_answer": 42, 55 "foo": "remains the same", 56 }, 57 } 58 if err != nil { 59 t.Errorf("UnmarshalYAMLToMap produced an error: got %v, want %v", err, nil) 60 } 61 diff, equal := messagediff.DeepDiff(want, got) 62 if !equal { 63 t.Errorf("UnmarshalYAMLToMap returned an incorrect value; diff (want -> got)\n%s", diff) 64 } 65 } 66 67 func TestDOSYAML(t *testing.T) { 68 dos := ` 69 a: &a ["lol","lol","lol","lol","lol","lol","lol","lol","lol"] 70 b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a] 71 c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b] 72 d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c] 73 e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d] 74 f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e] 75 g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f] 76 h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g] 77 i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h] 78 ` 79 if b, err := UnmarshalYAMLToMap([]byte(dos)); err == nil { 80 t.Errorf("DOS YAML successfully parsed into build %v.", b) 81 } 82 }