Use json encoding for cquery ParseResult

also using a generic json_encode as an approximation for `json.encode`

Test: 1. use m --bazel-mode-dev nothing to verify no error occured. 2.
revise the test cases under request_type_test. 3. pass the test cases

Bug:242587802

Change-Id: I1288ecca1afd3e32f6473bcabae4ee2cb5838007
This commit is contained in:
Jason 2022-09-15 23:02:53 +00:00 committed by Jason Wu
parent bda5850a71
commit fcc53f992b
2 changed files with 43 additions and 115 deletions

View file

@ -158,63 +158,28 @@ else:
# NOTE: It's OK if there's no ToC, as Soong just uses it for optimization
pass
returns = [
outputFiles,
ccObjectFiles,
sharedLibraries,
staticLibraries,
includes,
system_includes,
headers,
rootStaticArchives,
rootSharedLibraries,
[toc_file]
]
return "|".join([", ".join(r) for r in returns])`
return json_encode({
"OutputFiles": outputFiles,
"CcObjectFiles": ccObjectFiles,
"CcSharedLibraryFiles": sharedLibraries,
"CcStaticLibraryFiles": staticLibraries,
"Includes": includes,
"SystemIncludes": system_includes,
"Headers": headers,
"RootStaticArchives": rootStaticArchives,
"RootDynamicLibraries": rootSharedLibraries,
"toc_file": toc_file
})`
}
// ParseResult returns a value obtained by parsing the result of the request's Starlark function.
// The given rawString must correspond to the string output which was created by evaluating the
// Starlark given in StarlarkFunctionBody.
func (g getCcInfoType) ParseResult(rawString string) (CcInfo, error) {
const expectedLen = 10
splitString := strings.Split(rawString, "|")
if len(splitString) != expectedLen {
return CcInfo{}, fmt.Errorf("expected %d items, got %q", expectedLen, splitString)
}
outputFilesString := splitString[0]
ccObjectsString := splitString[1]
ccSharedLibrariesString := splitString[2]
ccStaticLibrariesString := splitString[3]
includesString := splitString[4]
systemIncludesString := splitString[5]
headersString := splitString[6]
rootStaticArchivesString := splitString[7]
rootDynamicLibrariesString := splitString[8]
tocFile := splitString[9] // NOTE: Will be the empty string if there wasn't
outputFiles := splitOrEmpty(outputFilesString, ", ")
ccObjects := splitOrEmpty(ccObjectsString, ", ")
ccSharedLibraries := splitOrEmpty(ccSharedLibrariesString, ", ")
ccStaticLibraries := splitOrEmpty(ccStaticLibrariesString, ", ")
includes := splitOrEmpty(includesString, ", ")
systemIncludes := splitOrEmpty(systemIncludesString, ", ")
headers := splitOrEmpty(headersString, ", ")
rootStaticArchives := splitOrEmpty(rootStaticArchivesString, ", ")
rootDynamicLibraries := splitOrEmpty(rootDynamicLibrariesString, ", ")
return CcInfo{
OutputFiles: outputFiles,
CcObjectFiles: ccObjects,
CcSharedLibraryFiles: ccSharedLibraries,
CcStaticLibraryFiles: ccStaticLibraries,
Includes: includes,
SystemIncludes: systemIncludes,
Headers: headers,
RootStaticArchives: rootStaticArchives,
RootDynamicLibraries: rootDynamicLibraries,
TocFile: tocFile,
}, nil
var ccInfo CcInfo
decoder := json.NewDecoder(strings.NewReader(rawString))
err := decoder.Decode(&ccInfo)
return ccInfo, err
}
// Query Bazel for the artifacts generated by the apex modules.

View file

