github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/appfile/load/loader_test.go (about) 1 package load 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 "reflect" 7 "testing" 8 9 "github.com/hashicorp/otto/app" 10 "github.com/hashicorp/otto/appfile" 11 "github.com/hashicorp/otto/appfile/detect" 12 ) 13 14 func TestLoader_basic(t *testing.T) { 15 cases := []struct { 16 Path string 17 Input, Expected *appfile.File 18 }{ 19 { 20 "basic", 21 &appfile.File{ 22 Application: &appfile.Application{ 23 Name: "foo", 24 }, 25 }, 26 &appfile.File{ 27 Application: &appfile.Application{ 28 Name: "foo", 29 }, 30 }, 31 }, 32 33 { 34 "detect", 35 &appfile.File{ 36 Application: &appfile.Application{ 37 Name: "foo", 38 Detect: true, 39 }, 40 }, 41 &appfile.File{ 42 Application: &appfile.Application{ 43 Name: "foo", 44 Type: "test", 45 Dependencies: []*appfile.Dependency{ 46 &appfile.Dependency{Source: "tubes"}, 47 }, 48 }, 49 }, 50 }, 51 52 { 53 "detect-no-appfile", 54 nil, 55 &appfile.File{ 56 Application: &appfile.Application{ 57 Type: "test", 58 Dependencies: []*appfile.Dependency{ 59 &appfile.Dependency{Source: "tubes"}, 60 }, 61 }, 62 }, 63 }, 64 65 { 66 "detect-false", 67 &appfile.File{ 68 Application: &appfile.Application{ 69 Name: "foo", 70 Detect: false, 71 }, 72 }, 73 &appfile.File{ 74 Application: &appfile.Application{ 75 Name: "foo", 76 Type: "test", 77 Detect: false, 78 }, 79 }, 80 }, 81 } 82 83 l, appMock := testLoader(t) 84 appMock.ImplicitResult = &appfile.File{ 85 Application: &appfile.Application{ 86 Dependencies: []*appfile.Dependency{ 87 &appfile.Dependency{Source: "tubes"}, 88 }, 89 }, 90 } 91 92 for _, tc := range cases { 93 tc.Path = testPath(tc.Path) 94 95 actual, err := l.Load(tc.Input, tc.Path) 96 if err != nil { 97 t.Fatalf("err: %s", err) 98 } 99 100 // Load the default and merge it 101 def, err := appfile.Default(tc.Path, nil) 102 if err != nil { 103 t.Fatalf("err %s: %s", tc.Path, err) 104 } 105 if err := def.Merge(tc.Expected); err != nil { 106 t.Fatalf("err %s: %s", tc.Path, err) 107 } 108 tc.Expected = def 109 110 if !reflect.DeepEqual(actual, tc.Expected) { 111 t.Fatalf("err: %s\n\n%#v", tc.Path, actual) 112 } 113 } 114 } 115 116 func testPath(path ...string) string { 117 args := make([]string, len(path)+1) 118 args[0] = "./test-fixtures" 119 copy(args[1:], path) 120 return filepath.Join(args...) 121 } 122 123 func testLoader(t *testing.T) (*Loader, *app.Mock) { 124 td, err := ioutil.TempDir("", "otto") 125 if err != nil { 126 t.Fatalf("err: %s", err) 127 } 128 129 compiler, err := appfile.NewCompiler(&appfile.CompileOpts{ 130 Dir: filepath.Join(td, "compile"), 131 }) 132 if err != nil { 133 t.Fatalf("err: %s", err) 134 } 135 136 // Create a single mock instance that is returned so we can verify 137 // calls and modify return values. 138 appMock := new(app.Mock) 139 140 return &Loader{ 141 Detector: &detect.Config{ 142 Detectors: []*detect.Detector{ 143 &detect.Detector{ 144 Type: "test", 145 File: []string{"test-file"}, 146 }, 147 }, 148 }, 149 150 Compiler: compiler, 151 152 Apps: map[app.Tuple]app.Factory{ 153 app.Tuple{"test", "*", "*"}: func() (app.App, error) { 154 return appMock, nil 155 }, 156 }, 157 }, appMock 158 }