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

     1  // Copyright 2022 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 TestHardcodedUserDirs(t *testing.T) {
    12  	const code = `package main
    13  
    14  import (
    15  	"file/filepath"
    16  
    17  	"go.chromium.org/tast-tests/cros/local/chrome/uiauto/filesapp"
    18  )
    19  
    20  const homeDirMount = "/home/chronos/user"
    21  const (
    22  	downloadsPath = "/home/chronos/user/Downloads"
    23  )
    24  
    25  func main() {
    26  	var x string
    27  	const downloadsPath = "/home/chronos/user"
    28  	x = "/home/chronos/user/Downloads"
    29  	decl := "/home/chronos/user"
    30  	stringArray := []string{"/home/chronos/user"}
    31  	// Appending to string array.
    32  	stringArray = append(stringArray, "/home/chronos/user/Downloads")
    33  	for path := range []string{"/home/chronos/user", "/some/other/path"} {
    34  		fmt.Println(path)
    35  	}
    36  	testFunc("/home/chronos/user")
    37  	fileName := filepath.Join(filesapp.DownloadPath, "test.txt")
    38  	fileName2 := x.(string).DownloadPath
    39  	filePaths := []string{filesapp.MyFilesPath}
    40  
    41  	comment := "/home/chronos/user in strings with whitespace should be ignored"
    42  }
    43  
    44  func testFunc(path string) {
    45  	return path
    46  }
    47  `
    48  	expects := []string{
    49  		"testfile.go:11:18: A reference to the /home/chronos/user bind mount was found which is being deprecated, please use the cryptohome package instead",
    50  		"testfile.go:17:6: A reference to the /home/chronos/user bind mount was found which is being deprecated, please use the cryptohome package instead",
    51  		"testfile.go:21:36: A reference to the /home/chronos/user bind mount was found which is being deprecated, please use the cryptohome package instead",
    52  		"testfile.go:26:28: filesapp.DownloadPath references the /home/chronos/user bind mount which is being deprecated, please use the cryptohome package instead",
    53  		"testfile.go:28:24: filesapp.MyFilesPath references the /home/chronos/user bind mount which is being deprecated, please use the cryptohome package instead",
    54  	}
    55  
    56  	f, fs := parse(code, "testfile.go")
    57  	issues := NoHardcodedUserDirs(fs, f)
    58  	verifyIssues(t, issues, expects)
    59  }