Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 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 | // http://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 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
Yuchen Wu | f995846 | 2015-10-09 17:31:27 -0700 | [diff] [blame] | 22 | type Walker interface { |
| 23 | Walk() bool |
| 24 | } |
| 25 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 26 | type fooModule struct { |
| 27 | properties struct { |
| 28 | Foo string |
| 29 | } |
| 30 | } |
| 31 | |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 32 | func newFooModule() (Module, []interface{}) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 33 | m := &fooModule{} |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 34 | return m, []interface{}{&m.properties} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func (f *fooModule) GenerateBuildActions(ModuleContext) { |
| 38 | } |
| 39 | |
| 40 | func (f *fooModule) Foo() string { |
| 41 | return f.properties.Foo |
| 42 | } |
| 43 | |
Yuchen Wu | f995846 | 2015-10-09 17:31:27 -0700 | [diff] [blame] | 44 | func (f *fooModule) Walk() bool { |
| 45 | return true |
| 46 | } |
| 47 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 48 | type barModule struct { |
| 49 | properties struct { |
| 50 | Bar bool |
| 51 | } |
| 52 | } |
| 53 | |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 54 | func newBarModule() (Module, []interface{}) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 55 | m := &barModule{} |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 56 | return m, []interface{}{&m.properties} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func (b *barModule) GenerateBuildActions(ModuleContext) { |
| 60 | } |
| 61 | |
| 62 | func (b *barModule) Bar() bool { |
| 63 | return b.properties.Bar |
| 64 | } |
| 65 | |
Yuchen Wu | f995846 | 2015-10-09 17:31:27 -0700 | [diff] [blame] | 66 | func (b *barModule) Walk() bool { |
| 67 | return false |
| 68 | } |
| 69 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 70 | func TestContextParse(t *testing.T) { |
| 71 | ctx := NewContext() |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 72 | ctx.RegisterModuleType("foo_module", newFooModule) |
| 73 | ctx.RegisterModuleType("bar_module", newBarModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 74 | |
| 75 | r := bytes.NewBufferString(` |
| 76 | foo_module { |
| 77 | name: "MyFooModule", |
| 78 | deps: ["MyBarModule"], |
| 79 | } |
| 80 | |
| 81 | bar_module { |
| 82 | name: "MyBarModule", |
| 83 | } |
| 84 | `) |
| 85 | |
Colin Cross | 1fef536 | 2015-04-20 16:50:54 -0700 | [diff] [blame] | 86 | _, _, _, errs := ctx.parse(".", "Blueprint", r, nil) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 87 | if len(errs) > 0 { |
| 88 | t.Errorf("unexpected parse errors:") |
| 89 | for _, err := range errs { |
| 90 | t.Errorf(" %s", err) |
| 91 | } |
| 92 | t.FailNow() |
| 93 | } |
| 94 | |
Colin Cross | 763b6f1 | 2015-10-29 15:32:56 -0700 | [diff] [blame] | 95 | errs = ctx.ResolveDependencies(nil) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 96 | if len(errs) > 0 { |
| 97 | t.Errorf("unexpected dep errors:") |
| 98 | for _, err := range errs { |
| 99 | t.Errorf(" %s", err) |
| 100 | } |
| 101 | t.FailNow() |
| 102 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 103 | } |
Yuchen Wu | f995846 | 2015-10-09 17:31:27 -0700 | [diff] [blame] | 104 | |
| 105 | // |---B===D - represents a non-walkable edge |
| 106 | // A = represents a walkable edge |
| 107 | // |===C---E===G |
| 108 | // | | A should not be visited because it's the root node. |
| 109 | // |===F===| B, D and E should not be walked. |
| 110 | func TestWalkDeps(t *testing.T) { |
| 111 | ctx := NewContext() |
Colin Cross | d7b0f60 | 2016-06-02 15:30:20 -0700 | [diff] [blame^] | 112 | ctx.MockFileSystem(map[string][]byte{ |
| 113 | "Blueprints": []byte(` |
| 114 | foo_module { |
| 115 | name: "A", |
| 116 | deps: ["B", "C"], |
| 117 | } |
| 118 | |
| 119 | bar_module { |
| 120 | name: "B", |
| 121 | deps: ["D"], |
| 122 | } |
| 123 | |
| 124 | foo_module { |
| 125 | name: "C", |
| 126 | deps: ["E", "F"], |
| 127 | } |
| 128 | |
| 129 | foo_module { |
| 130 | name: "D", |
| 131 | } |
| 132 | |
| 133 | bar_module { |
| 134 | name: "E", |
| 135 | deps: ["G"], |
| 136 | } |
| 137 | |
| 138 | foo_module { |
| 139 | name: "F", |
| 140 | deps: ["G"], |
| 141 | } |
| 142 | |
| 143 | foo_module { |
| 144 | name: "G", |
| 145 | } |
| 146 | `), |
| 147 | }) |
| 148 | |
Yuchen Wu | f995846 | 2015-10-09 17:31:27 -0700 | [diff] [blame] | 149 | ctx.RegisterModuleType("foo_module", newFooModule) |
| 150 | ctx.RegisterModuleType("bar_module", newBarModule) |
Colin Cross | d7b0f60 | 2016-06-02 15:30:20 -0700 | [diff] [blame^] | 151 | _, errs := ctx.ParseBlueprintsFiles("Blueprints") |
| 152 | if len(errs) > 0 { |
| 153 | t.Errorf("unexpected parse errors:") |
| 154 | for _, err := range errs { |
| 155 | t.Errorf(" %s", err) |
| 156 | } |
| 157 | t.FailNow() |
| 158 | } |
| 159 | |
| 160 | errs = ctx.ResolveDependencies(nil) |
| 161 | if len(errs) > 0 { |
| 162 | t.Errorf("unexpected dep errors:") |
| 163 | for _, err := range errs { |
| 164 | t.Errorf(" %s", err) |
| 165 | } |
| 166 | t.FailNow() |
| 167 | } |
Yuchen Wu | f995846 | 2015-10-09 17:31:27 -0700 | [diff] [blame] | 168 | |
| 169 | var output string |
| 170 | topModule := ctx.moduleGroups["A"].modules[0] |
| 171 | ctx.walkDeps(topModule, |
| 172 | func(module, parent Module) bool { |
| 173 | if module.(Walker).Walk() { |
| 174 | output += ctx.ModuleName(module) |
| 175 | return true |
| 176 | } |
| 177 | return false |
| 178 | }) |
| 179 | if output != "CFG" { |
| 180 | t.Fatalf("unexpected walkDeps behaviour: %s\nshould be: CFG", output) |
| 181 | } |
| 182 | } |