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

     1  // Copyright 2023 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  	"go/ast"
     9  	"go/token"
    10  	"strings"
    11  )
    12  
    13  // ForbiddenFlashromSubprocess checks if "flashrom" command is called.
    14  func ForbiddenFlashromSubprocess(fs *token.FileSet, f *ast.File) []*Issue {
    15  	var issues []*Issue
    16  
    17  	const (
    18  		localPkg  = "go.chromium.org/tast-tests/cros/local"
    19  		remotePkg = "go.chromium.org/tast-tests/cros/remote"
    20  	)
    21  
    22  	path := fs.File(f.Pos()).Name()
    23  	localTests := strings.Contains(path, localPkg)
    24  	remoteTests := strings.Contains(path, remotePkg)
    25  
    26  	// We only checks the changes in local test or remote test.
    27  	if !localTests && !remoteTests {
    28  		return issues
    29  	}
    30  
    31  	ast.Inspect(f, func(n ast.Node) bool {
    32  		if callExpr, ok := n.(*ast.CallExpr); ok {
    33  			isForbidden(callExpr, &issues, fs)
    34  		}
    35  		return true
    36  	})
    37  
    38  	return issues
    39  }
    40  
    41  func isForbidden(expr ast.Expr, issues *[]*Issue, fs *token.FileSet) {
    42  	v, _ := expr.(*ast.CallExpr)
    43  	if selector, ok := v.Fun.(*ast.SelectorExpr); ok {
    44  		for _, rule := range []struct {
    45  			commandName  string
    46  			parameterIdx int // 0 based idx
    47  		}{
    48  			{
    49  				commandName:  "CommandContext",
    50  				parameterIdx: 1,
    51  			},
    52  		} {
    53  			if selector.Sel.Name == rule.commandName {
    54  				if len(v.Args) > rule.parameterIdx {
    55  					arg, ok := v.Args[rule.parameterIdx].(*ast.BasicLit)
    56  					if ok && arg.Kind == token.STRING {
    57  						if arg.Value == `"flashrom"` || arg.Value == `"/usr/sbin/flashrom"` {
    58  							*issues = append(*issues, &Issue{
    59  								Pos:  fs.Position(v.Pos()),
    60  								Msg:  "Please don't use flashrom subprocess but use flashrom_library instead.",
    61  								Link: "https://chromium.googlesource.com/chromiumos/platform/tast-tests/+/HEAD/src/go.chromium.org/tast-tests/cros/common/flashrom/",
    62  							})
    63  						}
    64  					}
    65  
    66  				}
    67  			}
    68  		}
    69  	}
    70  }