1
1
mirror of https://github.com/wader/fq.git synced 2024-11-27 14:14:58 +03:00
fq/format/fuzz_test.go

105 lines
2.3 KiB
Go
Raw Normal View History

//go:build fuzz
2020-06-08 03:29:51 +03:00
package format_test
import (
"bytes"
"context"
"fmt"
"io"
"io/fs"
2020-06-08 03:29:51 +03:00
"io/ioutil"
"os"
"path/filepath"
"testing"
_ "github.com/wader/fq/format/all"
"github.com/wader/fq/format/registry"
"github.com/wader/fq/pkg/interp"
2020-06-08 03:29:51 +03:00
)
type fuzzFS struct{}
func (fuzzFS) Open(name string) (fs.File, error) {
return nil, fmt.Errorf("%s: file not found", name)
}
2020-06-08 03:29:51 +03:00
type fuzzTest struct {
b []byte
}
type fuzzTestInput struct {
interp.FileReader
io.Writer
}
func (fuzzTestInput) IsTerminal() bool { return false }
func (fuzzTestInput) Size() (int, int) { return 120, 25 }
2020-06-08 03:29:51 +03:00
type fuzzTestOutput struct {
io.Writer
}
func (o fuzzTestOutput) Size() (int, int) { return 120, 25 }
func (o fuzzTestOutput) IsTerminal() bool { return false }
func (ft *fuzzTest) Stdin() interp.Input {
return fuzzTestInput{FileReader: interp.FileReader{R: bytes.NewBuffer(ft.b)}}
}
2021-11-01 14:57:55 +03:00
func (ft *fuzzTest) Stdout() interp.Output { return fuzzTestOutput{os.Stdout} }
func (ft *fuzzTest) Stderr() interp.Output { return fuzzTestOutput{os.Stderr} }
2021-11-01 14:57:55 +03:00
func (ft *fuzzTest) InterruptChan() chan struct{} { return nil }
func (ft *fuzzTest) Environ() []string { return nil }
2020-06-08 03:29:51 +03:00
func (ft *fuzzTest) Args() []string {
return []string{
`fq`,
`-d`, `raw`,
`(_registry.groups | keys[] | select(. != "all")) as $f | decode($f)?`,
}
2020-06-08 03:29:51 +03:00
}
func (ft *fuzzTest) ConfigDir() (string, error) { return "/config", nil }
func (ft *fuzzTest) FS() fs.FS { return fuzzFS{} }
func (ft *fuzzTest) History() ([]string, error) { return nil, nil }
2020-06-08 03:29:51 +03:00
func (ft *fuzzTest) Readline(prompt string, complete func(line string, pos int) (newLine []string, shared int)) (string, error) {
return "", io.EOF
}
func FuzzFormats(f *testing.F) {
2020-06-08 03:29:51 +03:00
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".fqtest" {
return nil
}
if filepath.Base(filepath.Dir(path)) != "testdata" {
return nil
}
if st, err := os.Stat(path); err != nil || st.IsDir() {
return nil
}
b, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
f.Add(b)
return nil
})
f.Fuzz(func(t *testing.T, b []byte) {
fz := &fuzzTest{b: b}
q, err := interp.New(fz, registry.Default)
2020-06-08 03:29:51 +03:00
if err != nil {
t.Fatal(err)
}
_ = q.Main(context.Background(), fz.Stdout(), "dev")
// if err != nil {
// // TODO: expect error
// t.Fatal(err)
// }
2020-06-08 03:29:51 +03:00
})
}