xref: /aosp_15_r20/external/skia/bazel/exporter/util_test.go (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1// Copyright 2022 Google LLC
2//
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
6package exporter
7
8import (
9	"go.skia.org/infra/go/skerr"
10	"go.skia.org/skia/bazel/exporter/build_proto/analysis_v2"
11	"go.skia.org/skia/bazel/exporter/build_proto/build"
12	"google.golang.org/protobuf/encoding/prototext"
13	"google.golang.org/protobuf/proto"
14)
15
16// A test helper function to convert a textproto protobuf into a binary protobuf.
17func textProtoToProtobuf(textProto string) ([]byte, error) {
18	qr := analysis_v2.CqueryResult{}
19	err := prototext.Unmarshal([]byte(textProto), &qr)
20	if err != nil {
21		return nil, skerr.Wrapf(err, "unable to unmarshal textproto")
22	}
23
24	return proto.Marshal(&qr)
25}
26
27// A test helper function to create a build.Rule with given properties (and srcs).
28func createTestBuildRule(name, ruleClass, loc string, srcs []string) *build.Rule {
29	srcsAttrName := "srcs"
30	ad := build.Attribute_STRING_LIST
31	attr := build.Attribute{Name: &srcsAttrName, Type: &ad, StringListValue: srcs}
32
33	r := build.Rule{Name: &name, RuleClass: &ruleClass, Location: &loc}
34	r.Attribute = append(r.Attribute, &attr)
35	return &r
36}
37