1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 |
package engine
import (
"bufio"
"bytes"
"context"
"encoding/json"
"flag"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"testing"
"time"
"git.pablomurad.com/pablo/patchwork/internal/diff"
)
var update = flag.Bool("update", false, "rewrite testdata/cases/*/expected.json")
// A golden case is a directory of versions v1.md, v2.md, ... ingested in
// order as the same file, one hour apart. An optional steps.txt changes the
// schedule: one line per step, "<file> [+duration]".
//
// expected.json records every step's action and each published revision's
// excerpt, so a change in the publish rules or the diff shows up as a
// reviewable change in the golden files.
type goldenStep struct {
File string `json:"file"`
Action Action `json:"action"`
Number int `json:"number,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
type goldenRevision struct {
Number int `json:"number"`
Note string `json:"note"`
Kind string `json:"kind,omitempty"`
Maturity string `json:"maturity,omitempty"`
Stats diff.Stats `json:"stats"`
Meta []diff.MetaChange `json:"meta_changes,omitempty"`
Excerpt []diff.Op `json:"excerpt"`
Truncated bool `json:"truncated,omitempty"`
}
type golden struct {
Steps []goldenStep `json:"steps"`
Revisions []goldenRevision `json:"revisions"`
}
var versionFile = regexp.MustCompile(`^v(\d+)\.md$`)
func TestGolden(t *testing.T) {
dirs, err := filepath.Glob("testdata/cases/*")
if err != nil {
t.Fatal(err)
}
if len(dirs) == 0 {
t.Fatal("no golden cases")
}
for _, dir := range dirs {
t.Run(filepath.Base(dir), func(t *testing.T) { runGolden(t, dir) })
}
}
type step struct {
file string
after time.Duration
}
func schedule(t *testing.T, dir string) []step {
if f, err := os.Open(filepath.Join(dir, "steps.txt")); err == nil {
defer f.Close()
var steps []step
sc := bufio.NewScanner(f)
for sc.Scan() {
fields := strings.Fields(sc.Text())
if len(fields) == 0 || strings.HasPrefix(fields[0], "#") {
continue
}
s := step{file: fields[0], after: time.Hour}
if len(fields) > 1 {
d, err := time.ParseDuration(strings.TrimPrefix(fields[1], "+"))
if err != nil {
t.Fatal(err)
}
s.after = d
}
steps = append(steps, s)
}
return steps
}
entries, _ := os.ReadDir(dir)
var nums []int
for _, e := range entries {
if m := versionFile.FindStringSubmatch(e.Name()); m != nil {
n, _ := strconv.Atoi(m[1])
nums = append(nums, n)
}
}
sort.Ints(nums)
var steps []step
for _, n := range nums {
steps = append(steps, step{file: "v" + strconv.Itoa(n) + ".md", after: time.Hour})
}
return steps
}
func runGolden(t *testing.T, dir string) {
h := newHarness(t)
var got golden
for _, s := range schedule(t, dir) {
raw, err := os.ReadFile(filepath.Join(dir, s.file))
if err != nil {
t.Fatal(err)
}
h.after(s.after)
res, err := h.e.Ingest(context.Background(), Observation{Path: "idea.md", Raw: raw}, false)
if err != nil {
t.Fatal(err)
}
got.Steps = append(got.Steps, goldenStep{File: s.file, Action: res.Action, Number: res.Number, Warnings: res.Warnings})
}
for _, r := range h.revisions("idea.md") {
if r.Baseline() {
continue
}
f := h.diffOf(r)
ex, cut := diff.Excerpt(f.Ops, 1, h.e.Cfg.Feeds.ExcerptBlocks)
got.Revisions = append(got.Revisions, goldenRevision{
Number: r.Number, Note: r.Note, Kind: r.Kind, Maturity: r.Maturity,
Stats: f.Stats, Meta: f.Meta, Excerpt: ex, Truncated: cut,
})
}
out, err := json.MarshalIndent(got, "", " ")
if err != nil {
t.Fatal(err)
}
out = append(out, '\n')
path := filepath.Join(dir, "expected.json")
if *update {
if err := os.WriteFile(path, out, 0o644); err != nil {
t.Fatal(err)
}
return
}
want, err := os.ReadFile(path)
if err != nil {
t.Fatalf("%v (run go test ./internal/engine -run TestGolden -update)", err)
}
if !bytes.Equal(bytes.ReplaceAll(want, []byte("\r\n"), []byte("\n")), out) {
t.Errorf("%s differs; got:\n%s", path, out)
}
}
|