github.com/0xKiwi/rules_go@v0.24.3/tests/core/nogo/deps/deps_test.go (about)

     1  // Copyright 2019 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 deps_test
    16  
    17  import (
    18  	"bytes"
    19  	"regexp"
    20  	"testing"
    21  
    22  	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
    23  )
    24  
    25  func TestMain(m *testing.M) {
    26  	bazel_testing.TestMain(m, bazel_testing.Args{
    27  		Nogo: "@//:nogo",
    28  		Main: `
    29  -- BUILD.bazel --
    30  load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_tool_library", "nogo")
    31  
    32  nogo(
    33      name = "nogo",
    34      deps = [
    35          ":a",
    36          ":b",
    37          ":c",
    38      ],
    39      visibility = ["//visibility:public"],
    40  )
    41  
    42  go_tool_library(
    43      name = "a",
    44      srcs = ["a.go"],
    45      importpath = "a",
    46      deps = [
    47          ":c",
    48          "@org_golang_x_tools//go/analysis:go_tool_library"
    49      ],
    50      visibility = ["//visibility:public"],
    51  )
    52  
    53  go_tool_library(
    54      name = "b",
    55      srcs = ["b.go"],
    56      importpath = "b",
    57      deps = [
    58          ":c",
    59          "@org_golang_x_tools//go/analysis:go_tool_library"
    60      ],
    61      visibility = ["//visibility:public"],
    62  )
    63  
    64  go_tool_library(
    65      name = "c",
    66      srcs = ["c.go"],
    67      importpath = "c",
    68      deps = [
    69          ":d",
    70          "@org_golang_x_tools//go/analysis:go_tool_library"
    71      ],
    72      visibility = ["//visibility:public"],
    73  )
    74  
    75  go_tool_library(
    76      name = "d",
    77      srcs = ["d.go"],
    78      importpath = "d",
    79      deps = ["@org_golang_x_tools//go/analysis:go_tool_library"],
    80      visibility = ["//visibility:public"],
    81  )
    82  
    83  go_library(
    84      name = "src",
    85      srcs = ["src.go"],
    86      importpath = "src",
    87  )
    88  
    89  -- a.go --
    90  package a
    91  
    92  import (
    93  	"c"
    94  	"go/token"
    95  
    96  	"golang.org/x/tools/go/analysis"
    97  )
    98  
    99  var Analyzer = &analysis.Analyzer{
   100  	Name:     "a",
   101  	Doc:      "an analyzer that depends on c.Analyzer",
   102  	Run:      run,
   103  	Requires: []*analysis.Analyzer{c.Analyzer},
   104  }
   105  
   106  func run(pass *analysis.Pass) (interface{}, error) {
   107  	pass.Reportf(token.NoPos, "a %s", pass.ResultOf[c.Analyzer])
   108  	return nil, nil
   109  }
   110  
   111  -- b.go --
   112  package b
   113  
   114  import (
   115  	"c"
   116  	"go/token"
   117  
   118  	"golang.org/x/tools/go/analysis"
   119  )
   120  
   121  var Analyzer = &analysis.Analyzer{
   122  	Name:     "b",
   123  	Doc:      "an analyzer that depends on c.Analyzer",
   124  	Run:      run,
   125  	Requires: []*analysis.Analyzer{c.Analyzer},
   126  }
   127  
   128  func run(pass *analysis.Pass) (interface{}, error) {
   129  	pass.Reportf(token.NoPos, "b %s", pass.ResultOf[c.Analyzer])
   130  	return nil, nil
   131  }
   132  
   133  -- c.go --
   134  package c
   135  
   136  import (
   137  	"d"
   138  	"fmt"
   139  	"go/token"
   140  	"reflect"
   141  
   142  	"golang.org/x/tools/go/analysis"
   143  )
   144  
   145  var Analyzer = &analysis.Analyzer{
   146  	Name:       "c",
   147  	Doc:        "an analyzer that depends on d.Analyzer",
   148  	Run:        run,
   149  	Requires:   []*analysis.Analyzer{d.Analyzer},
   150  	ResultType: reflect.TypeOf(""),
   151  }
   152  
   153  func run(pass *analysis.Pass) (interface{}, error) {
   154  	pass.Reportf(token.NoPos, "only printed once")
   155  	return fmt.Sprintf("c %s", pass.ResultOf[d.Analyzer]), nil
   156  }
   157  
   158  -- d.go --
   159  package d
   160  
   161  import (
   162  	"go/token"
   163  	"reflect"
   164  
   165  	"golang.org/x/tools/go/analysis"
   166  )
   167  
   168  var Analyzer = &analysis.Analyzer{
   169  	Name:       "d",
   170  	Doc:        "an analyzer that does not depend on other analyzers",
   171  	Run:        run,
   172  	ResultType: reflect.TypeOf(""),
   173  }
   174  
   175  func run(pass *analysis.Pass) (interface{}, error) {
   176  	pass.Reportf(token.NoPos, "this should not be printed")
   177  	return "d", nil
   178  }
   179  
   180  -- src.go --
   181  package src
   182  
   183  func Foo() int {
   184  	return 1
   185  }
   186  
   187  `,
   188  	})
   189  }
   190  
   191  func Test(t *testing.T) {
   192  	cmd := bazel_testing.BazelCmd("build", "//:src")
   193  	stderr := &bytes.Buffer{}
   194  	cmd.Stderr = stderr
   195  	if err := cmd.Run(); err == nil {
   196  		t.Fatal("unexpected success")
   197  	}
   198  
   199  	for _, pattern := range []string{
   200  		"a c d",
   201  		"b c d",
   202  		"only printed once",
   203  	} {
   204  		if matched, _ := regexp.Match(pattern, stderr.Bytes()); !matched {
   205  			t.Errorf("output does not contain pattern: %s", pattern)
   206  		}
   207  	}
   208  	if bytes.Contains(stderr.Bytes(), []byte("this should not be printed")) {
   209  		t.Errorf("%q was printed", "this should not be printed")
   210  	}
   211  }