|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +type goCutoverPythonTestCoverage struct { |
| 16 | + SchemaVersion int `json:"schema_version"` |
| 17 | + Description string `json:"description"` |
| 18 | + ConvertedPythonTests map[string][]string `json:"converted_python_tests"` |
| 19 | +} |
| 20 | + |
| 21 | +type pythonClassContext struct { |
| 22 | + name string |
| 23 | + indent int |
| 24 | +} |
| 25 | + |
| 26 | +var ( |
| 27 | + pythonClassRE = regexp.MustCompile(`^class\s+(Test[A-Za-z0-9_]*)\b`) |
| 28 | + pythonTestRE = regexp.MustCompile(`^(?:async\s+)?def\s+(test_[A-Za-z0-9_]*)\b`) |
| 29 | +) |
| 30 | + |
| 31 | +func TestGoCutoverPythonTestConversionCoverage(t *testing.T) { |
| 32 | + root := completionModuleRoot(t) |
| 33 | + pythonTests := discoverPythonTestsForCutover(t, root) |
| 34 | + coverage := loadGoCutoverPythonTestCoverage(t, root) |
| 35 | + |
| 36 | + converted := 0 |
| 37 | + var missing []string |
| 38 | + for _, id := range pythonTests { |
| 39 | + tests := coverage.ConvertedPythonTests[id] |
| 40 | + if len(tests) == 0 { |
| 41 | + missing = append(missing, id) |
| 42 | + continue |
| 43 | + } |
| 44 | + converted++ |
| 45 | + } |
| 46 | + |
| 47 | + defer emitCraneRatioGate("python_behavior_contracts", converted, len(pythonTests)) |
| 48 | + defer emitCraneBoolGate("golden_fixture_corpus", converted == len(pythonTests) && len(pythonTests) > 0) |
| 49 | + defer emitCraneBoolGate("all_go_golden_tests", converted == len(pythonTests) && len(pythonTests) > 0) |
| 50 | + |
| 51 | + if len(pythonTests) == 0 { |
| 52 | + t.Fatal("no Python tests discovered under tests/; coverage gate cannot prove conversion") |
| 53 | + } |
| 54 | + if coverage.SchemaVersion != 1 { |
| 55 | + t.Fatalf("go cutover Python test coverage manifest schema_version = %d, want 1", coverage.SchemaVersion) |
| 56 | + } |
| 57 | + if len(missing) > 0 { |
| 58 | + t.Fatalf( |
| 59 | + "Go cutover coverage incomplete: %d/%d Python tests mapped to Go tests; %d missing.\nFirst missing tests:\n%s", |
| 60 | + converted, |
| 61 | + len(pythonTests), |
| 62 | + len(missing), |
| 63 | + formatCutoverMissing(missing, 80), |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestGoCutoverNoPythonRuntimeDependency(t *testing.T) { |
| 69 | + dir := t.TempDir() |
| 70 | + stdout, stderr, code := realBehaviorRunGoInDirSanitized(t, dir, "--version") |
| 71 | + passed := code == 0 && strings.Contains(strings.ToLower(stdout+stderr), "apm") |
| 72 | + emitCraneBoolGate("no_python_runtime_dependency", passed) |
| 73 | + if !passed { |
| 74 | + t.Fatalf("Go CLI must run without Python runtime env vars; exit=%d stdout=%q stderr=%q", code, stdout, stderr) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func discoverPythonTestsForCutover(t *testing.T, root string) []string { |
| 79 | + t.Helper() |
| 80 | + testsRoot := filepath.Join(root, "tests") |
| 81 | + var ids []string |
| 82 | + err := filepath.WalkDir(testsRoot, func(path string, entry os.DirEntry, err error) error { |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + if entry.IsDir() { |
| 87 | + if entry.Name() == "__pycache__" { |
| 88 | + return filepath.SkipDir |
| 89 | + } |
| 90 | + if rel, relErr := filepath.Rel(testsRoot, path); relErr == nil { |
| 91 | + parts := strings.Split(filepath.ToSlash(rel), "/") |
| 92 | + if len(parts) > 0 && parts[0] == "parity" { |
| 93 | + return filepath.SkipDir |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | + } |
| 98 | + name := entry.Name() |
| 99 | + if !strings.HasPrefix(name, "test") || !strings.HasSuffix(name, ".py") { |
| 100 | + return nil |
| 101 | + } |
| 102 | + fileIDs, scanErr := scanPythonTestFile(t, root, path) |
| 103 | + if scanErr != nil { |
| 104 | + return scanErr |
| 105 | + } |
| 106 | + ids = append(ids, fileIDs...) |
| 107 | + return nil |
| 108 | + }) |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("discover Python tests: %v", err) |
| 111 | + } |
| 112 | + sort.Strings(ids) |
| 113 | + return ids |
| 114 | +} |
| 115 | + |
| 116 | +func scanPythonTestFile(t *testing.T, root, path string) ([]string, error) { |
| 117 | + t.Helper() |
| 118 | + file, err := os.Open(path) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + defer file.Close() |
| 123 | + |
| 124 | + rel, err := filepath.Rel(root, path) |
| 125 | + if err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + rel = filepath.ToSlash(rel) |
| 129 | + |
| 130 | + var ids []string |
| 131 | + var classes []pythonClassContext |
| 132 | + scanner := bufio.NewScanner(file) |
| 133 | + for scanner.Scan() { |
| 134 | + line := scanner.Text() |
| 135 | + indent := leadingWhitespaceWidth(line) |
| 136 | + trimmed := strings.TrimSpace(line) |
| 137 | + if trimmed == "" || strings.HasPrefix(trimmed, "#") { |
| 138 | + continue |
| 139 | + } |
| 140 | + |
| 141 | + for len(classes) > 0 && indent <= classes[len(classes)-1].indent { |
| 142 | + classes = classes[:len(classes)-1] |
| 143 | + } |
| 144 | + |
| 145 | + if match := pythonClassRE.FindStringSubmatch(trimmed); match != nil { |
| 146 | + classes = append(classes, pythonClassContext{name: match[1], indent: indent}) |
| 147 | + continue |
| 148 | + } |
| 149 | + |
| 150 | + match := pythonTestRE.FindStringSubmatch(trimmed) |
| 151 | + if match == nil { |
| 152 | + continue |
| 153 | + } |
| 154 | + name := match[1] |
| 155 | + if len(classes) > 0 && indent > classes[len(classes)-1].indent { |
| 156 | + ids = append(ids, fmt.Sprintf("%s::%s::%s", rel, classes[len(classes)-1].name, name)) |
| 157 | + continue |
| 158 | + } |
| 159 | + if indent == 0 { |
| 160 | + ids = append(ids, fmt.Sprintf("%s::%s", rel, name)) |
| 161 | + } |
| 162 | + } |
| 163 | + if err := scanner.Err(); err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + return ids, nil |
| 167 | +} |
| 168 | + |
| 169 | +func loadGoCutoverPythonTestCoverage(t *testing.T, root string) goCutoverPythonTestCoverage { |
| 170 | + t.Helper() |
| 171 | + path := filepath.Join(root, "cmd", "apm", "testdata", "go_cutover", "python_test_coverage.json") |
| 172 | + data, err := os.ReadFile(path) |
| 173 | + if err != nil { |
| 174 | + t.Fatalf("read Go cutover Python test coverage manifest %s: %v", path, err) |
| 175 | + } |
| 176 | + var coverage goCutoverPythonTestCoverage |
| 177 | + if err := json.Unmarshal(data, &coverage); err != nil { |
| 178 | + t.Fatalf("parse Go cutover Python test coverage manifest %s: %v", path, err) |
| 179 | + } |
| 180 | + if coverage.ConvertedPythonTests == nil { |
| 181 | + coverage.ConvertedPythonTests = map[string][]string{} |
| 182 | + } |
| 183 | + return coverage |
| 184 | +} |
| 185 | + |
| 186 | +func formatCutoverMissing(missing []string, limit int) string { |
| 187 | + if limit > len(missing) { |
| 188 | + limit = len(missing) |
| 189 | + } |
| 190 | + lines := make([]string, 0, limit+1) |
| 191 | + for _, id := range missing[:limit] { |
| 192 | + lines = append(lines, " - "+id) |
| 193 | + } |
| 194 | + if limit < len(missing) { |
| 195 | + lines = append(lines, fmt.Sprintf(" ... %d more", len(missing)-limit)) |
| 196 | + } |
| 197 | + return strings.Join(lines, "\n") |
| 198 | +} |
| 199 | + |
| 200 | +func leadingWhitespaceWidth(line string) int { |
| 201 | + width := 0 |
| 202 | + for _, r := range line { |
| 203 | + switch r { |
| 204 | + case ' ': |
| 205 | + width++ |
| 206 | + case '\t': |
| 207 | + width += 4 |
| 208 | + default: |
| 209 | + return width |
| 210 | + } |
| 211 | + } |
| 212 | + return width |
| 213 | +} |
| 214 | + |
| 215 | +func realBehaviorRunGoInDirSanitized(t *testing.T, dir string, args ...string) (string, string, int) { |
| 216 | + t.Helper() |
| 217 | + cleared := map[string]string{ |
| 218 | + "APM_PYTHON_BIN": "", |
| 219 | + "APM_PYTHON_CONTRACT_INVENTORY": "", |
| 220 | + "PYTHONPATH": "", |
| 221 | + "VIRTUAL_ENV": "", |
| 222 | + } |
| 223 | + return realBehaviorRunGoInDir(t, dir, cleared, args...) |
| 224 | +} |
0 commit comments