|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +type pythonBehaviorInventory struct { |
| 13 | + Summary map[string]int `json:"summary"` |
| 14 | + Commands []pythonCommandContract `json:"commands"` |
| 15 | + Tests []pythonTestContract `json:"tests"` |
| 16 | + Source []pythonSourceContract `json:"source_contracts"` |
| 17 | +} |
| 18 | + |
| 19 | +type pythonCommandContract struct { |
| 20 | + ID string `json:"id"` |
| 21 | + Path []string `json:"path"` |
| 22 | + Hidden bool `json:"hidden"` |
| 23 | + Params []pythonParamContract `json:"params"` |
| 24 | +} |
| 25 | + |
| 26 | +type pythonParamContract struct { |
| 27 | + Name string `json:"name"` |
| 28 | + Type string `json:"type"` |
| 29 | + Opts []string `json:"opts"` |
| 30 | + SecondaryOpts []string `json:"secondary_opts"` |
| 31 | +} |
| 32 | + |
| 33 | +type pythonTestContract struct { |
| 34 | + ID string `json:"id"` |
| 35 | +} |
| 36 | + |
| 37 | +type pythonSourceContract struct { |
| 38 | + ID string `json:"id"` |
| 39 | +} |
| 40 | + |
| 41 | +func pythonInterpreterForContracts(t *testing.T, required bool) string { |
| 42 | + t.Helper() |
| 43 | + bin := os.Getenv("APM_PYTHON_BIN") |
| 44 | + if bin == "" { |
| 45 | + if required { |
| 46 | + t.Fatal("APM_PYTHON_BIN is required to extract Python behavior contracts") |
| 47 | + } |
| 48 | + t.Skip("APM_PYTHON_BIN not set; skipping Python behavior contract extraction") |
| 49 | + } |
| 50 | + python := filepath.Join(filepath.Dir(bin), "python") |
| 51 | + if _, err := os.Stat(python); err != nil { |
| 52 | + if required { |
| 53 | + t.Fatalf("Python interpreter next to APM_PYTHON_BIN not found at %s: %v", python, err) |
| 54 | + } |
| 55 | + t.Skipf("Python interpreter next to APM_PYTHON_BIN not found at %s", python) |
| 56 | + } |
| 57 | + return python |
| 58 | +} |
| 59 | + |
| 60 | +func loadPythonBehaviorInventory(t *testing.T, required bool) pythonBehaviorInventory { |
| 61 | + t.Helper() |
| 62 | + if path := os.Getenv("APM_PYTHON_CONTRACT_INVENTORY"); path != "" { |
| 63 | + data, err := os.ReadFile(path) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("read APM_PYTHON_CONTRACT_INVENTORY=%s: %v", path, err) |
| 66 | + } |
| 67 | + var inv pythonBehaviorInventory |
| 68 | + if err := json.Unmarshal(data, &inv); err != nil { |
| 69 | + t.Fatalf("parse APM_PYTHON_CONTRACT_INVENTORY=%s: %v", path, err) |
| 70 | + } |
| 71 | + return inv |
| 72 | + } |
| 73 | + |
| 74 | + root := completionModuleRoot(t) |
| 75 | + python := pythonInterpreterForContracts(t, required) |
| 76 | + cmd := exec.Command(python, "scripts/ci/python_behavior_contracts.py", "extract") |
| 77 | + cmd.Dir = root |
| 78 | + cmd.Env = append(os.Environ(), "NO_COLOR=1", "COLUMNS=10000") |
| 79 | + out, err := cmd.CombinedOutput() |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("extract Python behavior contracts failed: %v\n%s", err, string(out)) |
| 82 | + } |
| 83 | + var inv pythonBehaviorInventory |
| 84 | + if err := json.Unmarshal(out, &inv); err != nil { |
| 85 | + t.Fatalf("parse Python behavior contract inventory: %v\n%s", err, string(out)) |
| 86 | + } |
| 87 | + return inv |
| 88 | +} |
| 89 | + |
| 90 | +func contractHelpArgs(command pythonCommandContract) []string { |
| 91 | + if len(command.Path) == 0 { |
| 92 | + return []string{"--help"} |
| 93 | + } |
| 94 | + args := append([]string{}, command.Path...) |
| 95 | + args = append(args, "--help") |
| 96 | + return args |
| 97 | +} |
| 98 | + |
| 99 | +func normalizeContractHelp(text string) string { |
| 100 | + var lines []string |
| 101 | + for _, line := range strings.Split(text, "\n") { |
| 102 | + if strings.Contains(line, "A new version of APM is available") || |
| 103 | + strings.Contains(line, "Run apm update to upgrade") { |
| 104 | + continue |
| 105 | + } |
| 106 | + lines = append(lines, strings.TrimRight(line, " \t")) |
| 107 | + } |
| 108 | + return strings.TrimRight(strings.Join(lines, "\n"), "\n") |
| 109 | +} |
| 110 | + |
| 111 | +func TestParityPythonCommandSurfaceFromSource(t *testing.T) { |
| 112 | + inv := loadPythonBehaviorInventory(t, false) |
| 113 | + if len(inv.Commands) == 0 { |
| 114 | + t.Fatal("Python behavior inventory returned no commands") |
| 115 | + } |
| 116 | + for _, command := range inv.Commands { |
| 117 | + command := command |
| 118 | + if command.Hidden { |
| 119 | + continue |
| 120 | + } |
| 121 | + t.Run(command.ID, func(t *testing.T) { |
| 122 | + goOut, goErr, goCode := runGo(t, contractHelpArgs(command)...) |
| 123 | + if goCode != 0 { |
| 124 | + t.Fatalf("Go help for %s exited %d\nstdout:\n%s\nstderr:\n%s", |
| 125 | + command.ID, goCode, goOut, goErr) |
| 126 | + } |
| 127 | + combined := goOut + goErr |
| 128 | + if strings.Contains(combined, "not yet") { |
| 129 | + t.Fatalf("Go help for %s still contains WIP text:\n%s", command.ID, combined) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestParityPythonOptionsFromSource(t *testing.T) { |
| 136 | + if os.Getenv("APM_PYTHON_CONTRACT_INVENTORY") == "" { |
| 137 | + t.Skip("set APM_PYTHON_CONTRACT_INVENTORY to run option-coverage checks (migration CI only)") |
| 138 | + } |
| 139 | + inv := loadPythonBehaviorInventory(t, false) |
| 140 | + for _, command := range inv.Commands { |
| 141 | + command := command |
| 142 | + if command.Hidden { |
| 143 | + continue |
| 144 | + } |
| 145 | + t.Run(command.ID, func(t *testing.T) { |
| 146 | + goOut, goErr, goCode := runGo(t, contractHelpArgs(command)...) |
| 147 | + if goCode != 0 { |
| 148 | + t.Fatalf("Go help for %s exited %d\nstdout:\n%s\nstderr:\n%s", |
| 149 | + command.ID, goCode, goOut, goErr) |
| 150 | + } |
| 151 | + help := normalizeContractHelp(goOut + goErr) |
| 152 | + for _, param := range command.Params { |
| 153 | + if param.Type != "Option" { |
| 154 | + continue |
| 155 | + } |
| 156 | + opts := append([]string{}, param.Opts...) |
| 157 | + opts = append(opts, param.SecondaryOpts...) |
| 158 | + for _, opt := range opts { |
| 159 | + if opt == "" { |
| 160 | + continue |
| 161 | + } |
| 162 | + if !strings.Contains(help, opt) { |
| 163 | + t.Logf("TRACKING: %s help missing Python option %s (migration in progress)", command.ID, opt) |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestParityCompletionPythonBehaviorContracts(t *testing.T) { |
| 172 | + inventoryPath := os.Getenv("APM_PYTHON_CONTRACT_INVENTORY") |
| 173 | + if inventoryPath == "" { |
| 174 | + t.Skip("set APM_PYTHON_CONTRACT_INVENTORY to enforce the behavior-contracts coverage gate (migration CI only)") |
| 175 | + } |
| 176 | + |
| 177 | + root := completionModuleRoot(t) |
| 178 | + python := pythonInterpreterForContracts(t, true) |
| 179 | + |
| 180 | + check := exec.Command( |
| 181 | + python, |
| 182 | + "scripts/ci/python_behavior_contracts.py", |
| 183 | + "check", |
| 184 | + "--inventory", |
| 185 | + inventoryPath, |
| 186 | + "--coverage", |
| 187 | + filepath.Join(root, "tests", "parity", "python_contract_coverage.yml"), |
| 188 | + ) |
| 189 | + check.Dir = root |
| 190 | + check.Env = append(os.Environ(), "NO_COLOR=1", "COLUMNS=10000") |
| 191 | + out, err := check.CombinedOutput() |
| 192 | + if err != nil { |
| 193 | + t.Fatalf("Python behavior contracts are not fully covered:\n%s", string(out)) |
| 194 | + } |
| 195 | +} |
0 commit comments