@ -1,9 +1,8 @@
package cquery
import (
"fmt"
"encoding/json"
"reflect"
"strings"
"testing"
)
@ -63,74 +62,49 @@ func TestGetPythonBinaryParseResults(t *testing.T) {
}
func TestGetCcInfoParseResults(t *testing.T) {
const expectedSplits = 10
noResult := strings.Repeat("|", expectedSplits-1)
testCases := []struct {
description string
input string
inputCcInfo CcInfo
expectedOutput CcInfo
expectedErrorMessage string
}{
{
description: "no result",
input: noResult,
expectedOutput: CcInfo{
OutputFiles: []string{},
CcObjectFiles: []string{},
CcSharedLibraryFiles: []string{},
CcStaticLibraryFiles: []string{},
Includes: []string{},
SystemIncludes: []string{},
Headers: []string{},
RootStaticArchives: []string{},
RootDynamicLibraries: []string{},
TocFile: "",
},
description: "no result",
inputCcInfo: CcInfo{},
expectedOutput: CcInfo{},
},
{
description: "only output",
input: "test" + noResult,
inputCcInfo: CcInfo{
OutputFiles: []string{"test", "test3"},
},
expectedOutput: CcInfo{
OutputFiles: []string{"test"},
CcObjectFiles: []string{},
CcSharedLibraryFiles: []string{},
CcStaticLibraryFiles: []string{},
Includes: []string{},
SystemIncludes: []string{},
Headers: []string{},
RootStaticArchives: []string{},
RootDynamicLibraries: []string{},
TocFile: "",
OutputFiles: []string{"test", "test3"},
},
},
{
description: "only ToC",
input: noResult + "test",
inputCcInfo: CcInfo{
TocFile: "test",
},
expectedOutput: CcInfo{
OutputFiles: []string{},
CcObjectFiles: []string{},
CcSharedLibraryFiles: []string{},
CcStaticLibraryFiles: []string{},
Includes: []string{},
SystemIncludes: []string{},
Headers: []string{},
RootStaticArchives: []string{},
RootDynamicLibraries: []string{},
TocFile: "test",
TocFile: "test",
},
},
{
description: "all items set",
input: "out1, out2" +
"|object1, object2" +
"|shared_lib1, shared_lib2" +
"|static_lib1, static_lib2" +
"|., dir/subdir" +
"|system/dir, system/other/dir" +
"|dir/subdir/hdr.h" +
"|rootstaticarchive1" +
"|rootdynamiclibrary1" +
"|lib.so.toc",
inputCcInfo: CcInfo{
OutputFiles: []string{"out1", "out2"},
CcObjectFiles: []string{"object1", "object2"},
CcSharedLibraryFiles: []string{"shared_lib1", "shared_lib2"},
CcStaticLibraryFiles: []string{"static_lib1", "static_lib2"},
Includes: []string{".", "dir/subdir"},
SystemIncludes: []string{"system/dir", "system/other/dir"},
Headers: []string{"dir/subdir/hdr.h"},
RootStaticArchives: []string{"rootstaticarchive1"},
RootDynamicLibraries: []string{"rootdynamiclibrary1"},
TocFile: "lib.so.toc",
},
expectedOutput: CcInfo{
OutputFiles: []string{"out1", "out2"},
CcObjectFiles: []string{"object1", "object2"},
@ -144,21 +118,10 @@ func TestGetCcInfoParseResults(t *testing.T) {
TocFile: "lib.so.toc",
},
},
{
description: "too few result splits",
input: "|",
expectedOutput: CcInfo{},
expectedErrorMessage: fmt.Sprintf("expected %d items, got %q", expectedSplits, []string{"", ""}),
},
{
description: "too many result splits",
input: strings.Repeat("|", expectedSplits+1), // 2 too many
expectedOutput: CcInfo{},
expectedErrorMessage: fmt.Sprintf("expected %d items, got %q", expectedSplits, make([]string, expectedSplits+2)),
},
}
for _, tc := range testCases {
actualOutput, err := GetCcInfo.ParseResult(tc.input)
jsonInput, err := json.Marshal(tc.inputCcInfo)
actualOutput, err := GetCcInfo.ParseResult(string(jsonInput))
if (err == nil && tc.expectedErrorMessage != "") ||
(err != nil && err.Error() != tc.expectedErrorMessage) {
t.Errorf("%q:\n%12s: %q\n%12s: %q", tc.description, "expect Error", tc.expectedErrorMessage, "but got", err)