func (t Tool) noError(err error) {
	if err != nil {
		t.test.Fatalf("golden: %s", err)
	}
}
noError fails the test if an err is not nil.
package golden
import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"unicode"
	"github.com/stretchr/testify/assert"
)
type TestingTB interface {
	Name() string
	Logf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	FailNow()
	Fail()
}
type testingHelper interface {
	Helper()
}
type Tool struct {
	test      TestingTB
	dir       string
	fileMode  os.FileMode
	modeDir   os.FileMode
	target    target
	flag      *bool
	prefix    string
	extension string
	want []byte
	mkdirAll  func(path string, perm os.FileMode) error
	readFile  func(filename string) ([]byte, error)
	remove    func(name string) error
	stat      func(name string) (os.FileInfo, error)
	writeFile func(filename string, data []byte, perm os.FileMode) error
}
var _golden = Tool{
	dir:      "testdata",
	fileMode: 0644,
	modeDir:  0755,
	target:   Golden,
	mkdirAll:  os.MkdirAll,
	readFile:  ioutil.ReadFile,
	remove:    os.Remove,
	stat:      os.Stat,
	writeFile: ioutil.WriteFile,
}
const updateEnvName = "GOLDEN_UPDATE"
func getUpdateEnv() bool {
	env := os.Getenv(updateEnvName)
	if env == "" {
		env = strconv.FormatBool(false)
	}
	is, err := strconv.ParseBool(env)
	if err != nil {
		const msg = "cannot parse flag %q, error: %v"
		panic(fmt.Sprintf(msg, updateEnvName, err))
	}
	return is
}
func init() {
	_golden.flag = flag.Bool("getUpdateEnv", getUpdateEnv(), "update test golden files")
}
func Assert(t TestingTB, got []byte) {
	if h, ok := t.(testingHelper); ok {
		h.Helper()
	}
	SetTest(t).Assert(got)
}
func Equal(t TestingTB, got []byte) Conclusion {
	if h, ok := t.(testingHelper); ok {
		h.Helper()
	}
	return SetTest(t).Equal(got)
}
func Read(t TestingTB) []byte {
	return SetTest(t).SetTarget(Input).Read()
}
func Run(t TestingTB, do func(input []byte) (outcome []byte, err error)) {
	SetTest(t).Run(do)
}
func SetTest(t TestingTB) Tool {
	return _golden.SetTest(t)
}
func SetWant(t TestingTB, bs []byte) Tool {
	return _golden.SetTest(t).SetWant(bs)
}
func (t Tool) Assert(got []byte) {
	t.Update(got)
	if h, ok := t.test.(testingHelper); ok {
		h.Helper()
	}
	t.Equal(got).FailNow()
}
func (t Tool) Equal(got []byte) Conclusion {
	t.Update(got)
	if h, ok := t.test.(testingHelper); ok {
		h.Helper()
	}
	want := t.SetTarget(Golden).Read()
	if want == nil {
		want = []byte(fmt.Sprintf("%#v", want))
	}
	if got == nil {
		got = []byte(fmt.Sprintf("%#v", got))
	}
	i := new(interceptor)
	c := newConclusion(t.test)
	c.successful = assert.Equal(i, string(want), string(got))
	c.diff = i
	return c
}
func (t Tool) JSONEq(got string) Conclusion {
	if h, ok := t.test.(testingHelper); ok {
		h.Helper()
	}
	return t.jsonEqual(got)
}
func (t Tool) jsonEqual(got string) conclusion {
	t.setExtension("json").update(func() []byte {
		return []byte(jsonFormatter(t.test, got))
	})
	want := t.setExtension("json").SetTarget(Golden).Read()
	i := new(interceptor)
	c := newConclusion(t.test)
	c.successful = assert.JSONEq(i, string(want), string(got))
	c.diff = i
	return c
}
func JSONEq(tb TestingTB, got string) Conclusion {
	if h, ok := tb.(testingHelper); ok {
		h.Helper()
	}
	return _golden.SetTest(tb).jsonEqual(got)
}
func (t Tool) Read() (bs []byte) {
	if t.want != nil {
		t.test.Logf("golden: read the value from the want field")
		bs = make([]byte, len(t.want))
		copy(bs, t.want)
		return bs
	}
	bs, err := t.readFile(t.path())
	if os.IsNotExist(err) {
		const f = "golden: read the value of nil since it is not found file: %s"
		t.test.Logf(f, t.path())
		return nil
	} else if err != nil {
		t.test.Fatalf("golden: %s", err)
	}
	return bs
}
func (t Tool) Run(do func(input []byte) (got []byte, err error)) {
	bs, err := do(t.SetTarget(Input).Read())
	t.noError(err)
	t.Assert(bs)
}
func (t Tool) SetPrefix(prefix string) Tool {
	t.prefix = rewrite(prefix)
	return t
}
func (t Tool) SetTarget(tar target) Tool {
	t.target = tar
	return t
}
func (t Tool) SetTest(tb TestingTB) Tool {
	t.test = tb
	return t
}
func (t Tool) SetWant(bs []byte) Tool {
	t.want = nil
	if bs != nil {
		t.want = make([]byte, len(bs))
		copy(t.want, bs)
	}
	return t
}
func (t Tool) Update(bs []byte) {
	t.update(func() []byte { return bs })
}
func (t Tool) write(bs []byte) {
	path := t.path()
	t.mkdir(filepath.Dir(path))
	t.test.Logf("golden: start write to file: %s", path)
	if bs == nil {
		t.test.Logf("golden: nil value will not be written")
		fileInfo, err := t.stat(path)
		if err == nil && !fileInfo.IsDir() {
			t.test.Logf("golden: current test bytes file will be deleted")
			t.noError(t.remove(path))
		}
		if !os.IsNotExist(err) {
			t.noError(err)
		}
	} else {
		t.noError(t.writeFile(path, bs, t.fileMode))
	}
}
func (t Tool) mkdir(loc string) {
	fileInfo, err := t.stat(loc)
	switch {
	case err != nil && os.IsNotExist(err):
		t.test.Logf("golden: trying to create a directory: %q", loc)
		err = t.mkdirAll(loc, t.modeDir)
	case err == nil && !fileInfo.IsDir():
		t.test.Errorf("golden: test dir is a file: %s", loc)
	}
	t.noError(err)
}