xref: /aosp_15_r20/external/bazelbuild-rules_android/src/tools/ak/bucketize/pipe_test.go (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bucketize
16
17import (
18	"context"
19	"errors"
20	"reflect"
21	"testing"
22)
23
24func TestPrefixErr(t *testing.T) {
25	tests := []struct {
26		ctx  context.Context
27		fmts string
28		args []interface{}
29		want error
30	}{
31		{
32			ctx:  context.Background(),
33			fmts: "Hello world",
34			want: errors.New("Hello world"),
35		},
36		{
37			ctx:  prefixErr(context.Background(), "file: foo: "),
38			fmts: "Hello world: %d",
39			args: []interface{}{1},
40			want: errors.New("file: foo: Hello world: 1"),
41		},
42		{
43			ctx:  prefixErr(prefixErr(context.Background(), "file: foo: "), "tag: <resources>: "),
44			fmts: "Hello world: %d",
45			args: []interface{}{1},
46			want: errors.New("file: foo: tag: <resources>: Hello world: 1"),
47		},
48	}
49	for _, tc := range tests {
50		got := errorf(tc.ctx, tc.fmts, tc.args...)
51		if !reflect.DeepEqual(got, tc.want) {
52			t.Errorf("Errorf(%v, %v, %v): %v wanted %v", tc.ctx, tc.fmts, tc.args, got, tc.want)
53		}
54	}
55}
56
57func TestMergeErrStreams(t *testing.T) {
58	ctx := context.Background()
59	sendClose := func(e error, eC chan<- error) {
60		defer close(eC)
61		eC <- e
62	}
63	in1 := make(chan error)
64	in2 := make(chan error)
65	go sendClose(errors.New("hi"), in1)
66	go sendClose(errors.New("hello"), in2)
67	merged := mergeErrStreams(ctx, []<-chan error{in1, in2})
68	var rcv []error
69	for r := range merged {
70		rcv = append(rcv, r)
71	}
72	if len(rcv) != 2 {
73		t.Errorf("got: %v on merged stream, wanted only 2 elements", rcv)
74	}
75}
76