TestGenerateParallelWithMerge verifies that parallel file writing correctly handles file merging when multiple generators target the same path. This tests the interaction between mergeFilesByPath and parallel rendering.
(t *testing.T)
| 134 | // handles file merging when multiple generators target the same path. This |
| 135 | // tests the interaction between mergeFilesByPath and parallel rendering. |
| 136 | func TestGenerateParallelWithMerge(t *testing.T) { |
| 137 | t.Cleanup(func() { Generators = generators }) |
| 138 | |
| 139 | // Three generators: first two merge to same path, third is separate. |
| 140 | // This exercises both merging and parallel writing with NumCPU workers. |
| 141 | Generators = func(cmd string) ([]Genfunc, error) { |
| 142 | return []Genfunc{ |
| 143 | func(genpkg string, roots []eval.Root) ([]*codegen.File, error) { |
| 144 | f1 := &codegen.File{Path: filepath.Join(codegen.Gendir, "types", "merged.go")} |
| 145 | f1.SectionTemplates = []*codegen.SectionTemplate{ |
| 146 | codegen.Header("Types", "types", nil), |
| 147 | {Name: "type1", Source: "type Type1 struct{}\n"}, |
| 148 | } |
| 149 | return []*codegen.File{f1}, nil |
| 150 | }, |
| 151 | func(genpkg string, roots []eval.Root) ([]*codegen.File, error) { |
| 152 | f2 := &codegen.File{Path: filepath.Join(codegen.Gendir, "types", "merged.go")} |
| 153 | f2.SectionTemplates = []*codegen.SectionTemplate{ |
| 154 | codegen.Header("Types", "types", nil), |
| 155 | {Name: "type2", Source: "type Type2 struct{}\n"}, |
| 156 | } |
| 157 | return []*codegen.File{f2}, nil |
| 158 | }, |
| 159 | func(genpkg string, roots []eval.Root) ([]*codegen.File, error) { |
| 160 | f3 := &codegen.File{Path: filepath.Join(codegen.Gendir, "types", "separate.go")} |
| 161 | f3.SectionTemplates = []*codegen.SectionTemplate{ |
| 162 | codegen.Header("Types", "types", nil), |
| 163 | {Name: "type3", Source: "type Type3 struct{}\n"}, |
| 164 | } |
| 165 | return []*codegen.File{f3}, nil |
| 166 | }, |
| 167 | }, nil |
| 168 | } |
| 169 | |
| 170 | dir := t.TempDir() |
| 171 | outputs, err := Generate(dir, "gen", false) |
| 172 | if err != nil { |
| 173 | t.Fatalf("Generate failed: %v", err) |
| 174 | } |
| 175 | outputs = assertVersionFile(t, dir, outputs) |
| 176 | |
| 177 | if len(outputs) != 2 { |
| 178 | t.Fatalf("expected 2 output files, got %d", len(outputs)) |
| 179 | } |
| 180 | |
| 181 | // Verify merged file contains both types |
| 182 | mergedPath := filepath.Join(dir, codegen.Gendir, "types", "merged.go") |
| 183 | bs, err := os.ReadFile(mergedPath) |
| 184 | if err != nil { |
| 185 | t.Fatalf("failed reading merged file: %v", err) |
| 186 | } |
| 187 | content := string(bs) |
| 188 | if !strings.Contains(content, "type Type1 struct{}") { |
| 189 | t.Fatalf("merged file missing Type1:\n%s", content) |
| 190 | } |
| 191 | if !strings.Contains(content, "type Type2 struct{}") { |
| 192 | t.Fatalf("merged file missing Type2:\n%s", content) |
| 193 | } |
nothing calls this directly
no test coverage detected