xref: /aosp_15_r20/build/soong/golang/golang_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2024 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 golang
16
17import (
18	"android/soong/android"
19	"regexp"
20	"testing"
21
22	"github.com/google/blueprint/bootstrap"
23)
24
25func TestGolang(t *testing.T) {
26	bp := `
27		bootstrap_go_package {
28			name: "gopkg",
29			pkgPath: "test/pkg",
30		}
31
32		blueprint_go_binary {
33			name: "gobin",
34			deps: ["gopkg"],
35		}
36	`
37
38	result := android.GroupFixturePreparers(
39		android.PrepareForTestWithArchMutator,
40		android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
41			RegisterGoModuleTypes(ctx)
42			ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
43				ctx.BottomUpBlueprint("bootstrap_deps", bootstrap.BootstrapDeps).UsesReverseDependencies()
44			})
45		}),
46	).RunTestWithBp(t, bp)
47
48	bin := result.ModuleForTests("gobin", result.Config.BuildOSTarget.String())
49
50	expected := "^out/soong/host/" + result.Config.PrebuiltOS() + "/bin/go/gobin/?[^/]*/obj/gobin$"
51	actual := android.PathsRelativeToTop(bin.OutputFiles(result.TestContext, t, ""))
52	if len(actual) != 1 {
53		t.Fatalf("Expected 1 output file, got %v", actual)
54	}
55	if match, err := regexp.Match(expected, []byte(actual[0])); err != nil || !match {
56		t.Fatalf("Expected output file to match %q, but got %q", expected, actual[0])
57	}
58}
59