Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "reflect" |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 7 | "strings" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 8 | "testing" |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 9 | |
| 10 | "android/soong/bazel/cquery" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 13 | var testConfig = TestConfig("out", nil, "", nil) |
| 14 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 15 | func TestRequestResultsAfterInvokeBazel(t *testing.T) { |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 16 | label := "@//foo:bar" |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 17 | cfg := configKey{"arm64_armv8-a", Android} |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 18 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{ |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 19 | bazelCommand{command: "cquery", expression: "deps(@soong_injection//mixed_builds:buildroot, 2)"}: `@//foo:bar|arm64_armv8-a|android>>out/foo/bar.txt`, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 20 | }) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 21 | bazelContext.QueueBazelRequest(label, cquery.GetOutputFiles, cfg) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 22 | err := bazelContext.InvokeBazel(testConfig) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 23 | if err != nil { |
| 24 | t.Fatalf("Did not expect error invoking Bazel, but got %s", err) |
| 25 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 26 | 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 Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 29 | } 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 | |
| 34 | func TestInvokeBazelWritesBazelFiles(t *testing.T) { |
| 35 | bazelContext, baseDir := testBazelContext(t, map[bazelCommand]string{}) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 36 | err := bazelContext.InvokeBazel(testConfig) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 37 | if err != nil { |
| 38 | t.Fatalf("Did not expect error invoking Bazel, but got %s", err) |
| 39 | } |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 40 | 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] | 41 | 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. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 46 | 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] | 47 | 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 Kammer | 286c9fa | 2021-04-21 08:46:34 -0400 | [diff] [blame] | 52 | 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] | 53 | 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 | |
| 59 | func TestInvokeBazelPopulatesBuildStatements(t *testing.T) { |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 60 | type testCase struct { |
| 61 | input string |
| 62 | command string |
| 63 | } |
| 64 | |
| 65 | var testCases = []testCase{ |
| 66 | {` |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 67 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 68 | "artifacts": [ |
| 69 | { "id": 1, "pathFragmentId": 1 }, |
| 70 | { "id": 2, "pathFragmentId": 2 }], |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 71 | "actions": [{ |
| 72 | "targetId": 1, |
| 73 | "actionKey": "x", |
| 74 | "mnemonic": "x", |
| 75 | "arguments": ["touch", "foo"], |
| 76 | "inputDepSetIds": [1], |
| 77 | "outputIds": [1], |
| 78 | "primaryOutputId": 1 |
| 79 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 80 | "depSetOfFiles": [ |
| 81 | { "id": 1, "directArtifactIds": [1, 2] }], |
| 82 | "pathFragments": [ |
| 83 | { "id": 1, "label": "one" }, |
| 84 | { "id": 2, "label": "two" }] |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 85 | }`, |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 86 | "cd 'test/exec_root' && rm -f 'one' && touch foo", |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 87 | }, {` |
| 88 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 89 | "artifacts": [ |
| 90 | { "id": 1, "pathFragmentId": 10 }, |
| 91 | { "id": 2, "pathFragmentId": 20 }], |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 92 | "actions": [{ |
| 93 | "targetId": 100, |
| 94 | "actionKey": "x", |
| 95 | "mnemonic": "x", |
| 96 | "arguments": ["bogus", "command"], |
| 97 | "outputIds": [1, 2], |
| 98 | "primaryOutputId": 1 |
| 99 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 100 | "pathFragments": [ |
| 101 | { "id": 10, "label": "one", "parentId": 30 }, |
| 102 | { "id": 20, "label": "one.d", "parentId": 30 }, |
| 103 | { "id": 30, "label": "parent" }] |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 104 | }`, |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 105 | `cd 'test/exec_root' && rm -f '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] | 106 | }, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 109 | for i, testCase := range testCases { |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 110 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{ |
| 111 | bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}: testCase.input}) |
| 112 | |
| 113 | err := bazelContext.InvokeBazel(testConfig) |
| 114 | if err != nil { |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 115 | 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] | 116 | } |
| 117 | |
| 118 | got := bazelContext.BuildStatementsToRegister() |
| 119 | if want := 1; len(got) != want { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 120 | t.Fatalf("expected %d registered build statements, but got %#v", want, got) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | cmd := RuleBuilderCommand{} |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 124 | createCommand(&cmd, got[0], "test/exec_root", "test/bazel_out", PathContextForTesting(TestConfig("out", nil, "", nil))) |
| 125 | if actual, expected := cmd.buf.String(), testCase.command; expected != actual { |
| 126 | t.Errorf("expected: [%s], actual: [%s]", expected, actual) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 127 | } |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 131 | func TestCoverageFlagsAfterInvokeBazel(t *testing.T) { |
| 132 | testConfig.productVariables.ClangCoverage = boolPtr(true) |
| 133 | |
| 134 | testConfig.productVariables.NativeCoveragePaths = []string{"foo1", "foo2"} |
| 135 | testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1", "bar2"} |
| 136 | verifyExtraFlags(t, testConfig, `--collect_code_coverage --instrumentation_filter=+foo1,+foo2,-bar1,-bar2`) |
| 137 | |
| 138 | testConfig.productVariables.NativeCoveragePaths = []string{"foo1"} |
| 139 | testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1"} |
| 140 | verifyExtraFlags(t, testConfig, `--collect_code_coverage --instrumentation_filter=+foo1,-bar1`) |
| 141 | |
| 142 | testConfig.productVariables.NativeCoveragePaths = []string{"foo1"} |
| 143 | testConfig.productVariables.NativeCoverageExcludePaths = nil |
| 144 | verifyExtraFlags(t, testConfig, `--collect_code_coverage --instrumentation_filter=+foo1`) |
| 145 | |
| 146 | testConfig.productVariables.NativeCoveragePaths = nil |
| 147 | testConfig.productVariables.NativeCoverageExcludePaths = []string{"bar1"} |
| 148 | verifyExtraFlags(t, testConfig, `--collect_code_coverage --instrumentation_filter=-bar1`) |
| 149 | |
| 150 | testConfig.productVariables.ClangCoverage = boolPtr(false) |
| 151 | actual := verifyExtraFlags(t, testConfig, ``) |
| 152 | if strings.Contains(actual, "--collect_code_coverage") || |
| 153 | strings.Contains(actual, "--instrumentation_filter=") { |
| 154 | t.Errorf("Expected code coverage disabled, but got %#v", actual) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func verifyExtraFlags(t *testing.T, config Config, expected string) string { |
| 159 | bazelContext, _ := testBazelContext(t, map[bazelCommand]string{}) |
| 160 | |
| 161 | err := bazelContext.InvokeBazel(config) |
| 162 | if err != nil { |
| 163 | t.Fatalf("Did not expect error invoking Bazel, but got %s", err) |
| 164 | } |
| 165 | |
| 166 | flags := bazelContext.bazelRunner.(*mockBazelRunner).extraFlags |
| 167 | if expected := 3; len(flags) != expected { |
| 168 | t.Errorf("Expected %d extra flags got %#v", expected, flags) |
| 169 | } |
| 170 | |
| 171 | actual := flags[1] |
| 172 | if !strings.Contains(actual, expected) { |
| 173 | t.Errorf("Expected %#v got %#v", expected, actual) |
| 174 | } |
| 175 | |
| 176 | return actual |
| 177 | } |
| 178 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 179 | func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string) (*bazelContext, string) { |
| 180 | t.Helper() |
| 181 | p := bazelPaths{ |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 182 | soongOutDir: t.TempDir(), |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 183 | outputBase: "outputbase", |
| 184 | workspaceDir: "workspace_dir", |
| 185 | } |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 186 | aqueryCommand := bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"} |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 187 | if _, exists := bazelCommandResults[aqueryCommand]; !exists { |
| 188 | bazelCommandResults[aqueryCommand] = "{}\n" |
| 189 | } |
| 190 | runner := &mockBazelRunner{bazelCommandResults: bazelCommandResults} |
| 191 | return &bazelContext{ |
| 192 | bazelRunner: runner, |
| 193 | paths: &p, |
| 194 | requests: map[cqueryKey]bool{}, |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 195 | }, p.soongOutDir |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 196 | } |