blob: 8877be9c5e31d880544180a189984c0bcc879af1 [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// 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 Gennis1bc967e2014-05-27 16:34:41 -070015package blueprint
16
17import (
18 "bytes"
19 "testing"
20)
21
Yuchen Wuf9958462015-10-09 17:31:27 -070022type Walker interface {
23 Walk() bool
24}
25
Jamie Gennis1bc967e2014-05-27 16:34:41 -070026type fooModule struct {
27 properties struct {
28 Foo string
29 }
30}
31
Jamie Gennis68540da2014-10-06 09:10:40 -070032func newFooModule() (Module, []interface{}) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070033 m := &fooModule{}
Jamie Gennis68540da2014-10-06 09:10:40 -070034 return m, []interface{}{&m.properties}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070035}
36
37func (f *fooModule) GenerateBuildActions(ModuleContext) {
38}
39
40func (f *fooModule) Foo() string {
41 return f.properties.Foo
42}
43
Yuchen Wuf9958462015-10-09 17:31:27 -070044func (f *fooModule) Walk() bool {
45 return true
46}
47
Jamie Gennis1bc967e2014-05-27 16:34:41 -070048type barModule struct {
49 properties struct {
50 Bar bool
51 }
52}
53
Jamie Gennis68540da2014-10-06 09:10:40 -070054func newBarModule() (Module, []interface{}) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070055 m := &barModule{}
Jamie Gennis68540da2014-10-06 09:10:40 -070056 return m, []interface{}{&m.properties}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070057}
58
59func (b *barModule) GenerateBuildActions(ModuleContext) {
60}
61
62func (b *barModule) Bar() bool {
63 return b.properties.Bar
64}
65
Yuchen Wuf9958462015-10-09 17:31:27 -070066func (b *barModule) Walk() bool {
67 return false
68}
69
Jamie Gennis1bc967e2014-05-27 16:34:41 -070070func TestContextParse(t *testing.T) {
71 ctx := NewContext()
Jamie Gennis68540da2014-10-06 09:10:40 -070072 ctx.RegisterModuleType("foo_module", newFooModule)
73 ctx.RegisterModuleType("bar_module", newBarModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070074
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 Cross1fef5362015-04-20 16:50:54 -070086 _, _, _, errs := ctx.parse(".", "Blueprint", r, nil)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070087 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
Jamie Gennis68540da2014-10-06 09:10:40 -070095 errs = ctx.resolveDependencies(nil)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070096 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 }
103
Colin Cross691a60d2015-01-07 18:08:56 -0800104 errs = ctx.updateDependencies()
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700105 if len(errs) > 0 {
106 t.Errorf("unexpected dep cycle errors:")
107 for _, err := range errs {
108 t.Errorf(" %s", err)
109 }
110 t.FailNow()
111 }
112
113}
Yuchen Wuf9958462015-10-09 17:31:27 -0700114
115// |---B===D - represents a non-walkable edge
116// A = represents a walkable edge
117// |===C---E===G
118// | | A should not be visited because it's the root node.
119// |===F===| B, D and E should not be walked.
120func TestWalkDeps(t *testing.T) {
121 ctx := NewContext()
122 ctx.RegisterModuleType("foo_module", newFooModule)
123 ctx.RegisterModuleType("bar_module", newBarModule)
124 ctx.ParseBlueprintsFiles("context_test_Blueprints")
125 ctx.ResolveDependencies(nil)
126
127 var output string
128 topModule := ctx.moduleGroups["A"].modules[0]
129 ctx.walkDeps(topModule,
130 func(module, parent Module) bool {
131 if module.(Walker).Walk() {
132 output += ctx.ModuleName(module)
133 return true
134 }
135 return false
136 })
137 if output != "CFG" {
138 t.Fatalf("unexpected walkDeps behaviour: %s\nshould be: CFG", output)
139 }
140}