Skip to content

Commit 208aa67

Browse files
committed
pkg/clangtool/tooltest: add LoadOutput helper
Add LoadOutput helper to use in future commits, and switch to osutil.ReadJSON helper.
1 parent eefcfd0 commit 208aa67

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

pkg/clangtool/clangtool.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ type OutputDataPtr[T any] interface {
4242
// It always caches results, and optionally reuses previously cached results.
4343
func Run[Output any, OutputPtr OutputDataPtr[Output]](cfg *Config) (OutputPtr, error) {
4444
if cfg.CacheFile != "" {
45-
data, err := os.ReadFile(cfg.CacheFile)
45+
out, err := osutil.ReadJSON[OutputPtr](cfg.CacheFile)
4646
if err == nil {
47-
out, err := unmarshal[Output, OutputPtr](data)
48-
if err == nil {
49-
return out, nil
50-
}
47+
return out, nil
5148
}
5249
}
5350

@@ -111,7 +108,7 @@ func runTool[Output any, OutputPtr OutputDataPtr[Output]](cfg *Config, dbFile, f
111108
}
112109
return nil, err
113110
}
114-
out, err := unmarshal[Output, OutputPtr](data)
111+
out, err := osutil.ParseJSON[OutputPtr](data)
115112
if err != nil {
116113
return nil, err
117114
}
@@ -127,16 +124,6 @@ func runTool[Output any, OutputPtr OutputDataPtr[Output]](cfg *Config, dbFile, f
127124
return out, nil
128125
}
129126

130-
func unmarshal[Output any, OutputPtr OutputDataPtr[Output]](data []byte) (OutputPtr, error) {
131-
dec := json.NewDecoder(bytes.NewReader(data))
132-
dec.DisallowUnknownFields()
133-
out := OutputPtr(new(Output))
134-
if err := dec.Decode(out); err != nil {
135-
return nil, fmt.Errorf("failed to unmarshal clang tool output: %w\n%s", err, data)
136-
}
137-
return out, nil
138-
}
139-
140127
type compileCommand struct {
141128
Command string
142129
Directory string

pkg/clangtool/tooltest/tooltest.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ func TestClangTool[Output any, OutputPtr clangtool.OutputDataPtr[Output]](t *tes
3939
})
4040
}
4141

42+
func LoadOutput[Output any, OutputPtr clangtool.OutputDataPtr[Output]](t *testing.T) OutputPtr {
43+
out := OutputPtr(new(Output))
44+
forEachTestFile(t, func(t *testing.T, file string) {
45+
tmp, err := osutil.ReadJSON[OutputPtr](file + ".json")
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
out.Merge(tmp)
50+
})
51+
out.SortAndDedup()
52+
return out
53+
}
54+
4255
func ForEachTestFile(t *testing.T, fn func(t *testing.T, cfg *clangtool.Config, file string)) {
4356
forEachTestFile(t, func(t *testing.T, file string) {
4457
t.Run(filepath.Base(file), func(t *testing.T) {

0 commit comments

Comments
 (0)