Skip to content

Commit

Permalink
feat(validate): Validate struct name before generating (#61)
Browse files Browse the repository at this point in the history
* feat(types): handle types not found in input files by emitting error (#1)

* Feat/handle not found types (#3)

* feat(types): handle types not found in input files by emitting error

* ci(actions): enable actions

* ci(actions): enable actions (#4)

* Ci/actiosn (#5)

* ci(actions): enable actions

* ci(actions): enable actions

* fix(ci): remove releaser from actions (#6)

* feat(validate): input file for struct provided (#7)
  • Loading branch information
karthick-கார்த்திக் authored Aug 28, 2023
1 parent 4c9ff22 commit 975a959
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 213 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: test

on:
push:
branches:
- '*'
tags:
- '*'

permissions:
contents: write
# packages: write
# issues: write

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v3
with:
go-version: '>=1.19.3'
cache: true
- run: go test -v -run ./...
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30 changes: 0 additions & 30 deletions .travis.yml

This file was deleted.

14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module github.com/vburenin/ifacemaker

go 1.18
go 1.20

require (
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20
github.com/stretchr/testify v1.2.2
golang.org/x/tools v0.1.10
github.com/jessevdk/go-flags v1.5.0
github.com/stretchr/testify v1.8.4
golang.org/x/tools v0.11.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
27 changes: 15 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20 h1:dAOsPLhnBzIyxu0VvmnKjlNcIlgMK+erD6VRHDtweMI=
github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 5 additions & 3 deletions ifacemaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/jessevdk/go-flags"

"github.com/vburenin/ifacemaker/maker"
)

Expand Down Expand Up @@ -82,7 +80,11 @@ func main() {
if args.Output == "" {
fmt.Println(string(result))
} else {
if err := ioutil.WriteFile(args.Output, result, 0644); err != nil {
f, err := os.Create(args.Output)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write(result); err != nil {
log.Fatal(err)
}
}
Expand Down
109 changes: 50 additions & 59 deletions ifacemaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ import (
"fmt"
"io"
"os"
"strings"
"path/filepath"
"testing"

assert "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)

var src = `package main
import (
"fmt"
)
// Person contains data related to a person.
type Person struct {
name string
Expand All @@ -36,12 +32,13 @@ func (p *Person) SetName(name string) {
// Age ...
func (p *Person) Age() int {
return p.Age
return p.age
}
// Age ...
func (p *Person) SetAge(age int) int {
p.Age = age
p.age = age
return p.age
}
// AgeAndName ...
Expand All @@ -68,7 +65,9 @@ func (p *Person) SetNameAndTelephone(name, telephone string) {
func SomeFunction() string {
return "Something"
}`
}
`

var src2 = `package maker
Expand Down Expand Up @@ -117,14 +116,14 @@ func (s *Smiter) Smite(weapon Hammer) error {
}
`

var srcFile = os.TempDir() + "/ifacemaker_src.go"
var srcFile2 = os.TempDir() + "/test_impl.go"
var srcFile2_ext = os.TempDir() + "/test_impl_extended.go"
var srcFile3 = os.TempDir() + "/footest/footest.go"
var srcFile4 = os.TempDir() + "/footest/smiter.go"
var srcFile = filepath.Join(os.TempDir(), "ifacemaker_src.go")
var srcFile2 = filepath.Join(os.TempDir(), "test_impl.go")
var srcFile2_ext = filepath.Join(os.TempDir(), "test_impl_extended.go")
var srcFile3 = filepath.Join(os.TempDir(), "footest", "footest.go")
var srcFile4 = filepath.Join(os.TempDir(), "footest", "smiter.go")

func TestMain(m *testing.M) {
dirPath := os.TempDir() + "/footest"
dirPath := filepath.Join(os.TempDir(), "footest")
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err := os.Mkdir(dirPath, os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -157,7 +156,6 @@ func TestMainAllArgs(t *testing.T) {
out := captureStdout(func() {
main()
})

expected := `// DO NOT EDIT: Auto generated
package gen
Expand Down Expand Up @@ -187,11 +185,6 @@ type PersonIface interface {
}

func TestMainNoIfaceComment(t *testing.T) {
os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-D"}
out := captureStdout(func() {
main()
})

expected := `// DO NOT EDIT: Auto generated
package gen
Expand All @@ -216,16 +209,16 @@ type PersonIface interface {
}
`
os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-D"}
out := captureStdout(func() {
main()
})

assert.Equal(t, expected, out)

}

func TestMainNoCopyTypeDocs(t *testing.T) {
os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-y", "PersonIface is an interface for Person."}
out := captureStdout(func() {
main()
})

expected := `// DO NOT EDIT: Auto generated
package gen
Expand All @@ -249,16 +242,15 @@ type PersonIface interface {
}
`

assert.Equal(t, expected, out)
}

func TestMainNoCopyMethodDocs(t *testing.T) {
os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-d=false"}
os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-y", "PersonIface is an interface for Person."}
out := captureStdout(func() {
main()
})
assert.Equal(t, expected, out)

}

func TestMainNoCopyMethodDocs(t *testing.T) {
expected := `// DO NOT EDIT: Auto generated
package gen
Expand All @@ -276,16 +268,16 @@ type PersonIface interface {
}
`
os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-d=false"}
out := captureStdout(func() {
main()
})

assert.Equal(t, expected, out)

}

func TestMainDoNotImportPackageName(t *testing.T) {
os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"}
out := captureStdout(func() {
main()
})

expected := `// DO NOT EDIT: Auto generated
package footest
Expand All @@ -297,20 +289,15 @@ type TestInterface interface {
}
`
os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"}
out := captureStdout(func() {
main()
})

assert.Equal(t, expected, out)
}

func TestMainFileGlob(t *testing.T) {
src := strings.Replace(srcFile2, "test_impl.go", "test*.go", 1)
//assert.True(t, strings.HasSuffix(srcFile2, "test*.go"))
//assert.Equal(t, "bla", src)
//ssert.Contains(t, src, "test*")
os.Args = []string{"cmd", "-f", src, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"}
out := captureStdout(func() {
main()
})

expected := `// DO NOT EDIT: Auto generated
package footest
Expand All @@ -319,20 +306,18 @@ package footest
type TestInterface interface {
GetUser(userID string) *User
CreateUser(user *User) (*User, error)
UpdateUser(userID string) *User
}
`

assert.Equal(t, expected, out)
}

func TestMainDefaultComment(t *testing.T) {
os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-i", "TestInterface", "-d=false"}
os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"}
out := captureStdout(func() {
main()
})
assert.Equal(t, expected, out)

}

func TestMainDefaultComment(t *testing.T) {
expected := `// Code generated by ifacemaker; DO NOT EDIT.
package footest
Expand All @@ -344,16 +329,15 @@ type TestInterface interface {
}
`
os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-i", "TestInterface", "-d=false"}
out := captureStdout(func() {
main()
})

assert.Equal(t, expected, out)
}

func TestMainUsingDeclarationInSamePackage(t *testing.T) {
os.Args = []string{"cmd", "-f", srcFile4, "-m", "github.com/test/footest", "-s", "Smiter", "-i", "Smiter", "-p", "another", "-c", "DO NOT EDIT: Auto generated", "-d=false"}
out := captureStdout(func() {
main()
})

expected := `// DO NOT EDIT: Auto generated
package another
Expand All @@ -368,6 +352,10 @@ type Smiter interface {
}
`
os.Args = []string{"cmd", "-f", srcFile4, "-m", "github.com/test/footest", "-s", "Smiter", "-i", "Smiter", "-p", "another", "-c", "DO NOT EDIT: Auto generated", "-d=false"}
out := captureStdout(func() {
main()
})

assert.Equal(t, expected, out)
}
Expand All @@ -384,6 +372,9 @@ func captureStdout(f func()) string {
os.Stdout = old

var buf bytes.Buffer
io.Copy(&buf, r)
if _, err := io.Copy(&buf, r); err != nil {
fmt.Printf("error:[%v] copying file", err)
return ""
}
return buf.String()
}
Loading

0 comments on commit 975a959

Please sign in to comment.