aboutsummaryrefslogtreecommitdiff
path: root/tests/core/nogo/custom
diff options
context:
space:
mode:
Diffstat (limited to 'tests/core/nogo/custom')
-rw-r--r--tests/core/nogo/custom/BUILD.bazel6
-rw-r--r--tests/core/nogo/custom/README.rst16
-rw-r--r--tests/core/nogo/custom/custom_test.go510
-rw-r--r--tests/core/nogo/custom/flags/BUILD.bazel6
-rw-r--r--tests/core/nogo/custom/flags/README.rst19
-rw-r--r--tests/core/nogo/custom/flags/flags_test.go262
6 files changed, 819 insertions, 0 deletions
diff --git a/tests/core/nogo/custom/BUILD.bazel b/tests/core/nogo/custom/BUILD.bazel
new file mode 100644
index 00000000..06317a0b
--- /dev/null
+++ b/tests/core/nogo/custom/BUILD.bazel
@@ -0,0 +1,6 @@
+load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")
+
+go_bazel_test(
+ name = "custom_test",
+ srcs = ["custom_test.go"],
+)
diff --git a/tests/core/nogo/custom/README.rst b/tests/core/nogo/custom/README.rst
new file mode 100644
index 00000000..285c8ff6
--- /dev/null
+++ b/tests/core/nogo/custom/README.rst
@@ -0,0 +1,16 @@
+Custom nogo analyzers
+=====================
+
+.. _nogo: /go/nogo.rst
+.. _go_library: /docs/go/core/rules.md#_go_library
+
+Tests to ensure that custom `nogo`_ analyzers run and detect errors.
+
+.. contents::
+
+custom_test
+-----------
+Verifies that custom analyzers print errors and fail a `go_library`_ build when
+a configuration file is not provided, and that analyzers with the same package
+name do not conflict. Also checks that custom analyzers can be configured to
+apply only to certain file paths using a custom configuration file.
diff --git a/tests/core/nogo/custom/custom_test.go b/tests/core/nogo/custom/custom_test.go
new file mode 100644
index 00000000..27624431
--- /dev/null
+++ b/tests/core/nogo/custom/custom_test.go
@@ -0,0 +1,510 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package custom_test
+
+import (
+ "bytes"
+ "fmt"
+ "io/ioutil"
+ "regexp"
+ "testing"
+
+ "github.com/bazelbuild/rules_go/go/tools/bazel_testing"
+)
+
+const origConfig = `# config = "",`
+
+func TestMain(m *testing.M) {
+ bazel_testing.TestMain(m, bazel_testing.Args{
+ Nogo: "@//:nogo",
+ Main: `
+-- BUILD.bazel --
+load("@io_bazel_rules_go//go:def.bzl", "go_library", "nogo")
+
+nogo(
+ name = "nogo",
+ deps = [
+ ":foofuncname",
+ ":importfmt",
+ ":visibility",
+ ],
+ # config = "",
+ visibility = ["//visibility:public"],
+)
+
+go_library(
+ name = "importfmt",
+ srcs = ["importfmt.go"],
+ importpath = "importfmtanalyzer",
+ deps = ["@org_golang_x_tools//go/analysis"],
+ visibility = ["//visibility:public"],
+)
+
+go_library(
+ name = "foofuncname",
+ srcs = ["foofuncname.go"],
+ importpath = "foofuncanalyzer",
+ deps = ["@org_golang_x_tools//go/analysis"],
+ visibility = ["//visibility:public"],
+)
+
+go_library(
+ name = "visibility",
+ srcs = ["visibility.go"],
+ importpath = "visibilityanalyzer",
+ deps = [
+ "@org_golang_x_tools//go/analysis",
+ "@org_golang_x_tools//go/ast/inspector",
+ ],
+ visibility = ["//visibility:public"],
+)
+
+go_library(
+ name = "has_errors",
+ srcs = ["has_errors.go"],
+ importpath = "haserrors",
+ deps = [":dep"],
+)
+
+go_library(
+ name = "has_errors_linedirective",
+ srcs = ["has_errors_linedirective.go"],
+ importpath = "haserrors_linedirective",
+ deps = [":dep"],
+)
+
+go_library(
+ name = "uses_cgo_with_errors",
+ srcs = [
+ "examplepkg/uses_cgo_clean.go",
+ "examplepkg/pure_src_with_err_calling_native.go",
+ ],
+ importpath = "examplepkg",
+ cgo = True,
+)
+
+go_library(
+ name = "no_errors",
+ srcs = ["no_errors.go"],
+ importpath = "noerrors",
+ deps = [":dep"],
+)
+
+go_library(
+ name = "dep",
+ srcs = ["dep.go"],
+ importpath = "dep",
+)
+
+-- foofuncname.go --
+// importfmt checks for functions named "Foo".
+// It has the same package name as another check to test the checks with
+// the same package name do not conflict.
+package importfmt
+
+import (
+ "go/ast"
+
+ "golang.org/x/tools/go/analysis"
+)
+
+const doc = "report calls of functions named \"Foo\"\n\nThe foofuncname analyzer reports calls to functions that are\nnamed \"Foo\"."
+
+var Analyzer = &analysis.Analyzer{
+ Name: "foofuncname",
+ Run: run,
+ Doc: doc,
+}
+
+func run(pass *analysis.Pass) (interface{}, error) {
+ for _, f := range pass.Files {
+ // TODO(samueltan): use package inspector once the latest golang.org/x/tools
+ // changes are pulled into this branch (see #1755).
+ ast.Inspect(f, func(n ast.Node) bool {
+ switch n := n.(type) {
+ case *ast.FuncDecl:
+ if n.Name.Name == "Foo" {
+ pass.Reportf(n.Pos(), "function must not be named Foo")
+ }
+ return true
+ }
+ return true
+ })
+ }
+ return nil, nil
+}
+
+-- importfmt.go --
+// importfmt checks for the import of package fmt.
+package importfmt
+
+import (
+ "go/ast"
+ "strconv"
+
+ "golang.org/x/tools/go/analysis"
+)
+
+const doc = "report imports of package fmt\n\nThe importfmt analyzer reports imports of package fmt."
+
+var Analyzer = &analysis.Analyzer{
+ Name: "importfmt",
+ Run: run,
+ Doc: doc,
+}
+
+func run(pass *analysis.Pass) (interface{}, error) {
+ for _, f := range pass.Files {
+ // TODO(samueltan): use package inspector once the latest golang.org/x/tools
+ // changes are pulled into this branch (see #1755).
+ ast.Inspect(f, func(n ast.Node) bool {
+ switch n := n.(type) {
+ case *ast.ImportSpec:
+ if path, _ := strconv.Unquote(n.Path.Value); path == "fmt" {
+ pass.Reportf(n.Pos(), "package fmt must not be imported")
+ }
+ return true
+ }
+ return true
+ })
+ }
+ return nil, nil
+}
+
+-- visibility.go --
+// visibility looks for visibility annotations on functions and
+// checks they are only called from packages allowed to call them.
+package visibility
+
+import (
+ "encoding/gob"
+ "go/ast"
+ "regexp"
+
+ "golang.org/x/tools/go/analysis"
+ "golang.org/x/tools/go/ast/inspector"
+)
+
+var Analyzer = &analysis.Analyzer{
+ Name: "visibility",
+ Run: run,
+ Doc: "enforce visibility requirements for functions\n\nThe visibility analyzer reads visibility annotations on functions and\nchecks that packages that call those functions are allowed to do so.",
+ FactTypes: []analysis.Fact{(*VisibilityFact)(nil)},
+}
+
+type VisibilityFact struct {
+ Paths []string
+}
+
+func (_ *VisibilityFact) AFact() {} // dummy method to satisfy interface
+
+func init() { gob.Register((*VisibilityFact)(nil)) }
+
+var visibilityRegexp = regexp.MustCompile("visibility:([^\\s]+)")
+
+func run(pass *analysis.Pass) (interface{}, error) {
+ in := inspector.New(pass.Files)
+
+ // Find visibility annotations on function declarations.
+ in.Nodes([]ast.Node{(*ast.FuncDecl)(nil)}, func(n ast.Node, push bool) (prune bool) {
+ if !push {
+ return false
+ }
+
+ fn := n.(*ast.FuncDecl)
+
+ if fn.Doc == nil {
+ return true
+ }
+ obj := pass.TypesInfo.ObjectOf(fn.Name)
+ if obj == nil {
+ return true
+ }
+ doc := fn.Doc.Text()
+
+ if matches := visibilityRegexp.FindAllStringSubmatch(doc, -1); matches != nil {
+ fact := &VisibilityFact{Paths: make([]string, len(matches))}
+ for i, m := range matches {
+ fact.Paths[i] = m[1]
+ }
+ pass.ExportObjectFact(obj, fact)
+ }
+
+ return true
+ })
+
+ // Find calls that may be affected by visibility declarations.
+ in.Nodes([]ast.Node{(*ast.CallExpr)(nil)}, func(n ast.Node, push bool) (prune bool) {
+ if !push {
+ return false
+ }
+
+ callee, ok := n.(*ast.CallExpr).Fun.(*ast.SelectorExpr)
+ if !ok {
+ return false
+ }
+ obj := pass.TypesInfo.ObjectOf(callee.Sel)
+ if obj == nil {
+ return false
+ }
+ var fact VisibilityFact
+ if ok := pass.ImportObjectFact(obj, &fact); !ok {
+ return false
+ }
+ visible := false
+ for _, path := range fact.Paths {
+ if path == pass.Pkg.Path() {
+ visible = true
+ break
+ }
+ }
+ if !visible {
+ pass.Reportf(callee.Pos(), "function %s is not visible in this package", callee.Sel.Name)
+ }
+
+ return false
+ })
+
+ return nil, nil
+}
+
+-- config.json --
+{
+ "importfmt": {
+ "only_files": {
+ "has_errors\\.go": ""
+ }
+ },
+ "foofuncname": {
+ "description": "no exemptions since we know this check is 100% accurate"
+ },
+ "visibility": {
+ "exclude_files": {
+ "has_.*\\.go": "special exception to visibility rules"
+ }
+ }
+}
+
+-- baseconfig.json --
+{
+ "_base": {
+ "exclude_files": {
+ "has_.*\\.go": "Visibility analyzer not specified. Still inherits this special exception."
+ }
+ },
+ "importfmt": {
+ "only_files": {
+ "has_errors\\.go": ""
+ }
+ },
+ "foofuncname": {
+ "description": "no exemptions since we know this check is 100% accurate, so override base config",
+ "exclude_files": {}
+ }
+}
+
+-- has_errors.go --
+package haserrors
+
+import (
+ _ "fmt" // This should fail importfmt
+
+ "dep"
+)
+
+func Foo() bool { // This should fail foofuncname
+ dep.D() // This should fail visibility
+ return true
+}
+
+-- has_errors_linedirective.go --
+//line linedirective.go:1
+package haserrors_linedirective
+
+import (
+ /*line linedirective_importfmt.go:4*/ _ "fmt" // This should fail importfmt
+
+ "dep"
+)
+
+//line linedirective_foofuncname.go:9
+func Foo() bool { // This should fail foofuncname
+//line linedirective_visibility.go:10
+ dep.D() // This should fail visibility
+ return true
+}
+
+-- no_errors.go --
+// package noerrors contains no analyzer errors.
+package noerrors
+
+import "dep"
+
+func Baz() int {
+ dep.D()
+ return 1
+}
+
+-- dep.go --
+package dep
+
+// visibility:noerrors
+func D() {
+}
+
+-- examplepkg/uses_cgo_clean.go --
+package examplepkg
+
+// #include <stdlib.h>
+import "C"
+
+func Bar() bool {
+ if C.rand() > 10 {
+ return true
+ }
+ return false
+}
+
+-- examplepkg/pure_src_with_err_calling_native.go --
+package examplepkg
+
+func Foo() bool { // This should fail foofuncname
+ return Bar()
+}
+
+`,
+ })
+}
+
+func Test(t *testing.T) {
+ for _, test := range []struct {
+ desc, config, target string
+ wantSuccess bool
+ includes, excludes []string
+ }{
+ {
+ desc: "default_config",
+ target: "//:has_errors",
+ wantSuccess: false,
+ includes: []string{
+ `has_errors.go:.*package fmt must not be imported \(importfmt\)`,
+ `has_errors.go:.*function must not be named Foo \(foofuncname\)`,
+ `has_errors.go:.*function D is not visible in this package \(visibility\)`,
+ },
+ }, {
+ desc: "default_config_linedirective",
+ target: "//:has_errors_linedirective",
+ wantSuccess: false,
+ includes: []string{
+ `linedirective_importfmt.go:.*package fmt must not be imported \(importfmt\)`,
+ `linedirective_foofuncname.go:.*function must not be named Foo \(foofuncname\)`,
+ `linedirective_visibility.go:.*function D is not visible in this package \(visibility\)`,
+ },
+ }, {
+ desc: "custom_config",
+ config: "config.json",
+ target: "//:has_errors",
+ wantSuccess: false,
+ includes: []string{
+ `has_errors.go:.*package fmt must not be imported \(importfmt\)`,
+ `has_errors.go:.*function must not be named Foo \(foofuncname\)`,
+ },
+ excludes: []string{
+ `visib`,
+ },
+ }, {
+ desc: "custom_config_linedirective",
+ config: "config.json",
+ target: "//:has_errors_linedirective",
+ wantSuccess: false,
+ includes: []string{
+ `linedirective_foofuncname.go:.*function must not be named Foo \(foofuncname\)`,
+ `linedirective_visibility.go:.*function D is not visible in this package \(visibility\)`,
+ },
+ excludes: []string{
+ `importfmt`,
+ },
+ }, {
+ desc: "custom_config_with_base_linedirective",
+ config: "baseconfig.json",
+ target: "//:has_errors_linedirective",
+ wantSuccess: false,
+ includes: []string{
+ `linedirective_foofuncname.go:.*function must not be named Foo \(foofuncname\)`,
+ `linedirective_visibility.go:.*function D is not visible in this package \(visibility\)`,
+ },
+ excludes: []string{
+ `importfmt`,
+ },
+ }, {
+ desc: "uses_cgo_with_errors",
+ config: "config.json",
+ target: "//:uses_cgo_with_errors",
+ wantSuccess: false,
+ includes: []string{
+ // note the cross platform regex :)
+ `.*[\\/]cgo[\\/]examplepkg[\\/]pure_src_with_err_calling_native.go:.*function must not be named Foo \(foofuncname\)`,
+ },
+ }, {
+ desc: "no_errors",
+ target: "//:no_errors",
+ wantSuccess: true,
+ excludes: []string{"no_errors.go"},
+ },
+ } {
+ t.Run(test.desc, func(t *testing.T) {
+ if test.config != "" {
+ customConfig := fmt.Sprintf("config = %q,", test.config)
+ if err := replaceInFile("BUILD.bazel", origConfig, customConfig); err != nil {
+ t.Fatal(err)
+ }
+ defer replaceInFile("BUILD.bazel", customConfig, origConfig)
+ }
+
+ cmd := bazel_testing.BazelCmd("build", test.target)
+ stderr := &bytes.Buffer{}
+ cmd.Stderr = stderr
+ if err := cmd.Run(); err == nil && !test.wantSuccess {
+ t.Fatal("unexpected success")
+ } else if err != nil && test.wantSuccess {
+ t.Fatalf("unexpected error: %v", err)
+ }
+
+ for _, pattern := range test.includes {
+ if matched, err := regexp.Match(pattern, stderr.Bytes()); err != nil {
+ t.Fatal(err)
+ } else if !matched {
+ t.Errorf("got output:\n %s\n which does not contain pattern: %s", string(stderr.Bytes()), pattern)
+ }
+ }
+ for _, pattern := range test.excludes {
+ if matched, err := regexp.Match(pattern, stderr.Bytes()); err != nil {
+ t.Fatal(err)
+ } else if matched {
+ t.Errorf("output contained pattern: %s", pattern)
+ }
+ }
+ })
+ }
+}
+
+func replaceInFile(path, old, new string) error {
+ data, err := ioutil.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ data = bytes.ReplaceAll(data, []byte(old), []byte(new))
+ return ioutil.WriteFile(path, data, 0666)
+}
diff --git a/tests/core/nogo/custom/flags/BUILD.bazel b/tests/core/nogo/custom/flags/BUILD.bazel
new file mode 100644
index 00000000..cdf4c76a
--- /dev/null
+++ b/tests/core/nogo/custom/flags/BUILD.bazel
@@ -0,0 +1,6 @@
+load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")
+
+go_bazel_test(
+ name = "flags_test",
+ srcs = ["flags_test.go"],
+)
diff --git a/tests/core/nogo/custom/flags/README.rst b/tests/core/nogo/custom/flags/README.rst
new file mode 100644
index 00000000..e525250b
--- /dev/null
+++ b/tests/core/nogo/custom/flags/README.rst
@@ -0,0 +1,19 @@
+Custom nogo analyzer flags
+=====================
+
+.. _nogo: /go/nogo.rst
+.. _go_library: /docs/go/core/rules.md#_go_library
+
+Tests to ensure that custom `nogo`_ analyzers that consume flags can be
+supplied those flags via nono config.
+
+.. contents::
+
+flags_test
+-----------
+Verifies that a simple custom analyzer's behavior can be modified by setting
+its analyzer flags in the nogo driver, and that these flags can be provided to
+the driver via the nogo config `analyzer_flags` field. Also checks that
+invalid flags as defined by the `flag` package cause the driver to immediately
+return an error.
+
diff --git a/tests/core/nogo/custom/flags/flags_test.go b/tests/core/nogo/custom/flags/flags_test.go
new file mode 100644
index 00000000..7381a3f0
--- /dev/null
+++ b/tests/core/nogo/custom/flags/flags_test.go
@@ -0,0 +1,262 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package flags_test
+
+import (
+ "bytes"
+ "fmt"
+ "io/ioutil"
+ "regexp"
+ "testing"
+
+ "github.com/bazelbuild/rules_go/go/tools/bazel_testing"
+)
+
+const origConfig = `# config = "",`
+
+func TestMain(m *testing.M) {
+ bazel_testing.TestMain(m, bazel_testing.Args{
+ Nogo: "@//:nogo",
+ Main: `
+-- BUILD.bazel --
+load("@io_bazel_rules_go//go:def.bzl", "go_library", "nogo")
+
+nogo(
+ name = "nogo",
+ deps = [
+ ":flagger",
+ ],
+ # config = "",
+ visibility = ["//visibility:public"],
+)
+
+go_library(
+ name = "flagger",
+ srcs = ["flagger.go"],
+ importpath = "flaggeranalyzer",
+ deps = [
+ "@org_golang_x_tools//go/analysis",
+ ],
+ visibility = ["//visibility:public"],
+)
+
+go_library(
+ name = "some_file",
+ srcs = ["some_file.go"],
+ importpath = "somefile",
+ deps = [":dep"],
+)
+
+go_library(
+ name = "dep",
+ srcs = ["dep.go"],
+ importpath = "dep",
+)
+
+-- flagger.go --
+// flagger crashes when three flags are set in the config or else it no-ops
+package flagger
+
+import (
+ "errors"
+
+ "golang.org/x/tools/go/analysis"
+)
+
+var (
+ boolSwitch bool
+ stringSwitch string
+ intSwitch int
+)
+
+var Analyzer = &analysis.Analyzer{
+ Name: "flagger",
+ Run: run,
+ Doc: "Dummy analyzer that crashes when all its flags are set correctly",
+}
+
+func init() {
+ Analyzer.Flags.BoolVar(&boolSwitch, "bool-switch", false, "Bool must be set to true to run")
+ Analyzer.Flags.StringVar(&stringSwitch, "string-switch", "no", "String must be set to \"yes\" to run")
+ Analyzer.Flags.IntVar(&intSwitch, "int-switch", 0, "Int must be set to 1 to run")
+}
+
+func run(pass *analysis.Pass) (interface{}, error) {
+ if !boolSwitch {
+ return nil, nil
+ }
+ if stringSwitch != "yes" {
+ return nil, nil
+ }
+ if intSwitch != 1 {
+ return nil, nil
+ }
+ return nil, errors.New("all switches were set -> fail")
+}
+
+-- all_flags_set.json --
+{
+ "flagger": {
+ "description": "this will crash on every file",
+ "analyzer_flags": {
+ "bool-switch": "true",
+ "int-switch": "1",
+ "string-switch": "yes"
+ }
+ }
+}
+
+-- two_flags_set.json --
+{
+ "flagger": {
+ "description": "this will succeed on every file",
+ "analyzer_flags": {
+ "bool-switch": "true",
+ "int-switch": "1"
+ }
+ }
+}
+
+-- invalid_int.json --
+{
+ "flagger": {
+ "description": "this will crash immediately due to an invalid int flag",
+ "analyzer_flags": {
+ "int-switch": "one",
+ "string-switch": "yes"
+ }
+ }
+}
+
+-- nonexistent_flag.json --
+{
+ "flagger": {
+ "description": "this will crash immediately due to a nonexistent flag",
+ "analyzer_flags": {
+ "int-switch": "1",
+ "bool-switch": "true",
+ "string-switch": "yes",
+ "description": "This is a good analyzer"
+ }
+ }
+}
+
+-- hyphenated_flag.json --
+{
+ "flagger": {
+ "description": "this will crash immediately due to a hyphenated flag",
+ "analyzer_flags": {
+ "-int-switch": "1"
+ }
+ }
+}
+
+-- some_file.go --
+// package somefile contains a file and has a dep
+package somefile
+
+import "dep"
+
+func Baz() int {
+ dep.D()
+ return 1
+}
+
+-- dep.go --
+package dep
+
+func D() {
+}
+
+`,
+ })
+}
+
+func Test(t *testing.T) {
+ for _, test := range []struct {
+ desc, config string
+ wantSuccess bool
+ includes, excludes []string
+ }{
+ {
+ desc: "config_flags_triggering_error",
+ wantSuccess: false,
+ config: "all_flags_set.json",
+ includes: []string{"all switches were set -> fail"},
+ }, {
+ desc: "config_flags_triggering_success",
+ wantSuccess: true,
+ config: "two_flags_set.json",
+ }, {
+ desc: "invalid_int_triggering_error",
+ wantSuccess: false,
+ config: "invalid_int.json",
+ includes: []string{"flagger: invalid value for flag: int-switch=one"},
+ }, {
+ desc: "nonexistent_flag_triggering_error",
+ wantSuccess: false,
+ config: "nonexistent_flag.json",
+ includes: []string{"flagger: unrecognized flag: description"},
+ }, {
+ desc: "hyphenated_flag_triggering_error",
+ wantSuccess: false,
+ config: "hyphenated_flag.json",
+ includes: []string{"flagger: flag should not begin with '-': -int-switch"},
+ },
+ } {
+ t.Run(test.desc, func(t *testing.T) {
+ if test.config != "" {
+ customConfig := fmt.Sprintf("config = %q,", test.config)
+ if err := replaceInFile("BUILD.bazel", origConfig, customConfig); err != nil {
+ t.Fatal(err)
+ }
+ defer replaceInFile("BUILD.bazel", customConfig, origConfig)
+ }
+
+ cmd := bazel_testing.BazelCmd("build", "//:some_file")
+ stderr := &bytes.Buffer{}
+ cmd.Stderr = stderr
+ if err := cmd.Run(); err == nil && !test.wantSuccess {
+ t.Fatal("unexpected success")
+ } else if err != nil && test.wantSuccess {
+ t.Fatalf("unexpected error: %v", err)
+ }
+
+ for _, pattern := range test.includes {
+ if matched, err := regexp.Match(pattern, stderr.Bytes()); err != nil {
+ t.Fatal(err)
+ } else if !matched {
+ t.Errorf("got output:\n %s\n which does not contain pattern: %s", string(stderr.Bytes()), pattern)
+ }
+ }
+ for _, pattern := range test.excludes {
+ if matched, err := regexp.Match(pattern, stderr.Bytes()); err != nil {
+ t.Fatal(err)
+ } else if matched {
+ t.Errorf("output contained pattern: %s", pattern)
+ }
+ }
+ })
+ }
+}
+
+func replaceInFile(path, old, new string) error {
+ data, err := ioutil.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ data = bytes.ReplaceAll(data, []byte(old), []byte(new))
+ return ioutil.WriteFile(path, data, 0666)
+}