diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2025-11-17 08:25:04 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2025-11-17 08:54:02 +0000 |
| commit | 208aa6706e1115fb0a18faf0fa72474ea2587184 (patch) | |
| tree | 428712b33de87b0f6bbd648f7f02dfe29ea4aac9 /pkg/clangtool | |
| parent | eefcfd016a5cb195a9a9c86722a2b15aade8b59a (diff) | |
pkg/clangtool/tooltest: add LoadOutput helper
Add LoadOutput helper to use in future commits,
and switch to osutil.ReadJSON helper.
Diffstat (limited to 'pkg/clangtool')
| -rw-r--r-- | pkg/clangtool/clangtool.go | 19 | ||||
| -rw-r--r-- | pkg/clangtool/tooltest/tooltest.go | 13 |
2 files changed, 16 insertions, 16 deletions
diff --git a/pkg/clangtool/clangtool.go b/pkg/clangtool/clangtool.go index 8ebc40dc2..9b9f9387d 100644 --- a/pkg/clangtool/clangtool.go +++ b/pkg/clangtool/clangtool.go @@ -42,12 +42,9 @@ type OutputDataPtr[T any] interface { // It always caches results, and optionally reuses previously cached results. func Run[Output any, OutputPtr OutputDataPtr[Output]](cfg *Config) (OutputPtr, error) { if cfg.CacheFile != "" { - data, err := os.ReadFile(cfg.CacheFile) + out, err := osutil.ReadJSON[OutputPtr](cfg.CacheFile) if err == nil { - out, err := unmarshal[Output, OutputPtr](data) - if err == nil { - return out, nil - } + return out, nil } } @@ -111,7 +108,7 @@ func runTool[Output any, OutputPtr OutputDataPtr[Output]](cfg *Config, dbFile, f } return nil, err } - out, err := unmarshal[Output, OutputPtr](data) + out, err := osutil.ParseJSON[OutputPtr](data) if err != nil { return nil, err } @@ -127,16 +124,6 @@ func runTool[Output any, OutputPtr OutputDataPtr[Output]](cfg *Config, dbFile, f return out, nil } -func unmarshal[Output any, OutputPtr OutputDataPtr[Output]](data []byte) (OutputPtr, error) { - dec := json.NewDecoder(bytes.NewReader(data)) - dec.DisallowUnknownFields() - out := OutputPtr(new(Output)) - if err := dec.Decode(out); err != nil { - return nil, fmt.Errorf("failed to unmarshal clang tool output: %w\n%s", err, data) - } - return out, nil -} - type compileCommand struct { Command string Directory string diff --git a/pkg/clangtool/tooltest/tooltest.go b/pkg/clangtool/tooltest/tooltest.go index b9ad18a83..14681946b 100644 --- a/pkg/clangtool/tooltest/tooltest.go +++ b/pkg/clangtool/tooltest/tooltest.go @@ -39,6 +39,19 @@ func TestClangTool[Output any, OutputPtr clangtool.OutputDataPtr[Output]](t *tes }) } +func LoadOutput[Output any, OutputPtr clangtool.OutputDataPtr[Output]](t *testing.T) OutputPtr { + out := OutputPtr(new(Output)) + forEachTestFile(t, func(t *testing.T, file string) { + tmp, err := osutil.ReadJSON[OutputPtr](file + ".json") + if err != nil { + t.Fatal(err) + } + out.Merge(tmp) + }) + out.SortAndDedup() + return out +} + func ForEachTestFile(t *testing.T, fn func(t *testing.T, cfg *clangtool.Config, file string)) { forEachTestFile(t, func(t *testing.T, file string) { t.Run(filepath.Base(file), func(t *testing.T) { |
