Add infrastructure to support PythonBinary

Add a new request type with its own StarLark function
Hook it up via GetPythonBinary
Add to MockBazelContext a LabelToPythonBinary
Add a test for the new request type

Test: request_type_test.go:TestGetPythonBinaryParseResults
Change-Id: I05f6506adfbbdac8b3f40475509ed02ab8e844e5
This commit is contained in:
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux
2021-08-11 16:48:30 +00:00
parent dd7107c84b
commit a05a255eac
3 changed files with 80 additions and 4 deletions

View File

@@ -80,6 +80,9 @@ type BazelContext interface {
// Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error)
// Returns the executable binary resultant from building together the python sources
GetPythonBinary(label string, archType ArchType) (string, bool)
// ** End cquery methods
// Issues commands to Bazel to receive results for all cquery requests
@@ -134,8 +137,9 @@ var _ BazelContext = noopBazelContext{}
type MockBazelContext struct {
OutputBaseDir string
LabelToOutputFiles map[string][]string
LabelToCcInfo map[string]cquery.CcInfo
LabelToOutputFiles map[string][]string
LabelToCcInfo map[string]cquery.CcInfo
LabelToPythonBinary map[string]string
}
func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
@@ -148,6 +152,11 @@ func (m MockBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcI
return result, ok, nil
}
func (m MockBazelContext) GetPythonBinary(label string, archType ArchType) (string, bool) {
result, ok := m.LabelToPythonBinary[label]
return result, ok
}
func (m MockBazelContext) InvokeBazel() error {
panic("unimplemented")
}
@@ -185,6 +194,16 @@ func (bazelCtx *bazelContext) GetCcInfo(label string, archType ArchType) (cquery
return ret, ok, err
}
func (bazelCtx *bazelContext) GetPythonBinary(label string, archType ArchType) (string, bool) {
rawString, ok := bazelCtx.cquery(label, cquery.GetPythonBinary, archType)
var ret string
if ok {
bazelOutput := strings.TrimSpace(rawString)
ret = cquery.GetPythonBinary.ParseResult(bazelOutput)
}
return ret, ok
}
func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
panic("unimplemented")
}
@@ -193,6 +212,10 @@ func (n noopBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcI
panic("unimplemented")
}
func (n noopBazelContext) GetPythonBinary(label string, archType ArchType) (string, bool) {
panic("unimplemented")
}
func (n noopBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
panic("unimplemented")
}

View File

@@ -6,8 +6,9 @@ import (
)
var (
GetOutputFiles = &getOutputFilesRequestType{}
GetCcInfo = &getCcInfoType{}
GetOutputFiles = &getOutputFilesRequestType{}
GetPythonBinary = &getPythonBinaryRequestType{}
GetCcInfo = &getCcInfoType{}
)
type CcInfo struct {
@@ -28,6 +29,8 @@ type CcInfo struct {
type getOutputFilesRequestType struct{}
type getPythonBinaryRequestType struct{}
// Name returns a string name for this request type. Such request type names must be unique,
// and must only consist of alphanumeric characters.
func (g getOutputFilesRequestType) Name() string {
@@ -53,6 +56,31 @@ func (g getOutputFilesRequestType) ParseResult(rawString string) []string {
return splitOrEmpty(rawString, ", ")
}
// Name returns a string name for this request type. Such request type names must be unique,
// and must only consist of alphanumeric characters.
func (g getPythonBinaryRequestType) Name() string {
return "getPythonBinary"
}
// StarlarkFunctionBody returns a starlark function body to process this request type.
// The returned string is the body of a Starlark function which obtains
// all request-relevant information about a target and returns a string containing
// this information.
// The function should have the following properties:
// - `target` is the only parameter to this function (a configured target).
// - The return value must be a string.
// - The function body should not be indented outside of its own scope.
func (g getPythonBinaryRequestType) StarlarkFunctionBody() string {
return "return providers(target)['FilesToRunProvider'].executable.path"
}
// 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 getPythonBinaryRequestType) ParseResult(rawString string) string {
return rawString
}
type getCcInfoType struct{}
// Name returns a string name for this request type. Such request type names must be unique,

View File

@@ -37,6 +37,31 @@ func TestGetOutputFilesParseResults(t *testing.T) {
}
}
func TestGetPythonBinaryParseResults(t *testing.T) {
testCases := []struct {
description string
input string
expectedOutput string
}{
{
description: "no result",
input: "",
expectedOutput: "",
},
{
description: "one result",
input: "test",
expectedOutput: "test",
},
}
for _, tc := range testCases {
actualOutput := GetPythonBinary.ParseResult(tc.input)
if !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
t.Errorf("%q: expected %#v != actual %#v", tc.description, tc.expectedOutput, actualOutput)
}
}
}
func TestGetCcInfoParseResults(t *testing.T) {
testCases := []struct {
description string