github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/forbidden_calls_test.go (about)

     1  // Copyright 2018 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package check
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestForbiddenCalls(t *testing.T) {
    12  	const code = `package main
    13  
    14  import (
    15  	"fmt"
    16  	"time"
    17  
    18  	"github.com/godbus/dbus/v5"
    19  
    20  	"go.chromium.org/tast/core/errors"
    21  )
    22  
    23  func main() {
    24  	fmt.Printf("foo")
    25  	fmt.Errorf("foo")
    26  	errors.Errorf("foo")
    27  	time.Sleep(time.Second)
    28  	context.Background()
    29  	context.TODO()
    30  	dbus.SystemBus()
    31  	dbus.SystemBusPrivate()
    32  	os.Chdir("tmp")
    33  }
    34  `
    35  	expects := []string{
    36  		"testfile.go:14:2: go.chromium.org/tast/core/errors.Errorf should be used instead of fmt.Errorf",
    37  		"testfile.go:16:2: time.Sleep ignores context deadline; use testing.Poll or testing.Sleep instead",
    38  		"testfile.go:17:2: context.Background ignores test timeout; use test function's ctx arg instead",
    39  		"testfile.go:18:2: context.TODO ignores test timeout; use test function's ctx arg instead",
    40  		"testfile.go:19:2: dbus.SystemBus may reorder signals; use go.chromium.org/tast-tests/cros/local/dbusutil.SystemBus instead",
    41  		"testfile.go:20:2: dbus.SystemBusPrivate may reorder signals; use go.chromium.org/tast-tests/cros/local/dbusutil.SystemBusPrivate instead",
    42  		"testfile.go:21:2: os.Chdir changes the shared CWD of the process, reference files using absolute paths.",
    43  	}
    44  
    45  	f, fs := parse(code, "testfile.go")
    46  	issues := ForbiddenCalls(fs, f, false)
    47  	verifyIssues(t, issues, expects)
    48  }
    49  
    50  func TestForbiddenCalls_Exceptions(t *testing.T) {
    51  	const code = `package main
    52  
    53  import (
    54  	"fmt"
    55  	"time"
    56  
    57  	"github.com/godbus/dbus/v5"
    58  
    59  	"go.chromium.org/tast/core/errors"
    60  )
    61  
    62  func main() {
    63  	fmt.Printf("foo")
    64  	fmt.Errorf("foo")
    65  	errors.Errorf("foo")
    66  	time.Sleep(time.Second)
    67  	context.Background()
    68  	context.TODO()
    69  	dbus.SystemBus()
    70  	dbus.SystemBusPrivate()
    71  	os.Chdir("tmp")
    72  }
    73  `
    74  	var expects []string
    75  
    76  	f, fs := parse(code, "src/go.chromium.org/tast-tests/cros/local/bundles/cros/meta/local_timeout.go")
    77  	issues := ForbiddenCalls(fs, f, false)
    78  	verifyIssues(t, issues, expects)
    79  
    80  	f, fs = parse(code, "src/go.chromium.org/tast-tests/cros/remote/bundles/cros/meta/remote_timeout.go")
    81  	issues = ForbiddenCalls(fs, f, false)
    82  	verifyIssues(t, issues, expects)
    83  }
    84  
    85  func TestAutoFixForbiddenCalls(t *testing.T) {
    86  	files := make(map[string]string)
    87  	expects := make(map[string]string)
    88  	const filename1, filename2, filename3, filename4, filename5, filename6, filename7 = "foo.go", "bar.go", "baz.go",
    89  		"dbus.go", "foo1.go", "bar1.go", "baz1.go"
    90  
    91  	files[filename1] = `package main
    92  
    93  import (
    94  	"fmt"
    95  	"time"
    96  
    97  	"github.com/godbus/dbus/v5"
    98  
    99  	"go.chromium.org/tast/core/errors"
   100  )
   101  
   102  func main() {
   103  	fmt.Printf("foo")
   104  	fmt.Errorf("foo") // This is fixable
   105  
   106  	errors.Errorf("foo")
   107  	time.Sleep(time.Second)
   108  	context.Background()
   109  	context.TODO()
   110  	dbus.SystemBus()
   111  	dbus.SystemBusPrivate()
   112  }
   113  `
   114  
   115  	expects[filename1] = `package main
   116  
   117  import (
   118  	"context"
   119  	"fmt"
   120  	"time"
   121  
   122  	"go.chromium.org/tast-tests/cros/local/dbusutil"
   123  	"go.chromium.org/tast/core/errors"
   124  )
   125  
   126  func main() {
   127  	fmt.Printf("foo")
   128  	errors.Errorf("foo") // This is fixable
   129  
   130  	errors.Errorf("foo")
   131  	time.Sleep(time.Second)
   132  	context.Background()
   133  	context.TODO()
   134  	dbusutil.SystemBus()
   135  	dbusutil.SystemBusPrivate()
   136  }
   137  `
   138  	files[filename2] = `package main
   139  
   140  import (
   141  	"fmt"
   142  )
   143  
   144  func main() {
   145  	fmt.Errorf("foo")
   146  	fmt.Println("bar")
   147  }
   148  `
   149  	expects[filename2] = `package main
   150  
   151  import (
   152  	"fmt"
   153  
   154  	"go.chromium.org/tast/core/errors"
   155  )
   156  
   157  func main() {
   158  	errors.Errorf("foo")
   159  	fmt.Println("bar")
   160  }
   161  `
   162  	files[filename3] = `package main
   163  
   164  import (
   165  	"fmt"
   166  )
   167  
   168  func main() {
   169  	fmt.Errorf("foo")
   170  }
   171  `
   172  	expects[filename3] = `package main
   173  
   174  import (
   175  	"go.chromium.org/tast/core/errors"
   176  )
   177  
   178  func main() {
   179  	errors.Errorf("foo")
   180  }
   181  `
   182  	files[filename4] = `package main
   183  
   184  import (
   185  	"github.com/godbus/dbus/v5"
   186  )
   187  
   188  func main() {
   189  	dbus.SystemBus()
   190  	dbus.SystemBusPrivate(dbus.WithHandler(nil))
   191  }
   192  `
   193  
   194  	expects[filename4] = `package main
   195  
   196  import (
   197  	"github.com/godbus/dbus/v5"
   198  
   199  	"go.chromium.org/tast-tests/cros/local/dbusutil"
   200  )
   201  
   202  func main() {
   203  	dbusutil.SystemBus()
   204  	dbusutil.SystemBusPrivate(dbus.WithHandler(nil))
   205  }
   206  `
   207  	files[filename5] = `package main
   208  
   209  import (
   210  	. "go.chromium.org/tast/core/errors"
   211  
   212  	"fmt"
   213  )
   214  
   215  func main() {
   216  	New("import go.chromium.org/tast/core/errors with alias")
   217  	fmt.Errorf("This is not fixable")
   218  }`
   219  	expects[filename5] = `package main
   220  
   221  import (
   222  	. "go.chromium.org/tast/core/errors"
   223  
   224  	"fmt"
   225  )
   226  
   227  func main() {
   228  	New("import go.chromium.org/tast/core/errors with alias")
   229  	fmt.Errorf("This is not fixable")
   230  }
   231  `
   232  	files[filename6] = `package main
   233  
   234  import (
   235  	"fmt"
   236  
   237  	"go.chromium.org/tast/core/errors"
   238  )
   239  
   240  func foo() error {
   241  	return errors.New("foo")
   242  }
   243  
   244  func bar(n int) error {
   245  	return fmt.Errorf("%d", n) // fixable, uses existing import
   246  }`
   247  	expects[filename6] = `package main
   248  
   249  import (
   250  	"go.chromium.org/tast/core/errors"
   251  )
   252  
   253  func foo() error {
   254  	return errors.New("foo")
   255  }
   256  
   257  func bar(n int) error {
   258  	return errors.Errorf("%d", n) // fixable, uses existing import
   259  }`
   260  	files[filename7] = `package main
   261  
   262  import "fmt"
   263  
   264  func errors() bool {
   265  	return false
   266  }
   267  func main() {
   268  	fmt.Errorf("error") // Not fixable
   269  	errors()
   270  }`
   271  	expects[filename7] = `package main
   272  
   273  import "fmt"
   274  
   275  func errors() bool {
   276  	return false
   277  }
   278  func main() {
   279  	fmt.Errorf("error") // Not fixable
   280  	errors()
   281  }`
   282  
   283  	verifyAutoFix(t, ForbiddenCalls, files, expects)
   284  }