Skip to content

Commit

Permalink
test: write stubs on tests run (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane authored Feb 18, 2023
1 parent 254ea0b commit eeb9c72
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 81 deletions.
87 changes: 77 additions & 10 deletions musttag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package musttag
import (
"go/token"
"io"
"path"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -12,14 +13,11 @@ import (
)

func TestAnalyzer(t *testing.T) {
// NOTE(junk1tm): analysistest isn't aware of the main package's modules
// (see https://github.com/golang/go/issues/37054), so to run tests with
// external dependencies we have to be creative. Using vendor with symlinks
// would work but the paths would contain the `vendor/` prefix, and that's
// not what we want because we match full paths. The solution is to write
// stubs of the dependencies (we don't need the actual code, only the
// functions signatures to match) and to put them exactly at
// testdata/src/path/to/pkg (GOPATH?), otherwise it won't work.
// NOTE(junk1tm): analysistest does not yet support modules
// (see https://github.com/golang/go/issues/37054 for details).
// So, to be able to run tests with external dependencies,
// we first need to write a GOPATH-like tree of stubs.
prepareTestFiles(t)

testPackages = []string{"tests", "examples"}

Expand Down Expand Up @@ -86,5 +84,74 @@ func shortName(name string) string {
name = strings.ReplaceAll(name, "*", "")
name = strings.ReplaceAll(name, "(", "")
name = strings.ReplaceAll(name, ")", "")
return path.Base(name)
return filepath.Base(name)
}

func prepareTestFiles(t *testing.T) {
testdata := analysistest.TestData()

t.Cleanup(func() {
_ = os.RemoveAll(filepath.Join(testdata, "src"))
})

hardlink := func(dir, file string) {
target := filepath.Join(testdata, "src", dir, file)
if err := os.MkdirAll(filepath.Dir(target), 0o777); err != nil {
t.Fatal(err)
}
if err := os.Link(filepath.Join(testdata, file), target); err != nil {
t.Fatal(err)
}
}
hardlink("tests", "tests.go")
hardlink("examples", "examples.go")

for file, data := range stubs {
target := filepath.Join(testdata, "src", file)
if err := os.MkdirAll(filepath.Dir(target), 0o777); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(target, []byte(data), 0o666); err != nil {
t.Fatal(err)
}
}
}

var stubs = map[string]string{
"gopkg.in/yaml.v3/yaml.go": `package yaml
import "io"
func Marshal(_ any) ([]byte, error) { return nil, nil }
func Unmarshal(_ []byte, _ any) error { return nil }
type Encoder struct{}
func NewEncoder(_ io.Writer) *Encoder { return nil }
func (*Encoder) Encode(_ any) error { return nil }
type Decoder struct{}
func NewDecoder(_ io.Reader) *Decoder { return nil }
func (*Decoder) Decode(_ any) error { return nil }`,

"github.com/BurntSushi/toml/toml.go": `package toml
import "io"
import "io/fs"
func Unmarshal(_ []byte, _ any) error { return nil }
type MetaData struct{}
func Decode(_ string, _ any) (MetaData, error) { return MetaData{}, nil }
func DecodeFS(_ fs.FS, _ string, _ any) (MetaData, error) { return MetaData{}, nil }
func DecodeFile(_ string, _ any) (MetaData, error) { return MetaData{}, nil }
type Encoder struct{}
func NewEncoder(_ io.Writer) *Encoder { return nil }
func (*Encoder) Encode(_ any) error { return nil }
type Decoder struct{}
func NewDecoder(_ io.Reader) *Decoder { return nil }
func (*Decoder) Decode(_ any) error { return nil }`,

"github.com/mitchellh/mapstructure/mapstructure.go": `package mapstructure
type Metadata struct{}
func Decode(_, _ any) error { return nil }
func DecodeMetadata(_, _ any, _ *Metadata) error { return nil }
func WeakDecode(_, _ any) error { return nil }
func WeakDecodeMetadata(_, _ any, _ *Metadata) error { return nil }`,

"example.com/custom/custom.go": `package custom
func Marshal(_ any) ([]byte, error) { return nil, nil }
func Unmarshal(_ []byte, _ any) error { return nil }`,
}
File renamed without changes.
5 changes: 0 additions & 5 deletions testdata/src/example.com/custom/custom.go

This file was deleted.

3 changes: 0 additions & 3 deletions testdata/src/example.com/custom/go.mod

This file was deleted.

3 changes: 0 additions & 3 deletions testdata/src/github.com/BurntSushi/toml/go.mod

This file was deleted.

25 changes: 0 additions & 25 deletions testdata/src/github.com/BurntSushi/toml/toml.go

This file was deleted.

3 changes: 0 additions & 3 deletions testdata/src/github.com/mitchellh/mapstructure/go.mod

This file was deleted.

12 changes: 0 additions & 12 deletions testdata/src/github.com/mitchellh/mapstructure/mapstructure.go

This file was deleted.

3 changes: 0 additions & 3 deletions testdata/src/gopkg.in/yaml.v3/go.mod

This file was deleted.

17 changes: 0 additions & 17 deletions testdata/src/gopkg.in/yaml.v3/yaml.go

This file was deleted.

File renamed without changes.

0 comments on commit eeb9c72

Please sign in to comment.