blob: f0a5fdb4ba8b48b1e72c513574292e0c081af9e4 [file] [log] [blame]
Aditya Choudharyb7b3de82023-11-17 12:37:41 +00001package java
2
3import (
4 "strings"
5 "testing"
6
7 "android/soong/android"
8 soongTesting "android/soong/testing"
9 "android/soong/testing/test_spec_proto"
10 "google.golang.org/protobuf/proto"
11)
12
13func TestTestSpec(t *testing.T) {
14 bp := `test_spec {
15 name: "module-name",
16 teamId: "12345",
17 tests: [
18 "java-test-module-name-one",
19 "java-test-module-name-two"
20 ]
21 }
22
23 java_test {
24 name: "java-test-module-name-one",
25 }
26
27 java_test {
28 name: "java-test-module-name-two",
29 }`
Aditya Choudhary8094b6b2023-10-12 19:40:17 +000030 result := runTestSpecTest(t, android.FixtureExpectsNoErrors, bp)
Aditya Choudharyb7b3de82023-11-17 12:37:41 +000031
Colin Cross31a67452023-11-02 16:57:08 -070032 module := result.ModuleForTests("module-name", "")
Aditya Choudharyb7b3de82023-11-17 12:37:41 +000033
34 // Check that the provider has the right contents
Yu Liu663e4502024-08-12 18:23:59 +000035 data, _ := android.OtherModuleProvider(result, module.Module(), soongTesting.TestSpecProviderKey)
Aditya Choudharyb7b3de82023-11-17 12:37:41 +000036 if !strings.HasSuffix(
37 data.IntermediatePath.String(), "/intermediateTestSpecMetadata.pb",
38 ) {
39 t.Errorf(
40 "Missing intermediates path in provider: %s",
41 data.IntermediatePath.String(),
42 )
43 }
44
Colin Cross31a67452023-11-02 16:57:08 -070045 metadata := android.ContentFromFileRuleForTests(t, result.TestContext,
46 module.Output(data.IntermediatePath.String()))
Aditya Choudharyb7b3de82023-11-17 12:37:41 +000047
48 metadataList := make([]*test_spec_proto.TestSpec_OwnershipMetadata, 0, 2)
49 teamId := "12345"
50 bpFilePath := "Android.bp"
51 targetNames := []string{
52 "java-test-module-name-one", "java-test-module-name-two",
53 }
54
55 for _, test := range targetNames {
56 targetName := test
57 metadata := test_spec_proto.TestSpec_OwnershipMetadata{
58 TrendyTeamId: &teamId,
59 TargetName: &targetName,
60 Path: &bpFilePath,
61 }
62 metadataList = append(metadataList, &metadata)
63 }
64 testSpecMetadata := test_spec_proto.TestSpec{OwnershipMetadataList: metadataList}
65 protoData, _ := proto.Marshal(&testSpecMetadata)
Colin Cross31a67452023-11-02 16:57:08 -070066 expectedMetadata := string(protoData)
Aditya Choudharyb7b3de82023-11-17 12:37:41 +000067
68 if metadata != expectedMetadata {
69 t.Errorf(
Aditya Choudhary8094b6b2023-10-12 19:40:17 +000070 "Retrieved metadata: %s doesn't contain expectedMetadata: %s", metadata,
Aditya Choudharyb7b3de82023-11-17 12:37:41 +000071 expectedMetadata,
72 )
73 }
74
75 // Tests for all_test_spec singleton.
76 singleton := result.SingletonForTests("all_test_specs")
77 rule := singleton.Rule("all_test_specs_rule")
Aditya Choudhary35629622023-11-21 22:40:27 +000078 prebuiltOs := result.Config.PrebuiltOS()
79 expectedCmd := "out/soong/host/" + prebuiltOs + "/bin/metadata -rule test_spec -inputFile out/soong/all_test_spec_paths.rsp -outputFile out/soong/ownership/all_test_specs.pb"
Aditya Choudharyb7b3de82023-11-17 12:37:41 +000080 expectedOutputFile := "out/soong/ownership/all_test_specs.pb"
81 expectedInputFile := "out/soong/.intermediates/module-name/intermediateTestSpecMetadata.pb"
82 if !strings.Contains(
83 strings.TrimSpace(rule.Output.String()),
84 expectedOutputFile,
85 ) {
86 t.Errorf(
87 "Retrieved singletonOutputFile: %s is not equal to expectedSingletonOutputFile: %s",
88 rule.Output.String(), expectedOutputFile,
89 )
90 }
91
92 if !strings.Contains(
93 strings.TrimSpace(rule.Inputs[0].String()),
94 expectedInputFile,
95 ) {
96 t.Errorf(
97 "Retrieved singletonInputFile: %s is not equal to expectedSingletonInputFile: %s",
98 rule.Inputs[0].String(), expectedInputFile,
99 )
100 }
101
102 if !strings.Contains(
103 strings.TrimSpace(rule.RuleParams.Command),
104 expectedCmd,
105 ) {
106 t.Errorf(
107 "Retrieved cmd: %s is not equal to expectedCmd: %s",
108 rule.RuleParams.Command, expectedCmd,
109 )
110 }
111}
112
Aditya Choudhary8094b6b2023-10-12 19:40:17 +0000113func runTestSpecTest(
Colin Crossbc7d76c2023-12-12 16:39:03 -0800114 t *testing.T, errorHandler android.FixtureErrorHandler, bp string,
Aditya Choudharyb7b3de82023-11-17 12:37:41 +0000115) *android.TestResult {
116 return android.GroupFixturePreparers(
Aditya Choudhary8094b6b2023-10-12 19:40:17 +0000117 soongTesting.PrepareForTestWithTestingBuildComponents,
Aditya Choudharyb7b3de82023-11-17 12:37:41 +0000118 PrepareForIntegrationTestWithJava,
119 ).
120 ExtendWithErrorHandler(errorHandler).
121 RunTestWithBp(t, bp)
122}