From a05a255eacd58408b38ea1704894736d7d777af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20M=C3=A1rquez=20P=C3=A9rez=20Mu=C3=B1=C3=ADz=20D?= =?UTF-8?q?=C3=ADaz=20P=C3=BAras=20Thaureaux?= Date: Wed, 11 Aug 2021 16:48:30 +0000 Subject: [PATCH] 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 --- android/bazel_handler.go | 27 ++++++++++++++++++++++++-- bazel/cquery/request_type.go | 32 +++++++++++++++++++++++++++++-- bazel/cquery/request_type_test.go | 25 ++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/android/bazel_handler.go b/android/bazel_handler.go index b272daa53..0e7c94480 100644 --- a/android/bazel_handler.go +++ b/android/bazel_handler.go @@ -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") } diff --git a/bazel/cquery/request_type.go b/bazel/cquery/request_type.go index 52c6c2fd9..131f0ecbc 100644 --- a/bazel/cquery/request_type.go +++ b/bazel/cquery/request_type.go @@ -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, diff --git a/bazel/cquery/request_type_test.go b/bazel/cquery/request_type_test.go index 035544e9f..49019abac 100644 --- a/bazel/cquery/request_type_test.go +++ b/bazel/cquery/request_type_test.go @@ -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