Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up code generation and CI process #760

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*.a
*.so

# Binaries
codegen

# Folders
_obj
_test
Expand Down
2 changes: 2 additions & 0 deletions .travis.gofmt.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

echo ".travis.gofmt.sh"

if [ -n "$(gofmt -l .)" ]; then
echo "Go code is not formatted:"
gofmt -d .
Expand Down
10 changes: 6 additions & 4 deletions .travis.gogenerate.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/bin/bash

if [[ "$TRAVIS_GO_VERSION" =~ ^1\.[45](\..*)?$ ]]; then
exit 0
fi
echo ".travis.gogenerate.sh"

go build -o codegen ./_codegen
go get github.com/ernesto-jimenez/gogen/imports
go generate ./...
if [ -n "$(git diff)" ]; then
if [ -n "$(git diff assert/ mock/ require/ suite/)" ]; then
echo "Go generate had not been run"
git diff
exit 1
else
echo "Go generate had been run"
fi

5 changes: 4 additions & 1 deletion .travis.govet.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/bin/bash

echo ".travis.govet.sh"

cd "$(dirname $0)"
DIRS=". assert require mock _codegen"
DIRS=". assert require mock suite _codegen"
set -e
for subdir in $DIRS; do
pushd $subdir
go vet
popd
done

17 changes: 12 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ matrix:
env: GO111MODULE=off
- go: "1.11.x"
env: GO111MODULE=on
- go: "1.12.x"
env: GO111MODULE=off
- go: "1.12.x"
env: GO111MODULE=on
- go: tip
script:
- ./.travis.gogenerate.sh
- ./.travis.gofmt.sh
- ./.travis.govet.sh
- go test -v -race $(go list ./... | grep -v vendor)
env: GO111MODULE=off
- go: tip
env: GO111MODULE=on
script:
- ./.travis.gogenerate.sh
- ./.travis.gofmt.sh
- ./.travis.govet.sh
- go test -v -race $(go list ./... | grep -v vendor)
5 changes: 5 additions & 0 deletions _codegen/importer_new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build go1.9

package main

const SOURCE_IMPORTER = true
5 changes: 5 additions & 0 deletions _codegen/importer_old.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !go1.9

package main

const SOURCE_IMPORTER = false
17 changes: 13 additions & 4 deletions _codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"os"
"path"
"regexp"
"runtime/debug"
"strings"
"text/template"

"github.com/ernesto-jimenez/gogen/imports"
)

var (
pkg = flag.String("assert-path", "github.com/stretchr/testify/assert", "Path to the assert package")
pkg = flag.String("assert-path", ".", "Path to the assert package")
includeF = flag.Bool("include-format-funcs", false, "include format functions such as Errorf and Equalf")
outputPkg = flag.String("output-package", "", "package for the resulting code")
tmplFile = flag.String("template", "", "What file to load the function template from")
Expand All @@ -43,12 +44,12 @@ func main() {
log.Fatal(err)
}

importer, funcs, err := analyzeCode(scope, docs)
imp, funcs, err := analyzeCode(scope, docs)
if err != nil {
log.Fatal(err)
}

if err := generateCode(importer, funcs); err != nil {
if err := generateCode(imp, funcs); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -193,14 +194,22 @@ func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) {
fileList[i] = f
}

var imp types.Importer
if SOURCE_IMPORTER {
imp = importer.For("source", nil)
} else {
imp = importer.Default()
}

cfg := types.Config{
Importer: importer.Default(),
Importer: imp,
}
info := types.Info{
Defs: make(map[*ast.Ident]types.Object),
}
tp, err := cfg.Check(pkg, fset, fileList, &info)
if err != nil {
log.Print(string(debug.Stack()))
return nil, nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/pmezard/go-difflib/difflib"
)

//go:generate go run ../_codegen/main.go -output-package=assert -template=assertion_format.go.tmpl
//go:generate ../codegen -output-package=assert -template=assertion_format.go.tmpl

// TestingT is an interface wrapper around *testing.T
type TestingT interface {
Expand Down
2 changes: 1 addition & 1 deletion assert/forward_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ func New(t TestingT) *Assertions {
}
}

//go:generate go run ../_codegen/main.go -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs
//go:generate ../codegen -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs
2 changes: 1 addition & 1 deletion require/forward_requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ func New(t TestingT) *Assertions {
}
}

//go:generate go run ../_codegen/main.go -output-package=require -template=require_forward.go.tmpl -include-format-funcs
//go:generate ../codegen -output-package=require -template=require_forward.go.tmpl -include-format-funcs
2 changes: 1 addition & 1 deletion require/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ type BoolAssertionFunc func(TestingT, bool, ...interface{})
// for table driven tests.
type ErrorAssertionFunc func(TestingT, error, ...interface{})

//go:generate go run ../_codegen/main.go -output-package=require -template=require.go.tmpl -include-format-funcs
//go:generate ../codegen -output-package=require -template=require.go.tmpl -include-format-funcs
2 changes: 1 addition & 1 deletion suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Run(t *testing.T, suite TestingSuite) {
defer failOnPanic(t)

suiteSetupDone := false

methodFinder := reflect.TypeOf(suite)
tests := []testing.InternalTest{}
for index := 0; index < methodFinder.NumMethod(); index++ {
Expand Down
2 changes: 1 addition & 1 deletion suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func TestRunSuite(t *testing.T) {
type SuiteSetupSkipTester struct {
Suite

setUp bool
setUp bool
toreDown bool
}

Expand Down