github.com/bazelbuild/bazel-watcher@v0.25.2/internal/bazel/testing/mock.go (about)

     1  // Copyright 2017 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  
    15  package testing
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"os/exec"
    21  	"regexp"
    22  	"testing"
    23  
    24  	"github.com/bazelbuild/bazel-watcher/third_party/bazel/master/src/main/protobuf/analysis"
    25  	"github.com/bazelbuild/bazel-watcher/third_party/bazel/master/src/main/protobuf/blaze_query"
    26  	"github.com/google/go-cmp/cmp"
    27  )
    28  
    29  type MockBazel struct {
    30  	actions        [][]string
    31  	queryResponse  map[string]*blaze_query.QueryResult
    32  	cqueryResponse map[string]*analysis.CqueryResult
    33  	args           []string
    34  	startupArgs    []string
    35  
    36  	buildError error
    37  	waitError  error
    38  }
    39  
    40  func (b *MockBazel) Args() []string {
    41  	return b.args
    42  }
    43  
    44  func (b *MockBazel) SetArguments(args []string) {
    45  	b.actions = append(b.actions, append([]string{"SetArguments"}, args...))
    46  	b.args = args
    47  }
    48  
    49  func (b *MockBazel) SetStartupArgs(args []string) {
    50  	b.actions = append(b.actions, append([]string{"SetStartupArgs"}, args...))
    51  	b.startupArgs = args
    52  }
    53  
    54  func (b *MockBazel) WriteToStderr(v bool) {
    55  	b.actions = append(b.actions, []string{"WriteToStderr", fmt.Sprint(v)})
    56  }
    57  func (b *MockBazel) WriteToStdout(v bool) {
    58  	b.actions = append(b.actions, []string{"WriteToStdout", fmt.Sprint(v)})
    59  }
    60  func (b *MockBazel) Info() (map[string]string, error) {
    61  	b.actions = append(b.actions, []string{"Info"})
    62  	return map[string]string{}, nil
    63  }
    64  func (b *MockBazel) AddQueryResponse(query string, res *blaze_query.QueryResult) {
    65  	if b.queryResponse == nil {
    66  		b.queryResponse = map[string]*blaze_query.QueryResult{}
    67  	}
    68  	b.queryResponse[query] = res
    69  }
    70  func (b *MockBazel) Query(args ...string) (*blaze_query.QueryResult, error) {
    71  	b.actions = append(b.actions, append([]string{"Query"}, args...))
    72  	query := args[0]
    73  	res, ok := b.queryResponse[query]
    74  
    75  	if !ok {
    76  		var candidates []string
    77  		for candidate := range b.queryResponse {
    78  			candidates = append(candidates, candidate)
    79  		}
    80  		panic(fmt.Sprintf("Unable to find query result for %q. Only have %v.", query, candidates))
    81  	}
    82  
    83  	return res, nil
    84  }
    85  func (b *MockBazel) AddCQueryResponse(query string, res *analysis.CqueryResult) {
    86  	if b.cqueryResponse == nil {
    87  		b.cqueryResponse = map[string]*analysis.CqueryResult{}
    88  	}
    89  	b.cqueryResponse[query] = res
    90  }
    91  func (b *MockBazel) CQuery(args ...string) (*analysis.CqueryResult, error) {
    92  	b.actions = append(b.actions, append([]string{"CQuery"}, args...))
    93  	query := args[0]
    94  	res, ok := b.cqueryResponse[query]
    95  
    96  	if !ok {
    97  		var candidates []string
    98  		for candidate := range b.cqueryResponse {
    99  			candidates = append(candidates, candidate)
   100  		}
   101  		panic(fmt.Sprintf("Unable to find cquery result for %q. Only have %v.", query, candidates))
   102  	}
   103  
   104  	return res, nil
   105  }
   106  func (b *MockBazel) Build(args ...string) (*bytes.Buffer, error) {
   107  	b.actions = append(b.actions, append([]string{"Build"}, args...))
   108  	return nil, b.buildError
   109  }
   110  func (b *MockBazel) BuildError(e error) {
   111  	b.buildError = e
   112  }
   113  func (b *MockBazel) Test(args ...string) (*bytes.Buffer, error) {
   114  	b.actions = append(b.actions, append([]string{"Test"}, args...))
   115  	return nil, nil
   116  }
   117  func (b *MockBazel) Run(args ...string) (*exec.Cmd, *bytes.Buffer, error) {
   118  	b.actions = append(b.actions, append([]string{"Run"}, args...))
   119  	return nil, nil, nil
   120  }
   121  func (b *MockBazel) WaitError(e error) {
   122  	b.waitError = e
   123  }
   124  func (b *MockBazel) Wait() error {
   125  	return b.waitError
   126  }
   127  func (b *MockBazel) Cancel() {
   128  	b.actions = append(b.actions, []string{"Cancel"})
   129  }
   130  func (b *MockBazel) AssertActions(t *testing.T, expected [][]string) {
   131  	t.Helper()
   132  
   133  	if diff := cmp.Diff(b.actions, expected, cmp.FilterValues(func(a, b string) bool {
   134  		return true
   135  	}, cmp.Comparer(func(a, b string) bool {
   136  		{
   137  			match, _ := regexp.MatchString(a, b)
   138  			if match {
   139  				return true
   140  			}
   141  		}
   142  		{
   143  			match, _ := regexp.MatchString(b, a)
   144  			if match {
   145  				return true
   146  			}
   147  		}
   148  		return a == b
   149  	}))); diff != "" {
   150  		t.Errorf("Action diff (-got (%d),+want (%d)):\n%s", len(b.actions), len(expected), diff)
   151  	}
   152  }
   153  
   154  func max(a, b int) int {
   155  	if a > b {
   156  		return a
   157  	}
   158  	return b
   159  }