TestParser runs driver_ast_parser on test_struct.cc and compares the output to the expected json.
(t *testing.T)
| 31 | |
| 32 | // TestParser runs driver_ast_parser on test_struct.cc and compares the output to the expected json. |
| 33 | func TestParser(t *testing.T) { |
| 34 | driverParser, err := testutil.FindFile("tools/nvidia_driver_differ/driver_ast_parser") |
| 35 | if err != nil { |
| 36 | t.Fatalf("failed to find driver_ast_parser: %v", err) |
| 37 | } |
| 38 | |
| 39 | testStructFile, err := testutil.FindFile("tools/nvidia_driver_differ/test_struct.cc") |
| 40 | if err != nil { |
| 41 | t.Fatalf("failed to find test_struct.cc: %v", err) |
| 42 | } |
| 43 | |
| 44 | // Write a file containing the struct name we want to parse. |
| 45 | inputFile, err := os.CreateTemp(os.TempDir(), "input.*.json") |
| 46 | if err != nil { |
| 47 | t.Fatalf("failed to create input file: %v", err) |
| 48 | } |
| 49 | defer func() { |
| 50 | if err := inputFile.Close(); err != nil { |
| 51 | t.Fatalf("failed to close input file: %v", err) |
| 52 | } |
| 53 | if err := os.Remove(inputFile.Name()); err != nil { |
| 54 | t.Fatalf("failed to remove input file: %v", err) |
| 55 | } |
| 56 | }() |
| 57 | |
| 58 | input := parser.InputJSON{ |
| 59 | Structs: []string{"TestStruct", "TestStruct2"}, |
| 60 | Constants: []string{ |
| 61 | "VAR_CONSTANT_MACRO", |
| 62 | "VAR_ADDITION_MACRO", |
| 63 | "VAR_UNSIGNED_HEX_MACRO", |
| 64 | "VAR_PARENTHESIZED_HEX_MACRO", |
| 65 | "VAR_USES_FUNCTION_MACRO", |
| 66 | }, |
| 67 | } |
| 68 | if err := json.NewEncoder(inputFile).Encode(&input); err != nil { |
| 69 | t.Fatalf("failed to write input input file: %v", err) |
| 70 | } |
| 71 | inputFile.Sync() |
| 72 | |
| 73 | cmd := exec.Command(driverParser, "--input", inputFile.Name(), testStructFile) |
| 74 | var stderr strings.Builder |
| 75 | cmd.Stderr = &stderr |
| 76 | out, err := cmd.Output() |
| 77 | if err != nil { |
| 78 | t.Fatalf("failed to run driver_ast_parser: %v\n%s", err, stderr.String()) |
| 79 | } |
| 80 | if stderr.Len() > 0 { |
| 81 | t.Logf("driver_ast_parser stderr:\n%s", stderr.String()) |
| 82 | } |
| 83 | |
| 84 | outputJSON := parser.OutputJSON{} |
| 85 | if err := json.Unmarshal(out, &outputJSON); err != nil { |
| 86 | t.Fatalf("failed to unmarshal output %s: %v", string(out), err) |
| 87 | } |
| 88 | expectedOutput := parser.OutputJSON{ |
| 89 | Records: parser.RecordDefs{ |
| 90 | "TestStruct": parser.RecordDef{ |
nothing calls this directly
no test coverage detected
searching dependent graphs…