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

     1  // Copyright 2021 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 TestDeprecatedAPIs(t *testing.T) {
    12  	deprecated := []*deprecatedAPI{{
    13  		pkg:         "go.chromium.org/tast-tests/cros/local/testexec",
    14  		alternative: "go.chromium.org/tast-tests/cros/common/testexec",
    15  		link:        "https://crbug.com/1119252",
    16  	}, {
    17  		pkg:         "go.chromium.org/tast/core/bundle",
    18  		ident:       "LocalDelegate",
    19  		alternative: "Delegate",
    20  		link:        "https://crbug.com/1134060",
    21  	}, {
    22  		pkg:         "syscall",
    23  		alternative: "golang.org/x/sys/unix",
    24  		exclusion:   map[string]struct{}{"stat_t": {}},
    25  		link:        "https://buganizer.corp.google.com/issues/187787902",
    26  	}}
    27  	const code = `package main
    28  
    29  import (
    30  	b "go.chromium.org/tast/core/bundle"
    31  	"go.chromium.org/tast/core/internal/bundle"
    32  	"go.chromium.org/tast-tests/cros/local/testexec"
    33  	"syscall"
    34  )
    35  
    36  func main() {
    37  	testexec.CommandContext(ctx, "cat")
    38  	f(b.LocalDelegate)
    39  	_ = a.b.LocalDelegate // ok
    40  	f(bundle.LocalDelegate) // ok
    41  	syscall.not_stat_t // not ok
    42  }
    43  `
    44  	want := []string{
    45  		"testfile.go:6:2: package go.chromium.org/tast-tests/cros/local/testexec is deprecated; use go.chromium.org/tast-tests/cros/common/testexec instead",
    46  		"testfile.go:12:4: go.chromium.org/tast/core/bundle.LocalDelegate is deprecated; use Delegate instead",
    47  		"testfile.go:15:2: syscall.not_stat_t is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
    48  	}
    49  
    50  	f, fs := parse(code, "testfile.go")
    51  	issues := deprecatedAPIs(fs, f, deprecated)
    52  	verifyIssues(t, issues, want)
    53  }
    54  
    55  func TestDeprecatedAPIsWithExclusion(t *testing.T) {
    56  	deprecated := []*deprecatedAPI{{
    57  		pkg:         "go.chromium.org/tast-tests/cros/local/testexec",
    58  		alternative: "go.chromium.org/tast-tests/cros/common/testexec",
    59  		link:        "https://crbug.com/1119252",
    60  	}, {
    61  		pkg:         "syscall",
    62  		alternative: "golang.org/x/sys/unix",
    63  		exclusion:   map[string]struct{}{"Stat_t": {}},
    64  		link:        "https://buganizer.corp.google.com/issues/187787902",
    65  	}}
    66  	const code = `package main
    67  
    68  import (
    69  	"go.chromium.org/tast-tests/cros/local/testexec"
    70  	"syscall"
    71  )
    72  
    73  func main() {
    74  	testexec.CommandContext(ctx, "cat")
    75  	syscall.Stat_t // ok
    76  	syscall.SIGSEGV // not ok
    77  }
    78  `
    79  	want := []string{
    80  		"testfile.go:4:2: package go.chromium.org/tast-tests/cros/local/testexec is deprecated; use go.chromium.org/tast-tests/cros/common/testexec instead",
    81  		"testfile.go:11:2: syscall.SIGSEGV is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
    82  	}
    83  
    84  	f, fs := parse(code, "testfile.go")
    85  	issues := deprecatedAPIs(fs, f, deprecated)
    86  	verifyIssues(t, issues, want)
    87  }
    88  
    89  func TestDeprecatedAPIsWithExclusionSameName(t *testing.T) {
    90  	deprecated := []*deprecatedAPI{{
    91  		pkg:         "go.chromium.org/tast-tests/cros/local/testexec",
    92  		alternative: "go.chromium.org/tast-tests/cros/common/testexec",
    93  		link:        "https://crbug.com/1119252",
    94  	}, {
    95  		pkg:         "syscall",
    96  		alternative: "golang.org/x/sys/unix",
    97  		exclusion:   map[string]struct{}{"Stat_t": {}},
    98  		link:        "https://buganizer.corp.google.com/issues/187787902",
    99  	}, {
   100  		pkg:         "syscall2",
   101  		alternative: "golang.org/x/sys/unix",
   102  		link:        "https://buganizer.corp.google.com/issues/187787902",
   103  	}, {
   104  		pkg:         "syscall3",
   105  		alternative: "golang.org/x/sys/unix",
   106  		exclusion: map[string]struct{}{
   107  			"Stat_t":      {},
   108  			"RawConn":     {},
   109  			"Conn":        {},
   110  			"SysProcAttr": {},
   111  			"WaitStatus":  {},
   112  			"Rusage":      {},
   113  			"Credential":  {},
   114  		},
   115  		link: "https://buganizer.corp.google.com/issues/187787902",
   116  	}}
   117  	const code = `package main
   118  
   119  import (
   120  	"go.chromium.org/tast-tests/cros/local/testexec"
   121  	"syscall"
   122  	"syscall2"
   123  	"syscall3"
   124  )
   125  
   126  func main() {
   127  	testexec.CommandContext(ctx, "cat")
   128  	syscall2.stat_t // not ok
   129  	syscall3.stat_t // not ok
   130  	syscall3.Stat_t // ok
   131  	syscall3.rawconn // not ok
   132  	syscall3.RawConn // ok
   133  	syscall3.coNN // not ok
   134  	syscall3.Conn // ok
   135  	syscall3.sysProcAttr // not ok
   136  	syscall3.SysProcAttr // ok
   137  	syscall3.waitStatus // not ok
   138  	syscall3.WaitStatus // ok
   139  	syscall3.rusage // not ok
   140  	syscall3.Rusage // ok
   141  	syscall3.credential // not ok
   142  	syscall3.Credential // ok
   143  }
   144  `
   145  	want := []string{
   146  		"testfile.go:4:2: package go.chromium.org/tast-tests/cros/local/testexec is deprecated; use go.chromium.org/tast-tests/cros/common/testexec instead",
   147  		"testfile.go:6:2: package syscall2 is deprecated; use golang.org/x/sys/unix instead",
   148  		"testfile.go:13:2: syscall3.stat_t is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
   149  		"testfile.go:15:2: syscall3.rawconn is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
   150  		"testfile.go:17:2: syscall3.coNN is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
   151  		"testfile.go:19:2: syscall3.sysProcAttr is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
   152  		"testfile.go:21:2: syscall3.waitStatus is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
   153  		"testfile.go:23:2: syscall3.rusage is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
   154  		"testfile.go:25:2: syscall3.credential is from a deprecated package; use corresponding API in golang.org/x/sys/unix instead",
   155  	}
   156  
   157  	f, fs := parse(code, "testfile.go")
   158  	issues := deprecatedAPIs(fs, f, deprecated)
   159  	verifyIssues(t, issues, want)
   160  }