blob: cfdccd7c382490e4bf8465747de9b901b1f5fbeb [file] [log] [blame]
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001package android
2
3import (
4 "os"
5 "path/filepath"
6 "reflect"
7 "testing"
Chris Parsonsf874e462022-05-10 13:50:12 -04008
9 "android/soong/bazel/cquery"
Liz Kammer8d62a4f2021-04-08 09:47:28 -040010)
11
12func TestRequestResultsAfterInvokeBazel(t *testing.T) {
13 label := "//foo:bar"
Liz Kammer0940b892022-03-18 15:55:04 -040014 cfg := configKey{"arm64_armv8-a", Android}
Liz Kammer8d62a4f2021-04-08 09:47:28 -040015 bazelContext, _ := testBazelContext(t, map[bazelCommand]string{
Liz Kammer0940b892022-03-18 15:55:04 -040016 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 -040017 })
Chris Parsonsf874e462022-05-10 13:50:12 -040018 bazelContext.QueueBazelRequest(label, cquery.GetOutputFiles, cfg)
Liz Kammer8d62a4f2021-04-08 09:47:28 -040019 err := bazelContext.InvokeBazel()
20 if err != nil {
21 t.Fatalf("Did not expect error invoking Bazel, but got %s", err)
22 }
Chris Parsonsf874e462022-05-10 13:50:12 -040023 g, err := bazelContext.GetOutputFiles(label, cfg)
24 if err != nil {
25 t.Errorf("Expected cquery results after running InvokeBazel(), but got err %v", err)
Liz Kammer8d62a4f2021-04-08 09:47:28 -040026 } else if w := []string{"out/foo/bar.txt"}; !reflect.DeepEqual(w, g) {
27 t.Errorf("Expected output %s, got %s", w, g)
28 }
29}
30
31func TestInvokeBazelWritesBazelFiles(t *testing.T) {
32 bazelContext, baseDir := testBazelContext(t, map[bazelCommand]string{})
33 err := bazelContext.InvokeBazel()
34 if err != nil {
35 t.Fatalf("Did not expect error invoking Bazel, but got %s", err)
36 }
Lukacs T. Berki3069dd92021-05-11 16:54:29 +020037 if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "mixed_builds", "main.bzl")); os.IsNotExist(err) {
Liz Kammer8d62a4f2021-04-08 09:47:28 -040038 t.Errorf("Expected main.bzl to exist, but it does not")
39 } else if err != nil {
40 t.Errorf("Unexpected error stating main.bzl %s", err)
41 }
42
Lukacs T. Berki3069dd92021-05-11 16:54:29 +020043 if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "mixed_builds", "BUILD.bazel")); os.IsNotExist(err) {
Liz Kammer8d62a4f2021-04-08 09:47:28 -040044 t.Errorf("Expected BUILD.bazel to exist, but it does not")
45 } else if err != nil {
46 t.Errorf("Unexpected error stating BUILD.bazel %s", err)
47 }
48
Liz Kammer286c9fa2021-04-21 08:46:34 -040049 if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "WORKSPACE.bazel")); os.IsNotExist(err) {
Liz Kammer8d62a4f2021-04-08 09:47:28 -040050 t.Errorf("Expected WORKSPACE.bazel to exist, but it does not")
51 } else if err != nil {
52 t.Errorf("Unexpected error stating WORKSPACE.bazel %s", err)
53 }
54}
55
56func TestInvokeBazelPopulatesBuildStatements(t *testing.T) {
57 bazelContext, _ := testBazelContext(t, map[bazelCommand]string{
Lukacs T. Berki3069dd92021-05-11 16:54:29 +020058 bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}: `
Liz Kammer8d62a4f2021-04-08 09:47:28 -040059{
60 "artifacts": [{
61 "id": 1,
62 "pathFragmentId": 1
63 }, {
64 "id": 2,
65 "pathFragmentId": 2
66 }],
67 "actions": [{
68 "targetId": 1,
69 "actionKey": "x",
70 "mnemonic": "x",
71 "arguments": ["touch", "foo"],
72 "inputDepSetIds": [1],
73 "outputIds": [1],
74 "primaryOutputId": 1
75 }],
76 "depSetOfFiles": [{
77 "id": 1,
78 "directArtifactIds": [1, 2]
79 }],
80 "pathFragments": [{
81 "id": 1,
82 "label": "one"
83 }, {
84 "id": 2,
85 "label": "two"
86 }]
87}`,
88 })
89 err := bazelContext.InvokeBazel()
90 if err != nil {
91 t.Fatalf("Did not expect error invoking Bazel, but got %s", err)
92 }
93
94 got := bazelContext.BuildStatementsToRegister()
95 if want := 1; len(got) != want {
96 t.Errorf("Expected %d registered build statements, got %#v", want, got)
97 }
98}
99
100func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string) (*bazelContext, string) {
101 t.Helper()
102 p := bazelPaths{
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200103 soongOutDir: t.TempDir(),
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400104 outputBase: "outputbase",
105 workspaceDir: "workspace_dir",
106 }
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200107 aqueryCommand := bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400108 if _, exists := bazelCommandResults[aqueryCommand]; !exists {
109 bazelCommandResults[aqueryCommand] = "{}\n"
110 }
111 runner := &mockBazelRunner{bazelCommandResults: bazelCommandResults}
112 return &bazelContext{
113 bazelRunner: runner,
114 paths: &p,
115 requests: map[cqueryKey]bool{},
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200116 }, p.soongOutDir
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400117}