Skip to content

Commit

Permalink
Do no import index package when no index used. Fixes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Sep 20, 2019
1 parent e9e4ff1 commit ea5d95b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion generator/generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
//go:generate go test -run=TestGenerate -update

import (
"bufio"
"bytes"
"flag"
"fmt"
Expand Down Expand Up @@ -119,6 +120,51 @@ func TestGenerate(t *testing.T) {
})
require.Error(t, err)
})

// Make sure imports are correct when using no index
// https://github.com/asdine/genji/issues/36
t.Run("No index", func(t *testing.T) {
src := `
package s
type S struct {
X,Y,Z string
}
`

expectedHeader := `// Code generated by genji.
// DO NOT EDIT!
package s
import (
"errors"
"github.com/asdine/genji/field"
"github.com/asdine/genji/query"
"github.com/asdine/genji/record"
)
`

var buf bytes.Buffer
err := Generate(&buf, Config{
Sources: []io.Reader{strings.NewReader(src)},
Structs: []Struct{{"S"}},
})
require.NoError(t, err)

rd := bufio.NewReader(&buf)
var res bytes.Buffer

for i := 0; i < 12; i++ {
l, err := rd.ReadString('\n')
require.NoError(t, err)
res.WriteString(l)
}

require.Equal(t, expectedHeader, res.String())
})

}

func TestGeneratedRecords(t *testing.T) {
Expand Down

0 comments on commit ea5d95b

Please sign in to comment.