github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/appfile/parse_test.go (about) 1 package appfile 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "testing" 7 ) 8 9 func TestParse(t *testing.T) { 10 cases := []struct { 11 File string 12 Result *File 13 Err bool 14 }{ 15 { 16 "basic.hcl", 17 &File{ 18 Application: &Application{ 19 Name: "foo", 20 Detect: true, 21 Dependencies: []*Dependency{ 22 &Dependency{ 23 Source: "foo", 24 }, 25 &Dependency{ 26 Source: "bar", 27 }, 28 }, 29 }, 30 Project: &Project{ 31 Name: "foo", 32 Infrastructure: "aws", 33 }, 34 Infrastructure: []*Infrastructure{ 35 &Infrastructure{ 36 Name: "aws", 37 Type: "aws", 38 Flavor: "foo", 39 }, 40 }, 41 }, 42 false, 43 }, 44 45 // Applications 46 { 47 "multi-app.hcl", 48 nil, 49 true, 50 }, 51 52 { 53 "app-no-detect.hcl", 54 &File{ 55 Application: &Application{ 56 Name: "foo", 57 Detect: false, 58 }, 59 }, 60 false, 61 }, 62 63 // Customizations 64 { 65 "basic-custom.hcl", 66 &File{ 67 Customization: &CustomizationSet{ 68 Raw: []*Customization{ 69 &Customization{ 70 Type: "dev", 71 Config: map[string]interface{}{ 72 "go_version": "1.5", 73 }, 74 }, 75 }, 76 }, 77 }, 78 false, 79 }, 80 81 { 82 "basic-custom-no-name.hcl", 83 &File{ 84 Customization: &CustomizationSet{ 85 Raw: []*Customization{ 86 &Customization{ 87 Type: "app", 88 Config: map[string]interface{}{ 89 "go_version": "1.5", 90 }, 91 }, 92 }, 93 }, 94 }, 95 false, 96 }, 97 98 { 99 "basic-custom-case.hcl", 100 &File{ 101 Customization: &CustomizationSet{ 102 Raw: []*Customization{ 103 &Customization{ 104 Type: "dev", 105 Config: map[string]interface{}{ 106 "go_version": "1.5", 107 }, 108 }, 109 }, 110 }, 111 }, 112 false, 113 }, 114 115 // Infrastructures 116 { 117 "infra-dup.hcl", 118 nil, 119 true, 120 }, 121 122 { 123 "infra-foundations.hcl", 124 &File{ 125 Application: &Application{ 126 Name: "foo", 127 Detect: true, 128 }, 129 Infrastructure: []*Infrastructure{ 130 &Infrastructure{ 131 Name: "aws", 132 Type: "aws", 133 Flavor: "foo", 134 Foundations: []*Foundation{ 135 &Foundation{ 136 Name: "consul", 137 Config: map[string]interface{}{ 138 "foo": "bar", 139 }, 140 }, 141 }, 142 }, 143 }, 144 }, 145 false, 146 }, 147 148 { 149 "infra-foundations-dup.hcl", 150 nil, 151 true, 152 }, 153 154 // Imports 155 156 { 157 "imports.hcl", 158 &File{ 159 Application: &Application{ 160 Name: "otto", 161 Type: "go", 162 Detect: true, 163 }, 164 Imports: []*Import{ 165 &Import{ 166 Source: "./foo", 167 }, 168 }, 169 }, 170 false, 171 }, 172 173 // Unknown keys 174 { 175 "unknown-keys.hcl", 176 nil, 177 true, 178 }, 179 } 180 181 for _, tc := range cases { 182 path, err := filepath.Abs(filepath.Join("./test-fixtures", tc.File)) 183 if err != nil { 184 t.Fatalf("file: %s\n\n%s", tc.File, err) 185 continue 186 } 187 188 actual, err := ParseFile(path) 189 if (err != nil) != tc.Err { 190 t.Fatalf("file: %s\n\n%s", tc.File, err) 191 continue 192 } 193 194 if actual != nil { 195 if actual.Path != path { 196 t.Fatalf("file: %s\n\n%s", tc.File, actual.Path) 197 } 198 actual.Path = "" 199 } 200 201 if !reflect.DeepEqual(actual, tc.Result) { 202 t.Fatalf("file: %s\n\n%#v\n\n%#v", tc.File, actual, tc.Result) 203 } 204 } 205 }