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

     1  // Copyright 2020 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  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func TestTestLevelVarFile_OK(t *testing.T) {
    13  	const (
    14  		path = "vars/foo.Bar.yaml"
    15  		data = `foo.Bar.baz: 1
    16  foo.Bar.qux: a
    17  foo.Bar.empty:`
    18  	)
    19  
    20  	var expects []string
    21  
    22  	issues := SecretVarFile(path, []byte(data))
    23  	verifyIssues(t, issues, expects)
    24  }
    25  
    26  func TestCategoryLevelVarFile_OK(t *testing.T) {
    27  	const (
    28  		path = "vars/foo.yaml"
    29  		data = `foo.baz: 1
    30  foo.qux: a
    31  foo.empty:`
    32  	)
    33  
    34  	// The import order is good, so no issue.
    35  	var expects []string
    36  	issues := SecretVarFile(path, []byte(data))
    37  	verifyIssues(t, issues, expects)
    38  }
    39  
    40  const invalidVarNameErrTmpl = "%s: Var name %s violates naming convention; it should match %s[A-Za-z][A-Za-z0-9_]*"
    41  
    42  func TestTestLevelVarFile(t *testing.T) {
    43  	const path = "vars/a.B.yaml"
    44  	for _, tc := range []struct {
    45  		name  string
    46  		valid bool
    47  	}{
    48  		{"a.B", false},
    49  		{"a.B.", false},
    50  		{"a.B.1", false},
    51  		{"a.B.c", true},
    52  		{"a.B.C", true},
    53  		{"a.B._c", false},
    54  		{"a.B.c.d", false},
    55  		{"a.B..c", false},
    56  		{"a.B.c_1xX", true},
    57  		{"a.B.c$", false},
    58  		{"a.Bc", false},
    59  		{"a.Z.c", false},
    60  		{"z.B.c", false},
    61  		{".B.c", false},
    62  		{"a..c", false},
    63  	} {
    64  		var expects []string
    65  		if !tc.valid {
    66  			expects = append(expects, fmt.Sprintf(invalidVarNameErrTmpl, path, tc.name, "a.B."))
    67  		}
    68  		issues := SecretVarFile(path, []byte(tc.name+":"))
    69  		verifyIssues(t, issues, expects)
    70  	}
    71  }
    72  
    73  func TestCategoryLevelVarFile(t *testing.T) {
    74  	const path = "vars/a.yaml"
    75  
    76  	for _, tc := range []struct {
    77  		name  string
    78  		valid bool
    79  	}{
    80  		{"a", false},
    81  		{"a.", false},
    82  		{"a.c", true},
    83  		{"a._c", false},
    84  		{"a.c.d", false},
    85  		{"a..c", false},
    86  		{"a.C_1x", true},
    87  		{"a.$", false},
    88  		{"ac", false},
    89  		{"z.c", false},
    90  	} {
    91  		var expects []string
    92  		if !tc.valid {
    93  			expects = append(expects, fmt.Sprintf(invalidVarNameErrTmpl, path, tc.name, "a."))
    94  		}
    95  		issues := SecretVarFile(path, []byte(tc.name+":"))
    96  		verifyIssues(t, issues, expects)
    97  	}
    98  
    99  }
   100  
   101  func TestInvalidFileNames(t *testing.T) {
   102  	paths := []string{
   103  		".yaml",
   104  		"a..yaml",
   105  		"a.B.c.yaml",
   106  		"_.yaml",
   107  	}
   108  	const data = ""
   109  
   110  	for _, path := range paths {
   111  		expects := []string{
   112  			path + `: File's basename doesn't match expected regex ^([a-z][a-z0-9]*(?:\.[A-Z][A-Za-z0-9]*)?\.)yaml$`,
   113  		}
   114  		issues := SecretVarFile(path, []byte(data))
   115  		verifyIssues(t, issues, expects)
   116  	}
   117  }
   118  
   119  func TestInvalidFileNameAndParseError(t *testing.T) {
   120  	const (
   121  		path = "vars/.yaml"
   122  		data = "1"
   123  	)
   124  
   125  	expects := []string{
   126  		path + `: File's basename doesn't match expected regex ^([a-z][a-z0-9]*(?:\.[A-Z][A-Za-z0-9]*)?\.)yaml$`,
   127  		path + ": Failed to parse the file data as key value pairs: yaml: unmarshal errors:\n  line 1: cannot unmarshal !!int `1` into map[string]string",
   128  	}
   129  	issues := SecretVarFile(path, []byte(data))
   130  	verifyIssues(t, issues, expects)
   131  }