blob: dd9a7ed6d128a5ebba768c709b7e1ea88b349d9d [file] [log] [blame]
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001package android
2
3import (
4 "os"
5 "path/filepath"
6 "reflect"
Yu Liu8d82ac52022-05-17 15:13:28 -07007 "strings"
Liz Kammer8d62a4f2021-04-08 09:47:28 -04008 "testing"
Chris Parsonsf874e462022-05-10 13:50:12 -04009
10 "android/soong/bazel/cquery"
Liz Kammer8d62a4f2021-04-08 09:47:28 -040011)
12
Yu Liu8d82ac52022-05-17 15:13:28 -070013var testConfig = TestConfig("out", nil, "", nil)
14
Liz Kammer8d62a4f2021-04-08 09:47:28 -040015func TestRequestResultsAfterInvokeBazel(t *testing.T) {
16 label := "//foo:bar"
Liz Kammer0940b892022-03-18 15:55:04 -040017 cfg := configKey{"arm64_armv8-a", Android}
Liz Kammer8d62a4f2021-04-08 09:47:28 -040018 bazelContext, _ := testBazelContext(t, map[bazelCommand]string{
Liz Kammer0940b892022-03-18 15:55:04 -040019 bazelCommand{command: "cquery", expression: "deps(@soong_injection//mixed_builds:buildroot, 2)"}: `//foo:bar|arm64_armv8-a|android>>out/foo/bar.txt`,
Liz Kammer8d62a4f2021-04-08 09:47:28 -040020 })
Chris Parsonsf874e462022-05-10 13:50:12 -040021 bazelContext.QueueBazelRequest(label, cquery.GetOutputFiles, cfg)
Yu Liu8d82ac52022-05-17 15:13:28 -070022 err := bazelContext.InvokeBazel(testConfig)
Liz Kammer8d62a4f2021-04-08 09:47:28 -040023 if err != nil {
24 t.Fatalf("Did not expect error invoking Bazel, but got %s", err)
25 }
Chris Parsonsf874e462022-05-10 13:50:12 -040026 g, err := bazelContext.GetOutputFiles(label, cfg)
27 if err != nil {
28 t.Errorf("Expected cquery results after running InvokeBazel(), but got err %v", err)
Liz Kammer8d62a4f2021-04-08 09:47:28 -040029 } else if w := []string{"out/foo/bar.txt"}; !reflect.DeepEqual(w, g) {
30 t.Errorf("Expected output %s, got %s", w, g)
31 }
32}
33
34func TestInvokeBazelWritesBazelFiles(t *testing.T) {
35 bazelContext, baseDir := testBazelContext(t, map[bazelCommand]string{})
Yu Liu8d82ac52022-05-17 15:13:28 -070036 err := bazelContext.InvokeBazel(testConfig)
Liz Kammer8d62a4f2021-04-08 09:47:28 -040037 if err != nil {
38 t.Fatalf("Did not expect error invoking Bazel, but got %s", err)
39 }
Lukacs T. Berki3069dd92021-05-11 16:54:29 +020040 if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "mixed_builds", "main.bzl")); os.IsNotExist(err) {
Liz Kammer8d62a4f2021-04-08 09:47:28 -040041 t.Errorf("Expected main.bzl to exist, but it does not")
42 } else if err != nil {
43 t.Errorf("Unexpected error stating main.bzl %s", err)
44 }
45
Lukacs T. Berki3069dd92021-05-11 16:54:29 +020046 if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "mixed_builds", "BUILD.bazel")); os.IsNotExist(err) {
Liz Kammer8d62a4f2021-04-08 09:47:28 -040047 t.Errorf("Expected BUILD.bazel to exist, but it does not")
48 } else if err != nil {
49 t.Errorf("Unexpected error stating BUILD.bazel %s", err)
50 }
51
Liz Kammer286c9fa2021-04-21 08:46:34 -040052 if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "WORKSPACE.bazel")); os.IsNotExist(err) {
Liz Kammer8d62a4f2021-04-08 09:47:28 -040053 t.Errorf("Expected WORKSPACE.bazel to exist, but it does not")
54 } else if err != nil {
55 t.Errorf("Unexpected error stating WORKSPACE.bazel %s", err)
56 }
57}
58
59func TestInvokeBazelPopulatesBuildStatements(t *testing.T) {
60 bazelContext, _ := testBazelContext(t, map[bazelCommand]string{
Lukacs T. Berki3069dd92021-05-11 16:54:29 +020061 bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}: `
Liz Kammer8d62a4f2021-04-08 09:47:28 -040062{
63 "artifacts": [{
64 "id": 1,
65 "pathFragmentId": 1
66 }, {
67 "id": 2,
68 "pathFragmentId": 2
69 }],
70 "actions": [{
71 "targetId": 1,
72 "actionKey": "x",
73 "mnemonic": "x",
74 "arguments": ["touch", "foo"],
75 "inputDepSetIds": [1],
76 "outputIds": [1],
77 "primaryOutputId": 1
78 }],
79 "depSetOfFiles": [{
80 "id": 1,
81 "directArtifactIds": [1, 2]
82 }],
83 "pathFragments": [{
84 "id": 1,
85 "label": "one"
86 }, {
87 "id": 2,
88 "label": "two"
89 }]
90}`,
91 })
Yu Liu8d82ac52022-05-17 15:13:28 -070092 err := bazelContext.InvokeBazel(testConfig)
Liz Kammer8d62a4f2021-04-08 09:47:28 -040093 if err != nil {
94 t.Fatalf("Did not expect error invoking Bazel, but got %s", err)
95 }
96
97 got := bazelContext.BuildStatementsToRegister()
98 if want := 1; len(got) != want {
99 t.Errorf("Expected %d registered build statements, got %#v", want, got)
100 }
101}
102
Yu Liu8d82ac52022-05-17 15:13:28 -0700103func TestCoverageFlagsAfterInvokeBazel(t *testing.T) {
104 testConfig.productVariables.ClangCoverage = boolPtr(true)
105
106 testConfig.productVariables.NativeCoveragePaths = []string{"foo1", "foo2"}
107 testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1", "bar2"}
108 verifyExtraFlags(t, testConfig, `--collect_code_coverage --instrumentation_filter=+foo1,+foo2,-bar1,-bar2`)
109
110 testConfig.productVariables.NativeCoveragePaths = []string{"foo1"}
111 testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1"}
112 verifyExtraFlags(t, testConfig, `--collect_code_coverage --instrumentation_filter=+foo1,-bar1`)
113
114 testConfig.productVariables.NativeCoveragePaths = []string{"foo1"}
115 testConfig.productVariables.NativeCoverageExcludePaths = nil
116 verifyExtraFlags(t, testConfig, `--collect_code_coverage --instrumentation_filter=+foo1`)
117
118 testConfig.productVariables.NativeCoveragePaths = nil
119 testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1"}
120 verifyExtraFlags(t, testConfig, `--collect_code_coverage --instrumentation_filter=-bar1`)
121
122 testConfig.productVariables.ClangCoverage = boolPtr(false)
123 actual := verifyExtraFlags(t, testConfig, ``)
124 if strings.Contains(actual, "--collect_code_coverage") ||
125 strings.Contains(actual, "--instrumentation_filter=") {
126 t.Errorf("Expected code coverage disabled, but got %#v", actual)
127 }
128}
129
130func verifyExtraFlags(t *testing.T, config Config, expected string) string {
131 bazelContext, _ := testBazelContext(t, map[bazelCommand]string{})
132
133 err := bazelContext.InvokeBazel(config)
134 if err != nil {
135 t.Fatalf("Did not expect error invoking Bazel, but got %s", err)
136 }
137
138 flags := bazelContext.bazelRunner.(*mockBazelRunner).extraFlags
139 if expected := 3; len(flags) != expected {
140 t.Errorf("Expected %d extra flags got %#v", expected, flags)
141 }
142
143 actual := flags[1]
144 if !strings.Contains(actual, expected) {
145 t.Errorf("Expected %#v got %#v", expected, actual)
146 }
147
148 return actual
149}
150
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400151func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string) (*bazelContext, string) {
152 t.Helper()
153 p := bazelPaths{
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200154 soongOutDir: t.TempDir(),
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400155 outputBase: "outputbase",
156 workspaceDir: "workspace_dir",
157 }
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200158 aqueryCommand := bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400159 if _, exists := bazelCommandResults[aqueryCommand]; !exists {
160 bazelCommandResults[aqueryCommand] = "{}\n"
161 }
162 runner := &mockBazelRunner{bazelCommandResults: bazelCommandResults}
163 return &bazelContext{
164 bazelRunner: runner,
165 paths: &p,
166 requests: map[cqueryKey]bool{},
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200167 }, p.soongOutDir
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400168}