github.com/massongit/reviewdog@v0.0.0-20240331071725-4a16675475a8/parser/errorformat_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	"google.golang.org/protobuf/encoding/protojson"
    11  )
    12  
    13  func TestNewErrorformatParserString(t *testing.T) {
    14  	in := []string{`%f:%l:%c:%m`, `%-G%.%#`}
    15  
    16  	got, err := NewErrorformatParserString(in)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	if len(got.efm.Efms) != len(in) {
    22  		t.Errorf("NewErrorformatParserString: len: got %v, want %v", len(got.efm.Efms), len(in))
    23  	}
    24  }
    25  func ExampleErrorformatParser() {
    26  	const sample = `/path/to/file1.txt:1:14: [E][RULE:14] message 1
    27  /path/to/file2.txt:2:14: [N][RULE:7] message 2`
    28  
    29  	p, err := NewErrorformatParserString([]string{`%f:%l:%c: [%t][RULE:%n] %m`})
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	diagnostics, err := p.Parse(strings.NewReader(sample))
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	for _, d := range diagnostics {
    38  		rdjson, _ := protojson.MarshalOptions{Indent: "  "}.Marshal(d)
    39  		var out bytes.Buffer
    40  		json.Indent(&out, rdjson, "", "  ")
    41  		fmt.Println(out.String())
    42  	}
    43  	// Output:
    44  	// {
    45  	//   "message": "message 1",
    46  	//   "location": {
    47  	//     "path": "/path/to/file1.txt",
    48  	//     "range": {
    49  	//       "start": {
    50  	//         "line": 1,
    51  	//         "column": 14
    52  	//       }
    53  	//     }
    54  	//   },
    55  	//   "severity": "ERROR",
    56  	//   "code": {
    57  	//     "value": "14"
    58  	//   },
    59  	//   "originalOutput": "/path/to/file1.txt:1:14: [E][RULE:14] message 1"
    60  	// }
    61  	// {
    62  	//   "message": "message 2",
    63  	//   "location": {
    64  	//     "path": "/path/to/file2.txt",
    65  	//     "range": {
    66  	//       "start": {
    67  	//         "line": 2,
    68  	//         "column": 14
    69  	//       }
    70  	//     }
    71  	//   },
    72  	//   "severity": "INFO",
    73  	//   "code": {
    74  	//     "value": "7"
    75  	//   },
    76  	//   "originalOutput": "/path/to/file2.txt:2:14: [N][RULE:7] message 2"
    77  	// }
    78  }