1// Copyright 2015 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
15package android
16
17import (
18	"testing"
19)
20
21func TestSrcIsModule(t *testing.T) {
22	type args struct {
23		s string
24	}
25	tests := []struct {
26		name       string
27		args       args
28		wantModule string
29	}{
30		{
31			name: "file",
32			args: args{
33				s: "foo",
34			},
35			wantModule: "",
36		},
37		{
38			name: "module",
39			args: args{
40				s: ":foo",
41			},
42			wantModule: "foo",
43		},
44		{
45			name: "tag",
46			args: args{
47				s: ":foo{.bar}",
48			},
49			wantModule: "foo{.bar}",
50		},
51		{
52			name: "extra colon",
53			args: args{
54				s: ":foo:bar",
55			},
56			wantModule: "foo:bar",
57		},
58	}
59	for _, tt := range tests {
60		t.Run(tt.name, func(t *testing.T) {
61			if gotModule := SrcIsModule(tt.args.s); gotModule != tt.wantModule {
62				t.Errorf("SrcIsModule() = %v, want %v", gotModule, tt.wantModule)
63			}
64		})
65	}
66}
67
68func TestSrcIsModuleWithTag(t *testing.T) {
69	type args struct {
70		s string
71	}
72	tests := []struct {
73		name       string
74		args       args
75		wantModule string
76		wantTag    string
77	}{
78		{
79			name: "file",
80			args: args{
81				s: "foo",
82			},
83			wantModule: "",
84			wantTag:    "",
85		},
86		{
87			name: "module",
88			args: args{
89				s: ":foo",
90			},
91			wantModule: "foo",
92			wantTag:    "",
93		},
94		{
95			name: "tag",
96			args: args{
97				s: ":foo{.bar}",
98			},
99			wantModule: "foo",
100			wantTag:    ".bar",
101		},
102		{
103			name: "empty tag",
104			args: args{
105				s: ":foo{}",
106			},
107			wantModule: "foo",
108			wantTag:    "",
109		},
110		{
111			name: "extra colon",
112			args: args{
113				s: ":foo:bar",
114			},
115			wantModule: "foo:bar",
116		},
117		{
118			name: "invalid tag",
119			args: args{
120				s: ":foo{.bar",
121			},
122			wantModule: "foo{.bar",
123		},
124		{
125			name: "invalid tag 2",
126			args: args{
127				s: ":foo.bar}",
128			},
129			wantModule: "foo.bar}",
130		},
131	}
132	for _, tt := range tests {
133		t.Run(tt.name, func(t *testing.T) {
134			gotModule, gotTag := SrcIsModuleWithTag(tt.args.s)
135			if gotModule != tt.wantModule {
136				t.Errorf("SrcIsModuleWithTag() gotModule = %v, want %v", gotModule, tt.wantModule)
137			}
138			if gotTag != tt.wantTag {
139				t.Errorf("SrcIsModuleWithTag() gotTag = %v, want %v", gotTag, tt.wantTag)
140			}
141		})
142	}
143}
144
145type depsModule struct {
146	ModuleBase
147	props struct {
148		Deps []string
149	}
150}
151
152func (m *depsModule) GenerateAndroidBuildActions(ctx ModuleContext) {
153}
154
155func (m *depsModule) DepsMutator(ctx BottomUpMutatorContext) {
156	ctx.AddDependency(ctx.Module(), nil, m.props.Deps...)
157}
158
159func depsModuleFactory() Module {
160	m := &depsModule{}
161	m.AddProperties(&m.props)
162	InitAndroidModule(m)
163	return m
164}
165
166func TestErrorDependsOnDisabledModule(t *testing.T) {
167	ctx := NewTestContext()
168	ctx.RegisterModuleType("deps", depsModuleFactory)
169
170	bp := `
171		deps {
172			name: "foo",
173			deps: ["bar"],
174		}
175		deps {
176			name: "bar",
177			enabled: false,
178		}
179	`
180
181	config := TestConfig(buildDir, nil, bp, nil)
182
183	ctx.Register(config)
184
185	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
186	FailIfErrored(t, errs)
187	_, errs = ctx.PrepareBuildActions(config)
188	FailIfNoMatchingErrors(t, `module "foo": depends on disabled module "bar"`, errs)
189}
190