Skip to content

Commit 8a7b85c

Browse files
authored
Debugging hack: env variable to capture the intermediate .pp of certain examples (#2810)
Capture the generated intermediate representation of code samples from a given file. Useful to debug codegen issues and create tests for p/p.
1 parent 52288d0 commit 8a7b85c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ pulumi-azure-native-sdk
3838
.make/
3939

4040
sdk/python/venv
41+
42+
DEBUG_CODEGEN_EXAMPLE_HCL

provider/pkg/gen/examples.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ func generateExamplePrograms(example resources.AzureAPIExample, body *model.Body
248248
return nil, err
249249
}
250250

251+
writeDebugHCL(programBody, example.Location)
252+
251253
debug.Log("Generating example programs for %s\n%s\n", example.Location, programBody)
252254
parser := syntax.NewParser()
253255
if err := parser.ParseFile(strings.NewReader(programBody), "program.pp"); err != nil {
@@ -313,6 +315,35 @@ func generateExamplePrograms(example resources.AzureAPIExample, body *model.Body
313315
return languageExample, nil
314316
}
315317

318+
// writeDebugHCL writes the given example program to DEBUG_CODEGEN_EXAMPLE_HCL/ as a .pp file if
319+
// the exampleLocation path matches the value of DEBUG_CODEGEN_EXAMPLE_HCL. Paths are from the
320+
// root of the provider repository. Wildcards are allowed.
321+
// Example: DEBUG_CODEGEN_EXAMPLE_HCL=azure-rest-api-specs/specification/containerservice/resource-manager/Microsoft.ContainerService/aks/stable/2023-08-01/examples/ManagedClustersCreate_*
322+
func writeDebugHCL(programBody string, exampleLocation string) {
323+
debugExamplesPath := os.Getenv("DEBUG_CODEGEN_EXAMPLE_HCL")
324+
match, err := path.Match(debugExamplesPath, exampleLocation)
325+
if err != nil {
326+
log.Printf("\nMalformed DEBUG_CODEGEN_EXAMPLE_HCL %s: %v", debugExamplesPath, err)
327+
} else if match {
328+
log.Printf("\nGenerated HCL for %s:\n%s\n", exampleLocation, programBody)
329+
330+
// Switch path from azure-rest-api-specs/specification/provider/... to reports/provider/...
331+
parts := strings.Split(exampleLocation, "/")
332+
newParts := make([]string, len(parts))
333+
newParts = append(newParts, "DEBUG_CODEGEN_EXAMPLE_HCL")
334+
newParts = append(newParts, parts[2:]...)
335+
newParts[len(newParts)-1] = newParts[len(newParts)-1] + ".pp"
336+
dest := filepath.Join(newParts...)
337+
log.Printf("Writing example %s to %s", exampleLocation, dest)
338+
339+
os.MkdirAll(filepath.Dir(dest), 0755)
340+
err := os.WriteFile(dest, []byte(programBody), 0644)
341+
if err != nil {
342+
log.Printf("\nFailed to write example to %s: %v", dest, err)
343+
}
344+
}
345+
}
346+
316347
func recoverableProgramGen(name string, program *hcl2.Program, fn programGenFn) (files map[string][]byte, err error) {
317348
const timeout = 3 * time.Minute
318349

0 commit comments

Comments
 (0)