|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2026-present Datadog, Inc. |
| 5 | + |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "bufio" |
| 10 | + "errors" |
| 11 | + "flag" |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "os" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/bazelbuild/rules_go/go/tools/bzltestutil" |
| 18 | +) |
| 19 | + |
| 20 | +type manifestEntry struct { |
| 21 | + pkg string |
| 22 | + logPath string |
| 23 | +} |
| 24 | + |
| 25 | +type options struct { |
| 26 | + manifestPath string |
| 27 | + outputPath string |
| 28 | +} |
| 29 | + |
| 30 | +func main() { |
| 31 | + if err := run(os.Args[1:], os.Stdout, os.Stderr); err != nil { |
| 32 | + fmt.Fprintln(os.Stderr, err) |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func run(args []string, stdout, stderr io.Writer) error { |
| 38 | + opts, err := parseFlags(args) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + entries, err := readManifest(opts.manifestPath) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + var out io.Writer = stdout |
| 49 | + var outFile *os.File |
| 50 | + if opts.outputPath != "" && opts.outputPath != "-" { |
| 51 | + outFile, err = os.Create(opts.outputPath) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("create output %q: %w", opts.outputPath, err) |
| 54 | + } |
| 55 | + defer outFile.Close() |
| 56 | + out = outFile |
| 57 | + } |
| 58 | + |
| 59 | + if err := convert(entries, out); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + fmt.Fprintf(stderr, "Converted %d Bazel test logs to test2json\n", len(entries)) |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func parseFlags(args []string) (options, error) { |
| 68 | + var opts options |
| 69 | + fs := flag.NewFlagSet("testlogs_to_json", flag.ContinueOnError) |
| 70 | + fs.SetOutput(io.Discard) |
| 71 | + fs.StringVar(&opts.manifestPath, "manifest", "", "Path to a tab-separated manifest: <go import path>\\t<test.log path>") |
| 72 | + fs.StringVar(&opts.outputPath, "output", "-", "Path to write test2json JSONL output, or '-' for stdout") |
| 73 | + if err := fs.Parse(args); err != nil { |
| 74 | + return opts, err |
| 75 | + } |
| 76 | + if opts.manifestPath == "" { |
| 77 | + return opts, errors.New("missing required -manifest") |
| 78 | + } |
| 79 | + if fs.NArg() != 0 { |
| 80 | + return opts, fmt.Errorf("unexpected positional arguments: %s", strings.Join(fs.Args(), " ")) |
| 81 | + } |
| 82 | + return opts, nil |
| 83 | +} |
| 84 | + |
| 85 | +func readManifest(path string) ([]manifestEntry, error) { |
| 86 | + f, err := os.Open(path) |
| 87 | + if err != nil { |
| 88 | + return nil, fmt.Errorf("open manifest %q: %w", path, err) |
| 89 | + } |
| 90 | + defer f.Close() |
| 91 | + |
| 92 | + var entries []manifestEntry |
| 93 | + scanner := bufio.NewScanner(f) |
| 94 | + for scanner.Scan() { |
| 95 | + line := scanner.Text() |
| 96 | + if strings.TrimSpace(line) == "" { |
| 97 | + continue |
| 98 | + } |
| 99 | + pkg, logPath, ok := strings.Cut(line, "\t") |
| 100 | + if !ok || pkg == "" || logPath == "" { |
| 101 | + return nil, fmt.Errorf("invalid manifest line: expected <go import path>\\t<test.log path>, got %q", line) |
| 102 | + } |
| 103 | + if strings.Contains(logPath, "\t") { |
| 104 | + return nil, fmt.Errorf("invalid manifest line: too many tab-separated fields, got %q", line) |
| 105 | + } |
| 106 | + entries = append(entries, manifestEntry{pkg: pkg, logPath: logPath}) |
| 107 | + } |
| 108 | + if err := scanner.Err(); err != nil { |
| 109 | + return nil, fmt.Errorf("read manifest %q: %w", path, err) |
| 110 | + } |
| 111 | + return entries, nil |
| 112 | +} |
| 113 | + |
| 114 | +func convert(entries []manifestEntry, out io.Writer) error { |
| 115 | + for _, entry := range entries { |
| 116 | + f, err := os.Open(entry.logPath) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("open test log %q for package %s: %w", entry.logPath, entry.pkg, err) |
| 119 | + } |
| 120 | + |
| 121 | + converter := bzltestutil.NewConverter(out, entry.pkg, bzltestutil.Timestamp) |
| 122 | + _, copyErr := io.Copy(converter, f) |
| 123 | + closeErr := f.Close() |
| 124 | + if copyErr != nil { |
| 125 | + return fmt.Errorf("convert test log %q for package %s: %w", entry.logPath, entry.pkg, copyErr) |
| 126 | + } |
| 127 | + if closeErr != nil { |
| 128 | + return fmt.Errorf("close test log %q for package %s: %w", entry.logPath, entry.pkg, closeErr) |
| 129 | + } |
| 130 | + if err := converter.Close(); err != nil { |
| 131 | + return fmt.Errorf("close converter for test log %q package %s: %w", entry.logPath, entry.pkg, err) |
| 132 | + } |
| 133 | + } |
| 134 | + return nil |
| 135 | +} |
0 commit comments