|
| 1 | +// / ctx: https://ctx.ist |
| 2 | +// ,'`./ do you remember? |
| 3 | +// `.,'\ |
| 4 | +// \ Copyright 2026-present Context contributors. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +package audit |
| 8 | + |
| 9 | +import ( |
| 10 | + "go/ast" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +// flagMethods lists cobra flag registration methods |
| 16 | +// that must go through internal/flagbind/. |
| 17 | +var flagMethods = map[string]bool{ |
| 18 | + "StringVar": true, |
| 19 | + "StringVarP": true, |
| 20 | + "BoolVar": true, |
| 21 | + "BoolVarP": true, |
| 22 | + "IntVar": true, |
| 23 | + "IntVarP": true, |
| 24 | + "IntP": true, |
| 25 | + "BoolP": true, |
| 26 | + "StringP": true, |
| 27 | + "DurationVar": true, |
| 28 | + "DurationVarP": true, |
| 29 | + "StringSliceVar": true, |
| 30 | +} |
| 31 | + |
| 32 | +// TestNoFlagBindOutsideFlagbind ensures direct cobra |
| 33 | +// flag registration (.Flags().StringVar, etc.) only |
| 34 | +// appears in internal/flagbind/. All other packages |
| 35 | +// must use the flagbind helpers. |
| 36 | +// |
| 37 | +// Test files are exempt. |
| 38 | +// |
| 39 | +// See specs/ast-audit-tests.md for rationale. |
| 40 | +func TestNoFlagBindOutsideFlagbind(t *testing.T) { |
| 41 | + pkgs := loadPackages(t) |
| 42 | + var violations []string |
| 43 | + |
| 44 | + for _, pkg := range pkgs { |
| 45 | + if strings.Contains(pkg.PkgPath, "flagbind") { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + for _, file := range pkg.Syntax { |
| 50 | + fpath := pkg.Fset.Position(file.Pos()).Filename |
| 51 | + if isTestFile(fpath) { |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + ast.Inspect(file, func(n ast.Node) bool { |
| 56 | + call, ok := n.(*ast.CallExpr) |
| 57 | + if !ok { |
| 58 | + return true |
| 59 | + } |
| 60 | + |
| 61 | + // Match: x.Flags().Method() or |
| 62 | + // x.PersistentFlags().Method() |
| 63 | + sel, ok := call.Fun.(*ast.SelectorExpr) |
| 64 | + if !ok { |
| 65 | + return true |
| 66 | + } |
| 67 | + |
| 68 | + if !flagMethods[sel.Sel.Name] { |
| 69 | + return true |
| 70 | + } |
| 71 | + |
| 72 | + // The receiver should be a call to |
| 73 | + // Flags() or PersistentFlags(). |
| 74 | + innerCall, ok := sel.X.(*ast.CallExpr) |
| 75 | + if !ok { |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | + innerSel, ok := innerCall.Fun.(*ast.SelectorExpr) |
| 80 | + if !ok { |
| 81 | + return true |
| 82 | + } |
| 83 | + |
| 84 | + method := innerSel.Sel.Name |
| 85 | + if method != "Flags" && |
| 86 | + method != "PersistentFlags" { |
| 87 | + return true |
| 88 | + } |
| 89 | + |
| 90 | + violations = append(violations, |
| 91 | + posString(pkg.Fset, call.Pos())+ |
| 92 | + ": ."+method+"()."+ |
| 93 | + sel.Sel.Name+ |
| 94 | + "() must use flagbind", |
| 95 | + ) |
| 96 | + |
| 97 | + return true |
| 98 | + }) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if len(violations) > 0 { |
| 103 | + t.Errorf( |
| 104 | + "%d direct flag registrations:", |
| 105 | + len(violations), |
| 106 | + ) |
| 107 | + } |
| 108 | + limit := 20 |
| 109 | + if len(violations) < limit { |
| 110 | + limit = len(violations) |
| 111 | + } |
| 112 | + for _, v := range violations[:limit] { |
| 113 | + t.Error(v) |
| 114 | + } |
| 115 | + if len(violations) > 20 { |
| 116 | + t.Errorf( |
| 117 | + "... and %d more", |
| 118 | + len(violations)-20, |
| 119 | + ) |
| 120 | + } |
| 121 | +} |
0 commit comments