Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 4 | "encoding/json" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 5 | "os" |
| 6 | "path/filepath" |
| 7 | "reflect" |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 8 | "strings" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 9 | "testing" |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 10 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 11 | "android/soong/bazel" |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 12 | "android/soong/bazel/cquery" |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 13 | analysis_v2_proto "prebuilts/bazel/common/proto/analysis_v2" |
Jingwen Chen | f3b1ec3 | 2022-11-07 15:02:48 +0000 | [diff] [blame] | 14 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 15 | "github.com/google/blueprint/metrics" |
Jingwen Chen | f3b1ec3 | 2022-11-07 15:02:48 +0000 | [diff] [blame] | 16 | "google.golang.org/protobuf/proto" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 17 | ) |
| 18 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 19 | var testConfig = TestConfig("out", nil, "", nil) |
| 20 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 21 | type testInvokeBazelContext struct{} |
| 22 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 23 | type mockBazelRunner struct { |
| 24 | testHelper *testing.T |
| 25 | // Stores mock behavior. If an issueBazelCommand request is made for command |
| 26 | // k, and {k:v} is present in this map, then the mock will return v. |
| 27 | bazelCommandResults map[bazelCommand]string |
| 28 | // Requests actually made of the mockBazelRunner with issueBazelCommand, |
| 29 | // keyed by the command they represent. |
| 30 | bazelCommandRequests map[bazelCommand]bazel.CmdRequest |
| 31 | } |
| 32 | |
| 33 | func (r *mockBazelRunner) bazelCommandForRequest(cmdRequest bazel.CmdRequest) bazelCommand { |
| 34 | for _, arg := range cmdRequest.Argv { |
| 35 | for _, cmdType := range allBazelCommands { |
| 36 | if arg == cmdType.command { |
| 37 | return cmdType |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | r.testHelper.Fatalf("Unrecognized bazel request: %s", cmdRequest) |
| 42 | return cqueryCmd |
| 43 | } |
| 44 | |
| 45 | func (r *mockBazelRunner) issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (string, string, error) { |
| 46 | command := r.bazelCommandForRequest(cmdRequest) |
| 47 | r.bazelCommandRequests[command] = cmdRequest |
| 48 | return r.bazelCommandResults[command], "", nil |
| 49 | } |
| 50 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 51 | func (t *testInvokeBazelContext) GetEventHandler() *metrics.EventHandler { |
| 52 | return &metrics.EventHandler{} |
| 53 | } |
| 54 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 55 | func TestRequestResultsAfterInvokeBazel(t *testing.T) { |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 56 | label_foo := "@//foo:foo" |
| 57 | label_bar := "@//foo:bar" |
| 58 | apexKey := ApexConfigKey{ |
| 59 | WithinApex: true, |
| 60 | ApexSdkVersion: "29", |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 61 | ApiDomain: "myapex", |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 62 | } |
| 63 | cfg_foo := configKey{"arm64_armv8-a", Android, apexKey} |
| 64 | cfg_bar := configKey{arch: "arm64_armv8-a", osType: Android} |
| 65 | cmd_results := []string{ |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 66 | `@//foo:foo|arm64_armv8-a|android|within_apex|29|myapex>>out/foo/foo.txt`, |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 67 | `@//foo:bar|arm64_armv8-a|android>>out/foo/bar.txt`, |
| 68 | } |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 69 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{cqueryCmd: strings.Join(cmd_results, "\n")}) |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 70 | |
| 71 | bazelContext.QueueBazelRequest(label_foo, cquery.GetOutputFiles, cfg_foo) |
| 72 | bazelContext.QueueBazelRequest(label_bar, cquery.GetOutputFiles, cfg_bar) |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 73 | err := bazelContext.InvokeBazel(testConfig, &testInvokeBazelContext{}) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 74 | if err != nil { |
| 75 | t.Fatalf("Did not expect error invoking Bazel, but got %s", err) |
| 76 | } |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 77 | verifyCqueryResult(t, bazelContext, label_foo, cfg_foo, "out/foo/foo.txt") |
| 78 | verifyCqueryResult(t, bazelContext, label_bar, cfg_bar, "out/foo/bar.txt") |
| 79 | } |
| 80 | |
| 81 | func verifyCqueryResult(t *testing.T, ctx *mixedBuildBazelContext, label string, cfg configKey, result string) { |
| 82 | g, err := ctx.GetOutputFiles(label, cfg) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 83 | if err != nil { |
| 84 | t.Errorf("Expected cquery results after running InvokeBazel(), but got err %v", err) |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 85 | } else if w := []string{result}; !reflect.DeepEqual(w, g) { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 86 | t.Errorf("Expected output %s, got %s", w, g) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestInvokeBazelWritesBazelFiles(t *testing.T) { |
| 91 | bazelContext, baseDir := testBazelContext(t, map[bazelCommand]string{}) |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 92 | err := bazelContext.InvokeBazel(testConfig, &testInvokeBazelContext{}) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 93 | if err != nil { |
| 94 | t.Fatalf("Did not expect error invoking Bazel, but got %s", err) |
| 95 | } |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 96 | if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "mixed_builds", "main.bzl")); os.IsNotExist(err) { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 97 | t.Errorf("Expected main.bzl to exist, but it does not") |
| 98 | } else if err != nil { |
| 99 | t.Errorf("Unexpected error stating main.bzl %s", err) |
| 100 | } |
| 101 | |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 102 | if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "mixed_builds", "BUILD.bazel")); os.IsNotExist(err) { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 103 | t.Errorf("Expected BUILD.bazel to exist, but it does not") |
| 104 | } else if err != nil { |
| 105 | t.Errorf("Unexpected error stating BUILD.bazel %s", err) |
| 106 | } |
| 107 | |
Liz Kammer | 286c9fa | 2021-04-21 08:46:34 -0400 | [diff] [blame] | 108 | if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "WORKSPACE.bazel")); os.IsNotExist(err) { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 109 | t.Errorf("Expected WORKSPACE.bazel to exist, but it does not") |
| 110 | } else if err != nil { |
| 111 | t.Errorf("Unexpected error stating WORKSPACE.bazel %s", err) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestInvokeBazelPopulatesBuildStatements(t *testing.T) { |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 116 | type testCase struct { |
| 117 | input string |
| 118 | command string |
| 119 | } |
| 120 | |
| 121 | var testCases = []testCase{ |
| 122 | {` |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 123 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 124 | "artifacts": [ |
| 125 | { "id": 1, "path_fragment_id": 1 }, |
| 126 | { "id": 2, "path_fragment_id": 2 }], |
| 127 | "actions": [{ |
| 128 | "target_Id": 1, |
| 129 | "action_Key": "x", |
| 130 | "mnemonic": "x", |
| 131 | "arguments": ["touch", "foo"], |
| 132 | "input_dep_set_ids": [1], |
| 133 | "output_Ids": [1], |
| 134 | "primary_output_id": 1 |
| 135 | }], |
| 136 | "dep_set_of_files": [ |
| 137 | { "id": 1, "direct_artifact_ids": [1, 2] }], |
| 138 | "path_fragments": [ |
| 139 | { "id": 1, "label": "one" }, |
| 140 | { "id": 2, "label": "two" }] |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 141 | }`, |
Jingwen Chen | f3b1ec3 | 2022-11-07 15:02:48 +0000 | [diff] [blame] | 142 | "cd 'test/exec_root' && rm -rf 'one' && touch foo", |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 143 | }, {` |
| 144 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 145 | "artifacts": [ |
| 146 | { "id": 1, "path_fragment_id": 10 }, |
| 147 | { "id": 2, "path_fragment_id": 20 }], |
| 148 | "actions": [{ |
| 149 | "target_Id": 100, |
| 150 | "action_Key": "x", |
| 151 | "mnemonic": "x", |
| 152 | "arguments": ["bogus", "command"], |
| 153 | "output_Ids": [1, 2], |
| 154 | "primary_output_id": 1 |
| 155 | }], |
| 156 | "path_fragments": [ |
| 157 | { "id": 10, "label": "one", "parent_id": 30 }, |
| 158 | { "id": 20, "label": "one.d", "parent_id": 30 }, |
| 159 | { "id": 30, "label": "parent" }] |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 160 | }`, |
Jingwen Chen | f3b1ec3 | 2022-11-07 15:02:48 +0000 | [diff] [blame] | 161 | `cd 'test/exec_root' && rm -rf 'parent/one' && bogus command && sed -i'' -E 's@(^|\s|")bazel-out/@\1test/bazel_out/@g' 'parent/one.d'`, |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 162 | }, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 165 | for i, testCase := range testCases { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 166 | data, err := JsonToActionGraphContainer(testCase.input) |
| 167 | if err != nil { |
| 168 | t.Error(err) |
| 169 | } |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 170 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{aqueryCmd: string(data)}) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 171 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 172 | err = bazelContext.InvokeBazel(testConfig, &testInvokeBazelContext{}) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 173 | if err != nil { |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 174 | t.Fatalf("testCase #%d: did not expect error invoking Bazel, but got %s", i+1, err) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | got := bazelContext.BuildStatementsToRegister() |
| 178 | if want := 1; len(got) != want { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 179 | t.Fatalf("expected %d registered build statements, but got %#v", want, got) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | cmd := RuleBuilderCommand{} |
Colin Cross | d5c7ddb | 2022-12-06 16:27:17 -0800 | [diff] [blame] | 183 | ctx := builderContextForTests{PathContextForTesting(TestConfig("out", nil, "", nil))} |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 184 | createCommand(&cmd, got[0], "test/exec_root", "test/bazel_out", ctx, map[string]bazel.AqueryDepset{}, "") |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 185 | if actual, expected := cmd.buf.String(), testCase.command; expected != actual { |
| 186 | t.Errorf("expected: [%s], actual: [%s]", expected, actual) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 187 | } |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 191 | func TestMixedBuildSandboxedAction(t *testing.T) { |
| 192 | input := `{ |
| 193 | "artifacts": [ |
| 194 | { "id": 1, "path_fragment_id": 1 }, |
| 195 | { "id": 2, "path_fragment_id": 2 }], |
| 196 | "actions": [{ |
| 197 | "target_Id": 1, |
| 198 | "action_Key": "x", |
| 199 | "mnemonic": "x", |
| 200 | "arguments": ["touch", "foo"], |
| 201 | "input_dep_set_ids": [1], |
| 202 | "output_Ids": [1], |
| 203 | "primary_output_id": 1 |
| 204 | }], |
| 205 | "dep_set_of_files": [ |
| 206 | { "id": 1, "direct_artifact_ids": [1, 2] }], |
| 207 | "path_fragments": [ |
| 208 | { "id": 1, "label": "one" }, |
| 209 | { "id": 2, "label": "two" }] |
| 210 | }` |
| 211 | data, err := JsonToActionGraphContainer(input) |
| 212 | if err != nil { |
| 213 | t.Error(err) |
| 214 | } |
| 215 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{aqueryCmd: string(data)}) |
| 216 | |
| 217 | err = bazelContext.InvokeBazel(testConfig, &testInvokeBazelContext{}) |
| 218 | if err != nil { |
| 219 | t.Fatalf("TestMixedBuildSandboxedAction did not expect error invoking Bazel, but got %s", err) |
| 220 | } |
| 221 | |
| 222 | statement := bazelContext.BuildStatementsToRegister()[0] |
| 223 | statement.ShouldRunInSbox = true |
| 224 | |
| 225 | cmd := RuleBuilderCommand{} |
| 226 | ctx := builderContextForTests{PathContextForTesting(TestConfig("out", nil, "", nil))} |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 227 | createCommand(&cmd, statement, "test/exec_root", "test/bazel_out", ctx, map[string]bazel.AqueryDepset{}, "") |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 228 | // Assert that the output is generated in an intermediate directory |
| 229 | // fe05bcdcdc4928012781a5f1a2a77cbb5398e106 is the sha1 checksum of "one" |
| 230 | if actual, expected := cmd.outputs[0].String(), "out/soong/mixed_build_sbox_intermediates/fe05bcdcdc4928012781a5f1a2a77cbb5398e106/test/exec_root/one"; expected != actual { |
| 231 | t.Errorf("expected: [%s], actual: [%s]", expected, actual) |
| 232 | } |
| 233 | |
| 234 | // Assert the actual command remains unchanged inside the sandbox |
| 235 | if actual, expected := cmd.buf.String(), "mkdir -p 'test/exec_root' && cd 'test/exec_root' && rm -rf 'one' && touch foo"; expected != actual { |
| 236 | t.Errorf("expected: [%s], actual: [%s]", expected, actual) |
| 237 | } |
| 238 | } |
| 239 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 240 | func TestCoverageFlagsAfterInvokeBazel(t *testing.T) { |
| 241 | testConfig.productVariables.ClangCoverage = boolPtr(true) |
| 242 | |
| 243 | testConfig.productVariables.NativeCoveragePaths = []string{"foo1", "foo2"} |
| 244 | testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1", "bar2"} |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 245 | verifyAqueryContainsFlags(t, testConfig, "--collect_code_coverage", "--instrumentation_filter=+foo1,+foo2,-bar1,-bar2") |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 246 | |
| 247 | testConfig.productVariables.NativeCoveragePaths = []string{"foo1"} |
| 248 | testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1"} |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 249 | verifyAqueryContainsFlags(t, testConfig, "--collect_code_coverage", "--instrumentation_filter=+foo1,-bar1") |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 250 | |
| 251 | testConfig.productVariables.NativeCoveragePaths = []string{"foo1"} |
| 252 | testConfig.productVariables.NativeCoverageExcludePaths = nil |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 253 | verifyAqueryContainsFlags(t, testConfig, "--collect_code_coverage", "--instrumentation_filter=+foo1") |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 254 | |
| 255 | testConfig.productVariables.NativeCoveragePaths = nil |
| 256 | testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1"} |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 257 | verifyAqueryContainsFlags(t, testConfig, "--collect_code_coverage", "--instrumentation_filter=-bar1") |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 258 | |
Wei Li | cbd181c | 2022-11-16 08:59:23 -0800 | [diff] [blame] | 259 | testConfig.productVariables.NativeCoveragePaths = []string{"*"} |
| 260 | testConfig.productVariables.NativeCoverageExcludePaths = nil |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 261 | verifyAqueryContainsFlags(t, testConfig, "--collect_code_coverage", "--instrumentation_filter=+.*") |
Wei Li | cbd181c | 2022-11-16 08:59:23 -0800 | [diff] [blame] | 262 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 263 | testConfig.productVariables.ClangCoverage = boolPtr(false) |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 264 | verifyAqueryDoesNotContainSubstrings(t, testConfig, "collect_code_coverage", "instrumentation_filter") |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 267 | func TestBazelRequestsSorted(t *testing.T) { |
| 268 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{}) |
| 269 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 270 | cfgKeyArm64Android := configKey{arch: "arm64_armv8-a", osType: Android} |
| 271 | cfgKeyArm64Linux := configKey{arch: "arm64_armv8-a", osType: Linux} |
| 272 | cfgKeyOtherAndroid := configKey{arch: "otherarch", osType: Android} |
| 273 | |
| 274 | bazelContext.QueueBazelRequest("zzz", cquery.GetOutputFiles, cfgKeyArm64Android) |
| 275 | bazelContext.QueueBazelRequest("ccc", cquery.GetApexInfo, cfgKeyArm64Android) |
| 276 | bazelContext.QueueBazelRequest("duplicate", cquery.GetOutputFiles, cfgKeyArm64Android) |
| 277 | bazelContext.QueueBazelRequest("duplicate", cquery.GetOutputFiles, cfgKeyArm64Android) |
| 278 | bazelContext.QueueBazelRequest("xxx", cquery.GetOutputFiles, cfgKeyArm64Linux) |
| 279 | bazelContext.QueueBazelRequest("aaa", cquery.GetOutputFiles, cfgKeyArm64Android) |
| 280 | bazelContext.QueueBazelRequest("aaa", cquery.GetOutputFiles, cfgKeyOtherAndroid) |
| 281 | bazelContext.QueueBazelRequest("bbb", cquery.GetOutputFiles, cfgKeyOtherAndroid) |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 282 | |
| 283 | if len(bazelContext.requests) != 7 { |
| 284 | t.Error("Expected 7 request elements, but got", len(bazelContext.requests)) |
| 285 | } |
| 286 | |
| 287 | lastString := "" |
| 288 | for _, val := range bazelContext.requests { |
| 289 | thisString := val.String() |
| 290 | if thisString <= lastString { |
| 291 | t.Errorf("Requests are not ordered correctly. '%s' came before '%s'", lastString, thisString) |
| 292 | } |
| 293 | lastString = thisString |
| 294 | } |
| 295 | } |
| 296 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 297 | func TestIsModuleNameAllowed(t *testing.T) { |
| 298 | libDisabled := "lib_disabled" |
| 299 | libEnabled := "lib_enabled" |
| 300 | libDclaWithinApex := "lib_dcla_within_apex" |
| 301 | libDclaNonApex := "lib_dcla_non_apex" |
| 302 | libNotConverted := "lib_not_converted" |
| 303 | |
| 304 | disabledModules := map[string]bool{ |
| 305 | libDisabled: true, |
| 306 | } |
| 307 | enabledModules := map[string]bool{ |
| 308 | libEnabled: true, |
| 309 | } |
| 310 | dclaEnabledModules := map[string]bool{ |
| 311 | libDclaWithinApex: true, |
| 312 | libDclaNonApex: true, |
| 313 | } |
| 314 | |
| 315 | bazelContext := &mixedBuildBazelContext{ |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 316 | bazelEnabledModules: enabledModules, |
| 317 | bazelDisabledModules: disabledModules, |
| 318 | bazelDclaEnabledModules: dclaEnabledModules, |
| 319 | } |
| 320 | |
| 321 | if bazelContext.IsModuleNameAllowed(libDisabled, true) { |
| 322 | t.Fatalf("%s shouldn't be allowed for mixed build", libDisabled) |
| 323 | } |
| 324 | |
| 325 | if !bazelContext.IsModuleNameAllowed(libEnabled, true) { |
| 326 | t.Fatalf("%s should be allowed for mixed build", libEnabled) |
| 327 | } |
| 328 | |
| 329 | if !bazelContext.IsModuleNameAllowed(libDclaWithinApex, true) { |
| 330 | t.Fatalf("%s should be allowed for mixed build", libDclaWithinApex) |
| 331 | } |
| 332 | |
| 333 | if bazelContext.IsModuleNameAllowed(libDclaNonApex, false) { |
| 334 | t.Fatalf("%s shouldn't be allowed for mixed build", libDclaNonApex) |
| 335 | } |
| 336 | |
| 337 | if bazelContext.IsModuleNameAllowed(libNotConverted, true) { |
| 338 | t.Fatalf("%s shouldn't be allowed for mixed build", libNotConverted) |
| 339 | } |
| 340 | } |
| 341 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 342 | func verifyAqueryContainsFlags(t *testing.T, config Config, expected ...string) { |
| 343 | t.Helper() |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 344 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{}) |
| 345 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 346 | err := bazelContext.InvokeBazel(config, &testInvokeBazelContext{}) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 347 | if err != nil { |
| 348 | t.Fatalf("Did not expect error invoking Bazel, but got %s", err) |
| 349 | } |
| 350 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 351 | sliceContains := func(slice []string, x string) bool { |
| 352 | for _, s := range slice { |
| 353 | if s == x { |
| 354 | return true |
| 355 | } |
| 356 | } |
| 357 | return false |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 360 | aqueryArgv := bazelContext.bazelRunner.(*mockBazelRunner).bazelCommandRequests[aqueryCmd].Argv |
| 361 | |
| 362 | for _, expectedFlag := range expected { |
| 363 | if !sliceContains(aqueryArgv, expectedFlag) { |
| 364 | t.Errorf("aquery does not contain expected flag %#v. Argv was: %#v", expectedFlag, aqueryArgv) |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | func verifyAqueryDoesNotContainSubstrings(t *testing.T, config Config, substrings ...string) { |
| 370 | t.Helper() |
| 371 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{}) |
| 372 | |
| 373 | err := bazelContext.InvokeBazel(config, &testInvokeBazelContext{}) |
| 374 | if err != nil { |
| 375 | t.Fatalf("Did not expect error invoking Bazel, but got %s", err) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 378 | sliceContainsSubstring := func(slice []string, substring string) bool { |
| 379 | for _, s := range slice { |
| 380 | if strings.Contains(s, substring) { |
| 381 | return true |
| 382 | } |
| 383 | } |
| 384 | return false |
| 385 | } |
| 386 | |
| 387 | aqueryArgv := bazelContext.bazelRunner.(*mockBazelRunner).bazelCommandRequests[aqueryCmd].Argv |
| 388 | |
| 389 | for _, substring := range substrings { |
| 390 | if sliceContainsSubstring(aqueryArgv, substring) { |
| 391 | t.Errorf("aquery contains unexpected substring %#v. Argv was: %#v", substring, aqueryArgv) |
| 392 | } |
| 393 | } |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 396 | func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string) (*mixedBuildBazelContext, string) { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 397 | t.Helper() |
| 398 | p := bazelPaths{ |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 399 | soongOutDir: t.TempDir(), |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 400 | outputBase: "outputbase", |
| 401 | workspaceDir: "workspace_dir", |
| 402 | } |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 403 | if _, exists := bazelCommandResults[aqueryCmd]; !exists { |
| 404 | bazelCommandResults[aqueryCmd] = "" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 405 | } |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 406 | runner := &mockBazelRunner{ |
| 407 | testHelper: t, |
| 408 | bazelCommandResults: bazelCommandResults, |
| 409 | bazelCommandRequests: map[bazelCommand]bazel.CmdRequest{}, |
| 410 | } |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 411 | return &mixedBuildBazelContext{ |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 412 | bazelRunner: runner, |
| 413 | paths: &p, |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 414 | }, p.soongOutDir |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 415 | } |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 416 | |
| 417 | // Transform the json format to ActionGraphContainer |
| 418 | func JsonToActionGraphContainer(inputString string) ([]byte, error) { |
| 419 | var aqueryProtoResult analysis_v2_proto.ActionGraphContainer |
| 420 | err := json.Unmarshal([]byte(inputString), &aqueryProtoResult) |
| 421 | if err != nil { |
| 422 | return []byte(""), err |
| 423 | } |
| 424 | data, _ := proto.Marshal(&aqueryProtoResult) |
| 425 | return data, err |
| 426 | } |