Aditya Choudhary | 8094b6b | 2023-10-12 19:40:17 +0000 | [diff] [blame^] | 1 | package java |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "android/soong/android" |
| 8 | soongTesting "android/soong/testing" |
| 9 | "android/soong/testing/code_metadata_internal_proto" |
| 10 | "google.golang.org/protobuf/proto" |
| 11 | ) |
| 12 | |
| 13 | func TestCodeMetadata(t *testing.T) { |
| 14 | bp := `code_metadata { |
| 15 | name: "module-name", |
| 16 | teamId: "12345", |
| 17 | code: [ |
| 18 | "foo", |
| 19 | ] |
| 20 | } |
| 21 | |
| 22 | java_sdk_library { |
| 23 | name: "foo", |
| 24 | srcs: ["a.java"], |
| 25 | }` |
| 26 | result := runCodeMetadataTest(t, android.FixtureExpectsNoErrors, bp) |
| 27 | |
| 28 | module := result.ModuleForTests( |
| 29 | "module-name", "", |
| 30 | ).Module().(*soongTesting.CodeMetadataModule) |
| 31 | |
| 32 | // Check that the provider has the right contents |
| 33 | data := result.ModuleProvider( |
| 34 | module, soongTesting.CodeMetadataProviderKey, |
| 35 | ).(soongTesting.CodeMetadataProviderData) |
| 36 | if !strings.HasSuffix( |
| 37 | data.IntermediatePath.String(), "/intermediateCodeMetadata.pb", |
| 38 | ) { |
| 39 | t.Errorf( |
| 40 | "Missing intermediates path in provider: %s", |
| 41 | data.IntermediatePath.String(), |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | buildParamsSlice := module.BuildParamsForTests() |
| 46 | var metadata = "" |
| 47 | for _, params := range buildParamsSlice { |
| 48 | if params.Rule.String() == "android/soong/android.writeFile" { |
| 49 | metadata = params.Args["content"] |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | metadataList := make([]*code_metadata_internal_proto.CodeMetadataInternal_TargetOwnership, 0, 2) |
| 54 | teamId := "12345" |
| 55 | bpFilePath := "Android.bp" |
| 56 | targetName := "foo" |
| 57 | srcFile := []string{"a.java"} |
| 58 | expectedMetadataProto := code_metadata_internal_proto.CodeMetadataInternal_TargetOwnership{ |
| 59 | TrendyTeamId: &teamId, |
| 60 | TargetName: &targetName, |
| 61 | Path: &bpFilePath, |
| 62 | SourceFiles: srcFile, |
| 63 | } |
| 64 | metadataList = append(metadataList, &expectedMetadataProto) |
| 65 | |
| 66 | CodeMetadataMetadata := code_metadata_internal_proto.CodeMetadataInternal{TargetOwnershipList: metadataList} |
| 67 | protoData, _ := proto.Marshal(&CodeMetadataMetadata) |
| 68 | rawData := string(protoData) |
| 69 | formattedData := strings.ReplaceAll(rawData, "\n", "\\n") |
| 70 | expectedMetadata := "'" + formattedData + "\\n'" |
| 71 | |
| 72 | if metadata != expectedMetadata { |
| 73 | t.Errorf( |
| 74 | "Retrieved metadata: %s is not equal to expectedMetadata: %s", metadata, |
| 75 | expectedMetadata, |
| 76 | ) |
| 77 | } |
| 78 | |
| 79 | // Tests for all_test_spec singleton. |
| 80 | singleton := result.SingletonForTests("all_code_metadata") |
| 81 | rule := singleton.Rule("all_code_metadata_rule") |
| 82 | prebuiltOs := result.Config.PrebuiltOS() |
| 83 | expectedCmd := "out/soong/host/" + prebuiltOs + "/bin/metadata -rule code_metadata -inputFile out/soong/all_code_metadata_paths.rsp -outputFile out/soong/ownership/all_code_metadata.pb" |
| 84 | expectedOutputFile := "out/soong/ownership/all_code_metadata.pb" |
| 85 | expectedInputFile := "out/soong/.intermediates/module-name/intermediateCodeMetadata.pb" |
| 86 | if !strings.Contains( |
| 87 | strings.TrimSpace(rule.Output.String()), |
| 88 | expectedOutputFile, |
| 89 | ) { |
| 90 | t.Errorf( |
| 91 | "Retrieved singletonOutputFile: %s is not equal to expectedSingletonOutputFile: %s", |
| 92 | rule.Output.String(), expectedOutputFile, |
| 93 | ) |
| 94 | } |
| 95 | |
| 96 | if !strings.Contains( |
| 97 | strings.TrimSpace(rule.Inputs[0].String()), |
| 98 | expectedInputFile, |
| 99 | ) { |
| 100 | t.Errorf( |
| 101 | "Retrieved singletonInputFile: %s is not equal to expectedSingletonInputFile: %s", |
| 102 | rule.Inputs[0].String(), expectedInputFile, |
| 103 | ) |
| 104 | } |
| 105 | |
| 106 | if !strings.Contains( |
| 107 | strings.TrimSpace(rule.RuleParams.Command), |
| 108 | expectedCmd, |
| 109 | ) { |
| 110 | t.Errorf( |
| 111 | "Retrieved cmd: %s doesn't contain expectedCmd: %s", |
| 112 | rule.RuleParams.Command, expectedCmd, |
| 113 | ) |
| 114 | } |
| 115 | } |
| 116 | func runCodeMetadataTest( |
| 117 | t *testing.T, errorHandler android.FixtureErrorHandler, bp string, |
| 118 | ) *android.TestResult { |
| 119 | return android.GroupFixturePreparers( |
| 120 | soongTesting.PrepareForTestWithTestingBuildComponents, prepareForJavaTest, |
| 121 | PrepareForTestWithJavaSdkLibraryFiles, FixtureWithLastReleaseApis("foo"), |
| 122 | ). |
| 123 | ExtendWithErrorHandler(errorHandler). |
| 124 | RunTestWithBp(t, bp) |
| 125 | } |