Debian Codegraph @

e2e_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 package humanlog
2
3 import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "io/ioutil"
8 "os"
9 "path/filepath"
10 "testing"
11
12 "github.com/humanlogio/humanlog/internal/pkg/config"
13 "github.com/humanlogio/humanlog/internal/pkg/sink/stdiosink"
14 )
15
16 func TestHarness(t *testing.T) {
17 ctx := context.Background()
18 root := "test/cases"
19 des, err := os.ReadDir(root)
20 if err != nil {
21 t.Fatal(err)
22 }
23 for _, de := range des {
24 t.Log(de.Name())
25 if !de.IsDir() {
26 continue
27 }
28 testCase := de.Name()
29 t.Run(testCase, func(t *testing.T) {
30 input, err := ioutil.ReadFile(filepath.Join(root, de.Name(), "input"))
31 if err != nil {
32 t.Fatalf("reading input: %v", err)
33 }
34 want, err := ioutil.ReadFile(filepath.Join(root, de.Name(), "want"))
35 if err != nil {
36 t.Fatalf("reading expected output: %v", err)
37 }
38 cfgjson, err := ioutil.ReadFile(filepath.Join(root, de.Name(), "config.json"))
39 if err != nil {
40 t.Fatalf("reading config: %v", err)
41 }
42 var cfg config.Config
43 if err := json.Unmarshal(cfgjson, &cfg); err != nil {
44 t.Fatalf("unmarshaling config: %v", err)
45 }
46 gotw := bytes.NewBuffer(nil)
47 sinkOpts, errs := stdiosink.StdioOptsFrom(cfg)
48 if len(errs) > 1 {
49 t.Fatalf("errs=%v", errs)
50 }
51 s := stdiosink.NewStdio(gotw, sinkOpts)
52 err = Scan(ctx, bytes.NewReader(input), s, HandlerOptionsFrom(cfg))
53 if err != nil {
54 t.Fatalf("scanning input: %v", err)
55 }
56 minl := len(want)
57 got := gotw.Bytes()
58 if len(want) < len(got) {
59 t.Errorf("want len %d got %d", len(want), len(got))
60 }
61 if len(want) > len(got) {
62 t.Errorf("want len %d got %d", len(want), len(got))
63 minl = len(got)
64 }
65
66 ranges := newByteRanges()
67 for i, w := range want[:minl] {
68 g := got[i]
69 if w != g {
70 ranges.track(i)
71 }
72 }
73 mismatches := len(ranges.ranges)
74 if mismatches == 0 {
75 return
76 }
77 t.Errorf("total of %d ranges mismatch", mismatches)
78 if len(ranges.ranges) > 10 {
79 mismatches = 10
80 t.Errorf("only showing first %d mismatches", mismatches)
81 }
82 for _, br := range ranges.ranges[:mismatches] {
83 t.Errorf("- mismatch from byte %d to %d", br.start, br.end)
84 wantPart := want[br.start:br.end]
85 gotPart := got[br.start:br.end]
86 t.Errorf("want %q", wantPart)
87 t.Errorf("got %q", gotPart)
88 }
89
90 dir, err := ioutil.TempDir(os.TempDir(), "humanlog-tests-*")
91 if err != nil {
92 t.Fatal(err)
93 }
94 gotf, err := ioutil.TempFile(dir, de.Name())
95 if err != nil {
96 t.Fatal(err)
97 }
98 if _, err := gotf.Write(got); err != nil {
99 t.Fatal(err)
100 }
101 if err := gotf.Close(); err != nil {
102 t.Fatal(err)
103 }
104 t.Logf("wrote output to %q", gotf.Name())
105 })
106 }
107 }
108
109 type byteranges struct {
110 ranges []*byterange
111 }
112
113 func newByteRanges() *byteranges {
114 return &byteranges{}
115 }
116
117 func (br *byteranges) track(idx int) {
118 if len(br.ranges) == 0 {
119 br.ranges = append(br.ranges, &byterange{start: idx, end: idx + 1})
120 return
121 }
122 lastRange := br.ranges[len(br.ranges)-1]
123 if lastRange.end == idx {
124 lastRange.end = idx + 1
125 } else {
126 br.ranges = append(br.ranges, &byterange{start: idx, end: idx + 1})
127 }
128 }
129
130 type byterange struct {
131 start int
132 end int
133 }