Skip to content

Commit

Permalink
Merge pull request #32 from csueiras/fix-issue-30
Browse files Browse the repository at this point in the history
Fix error regarding finding no targetable types
  • Loading branch information
csueiras authored Feb 20, 2021
2 parents 71f6691 + fef3800 commit 5fe60e1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
19 changes: 19 additions & 0 deletions cmd/reinforcer/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,23 @@ func TestRootCommand(t *testing.T) {
c.SetArgs([]string{"--src=/path/to/target.go", "--target=Client", "--target=SomeOtherClient", "--outputdir=./reinforced", "--ignorenoret"})
require.NoError(t, c.Execute())
})

t.Run("No targets found", func(t *testing.T) {
exec := &mocks.Executor{}
exec.On("Execute", &executor.Parameters{
Sources: []string{"/path/to/target.go"},
Targets: []string{},
TargetsAll: true,
OutPkg: "reinforced",
IgnoreNoReturnMethods: false,
}).Return(nil, executor.ErrNoTargetableTypesFound)
writ := &mocks.Writer{}
writ.On("Write", "./reinforced", gen).Return(nil)

b := bytes.NewBufferString("")
c := cmd.NewRootCmd(exec, writ)
c.SetOut(b)
c.SetArgs([]string{"--src=/path/to/target.go", "--targetall", "--outputdir=./reinforced"})
require.EqualError(t, c.Execute(), "failed to generate code; error=no targetable types were discovered")
})
}
7 changes: 7 additions & 0 deletions internal/generator/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"go/types"
)

// ErrNoTargetableTypesFound indicates that no types that could be targeted for code generation were discovered
var ErrNoTargetableTypesFound = fmt.Errorf("no targetable types were discovered")

// Loader describes the loader component
type Loader interface {
LoadAll(path string, mode loader.LoadMode) (map[string]*loader.Result, error)
Expand Down Expand Up @@ -71,6 +74,10 @@ func (e *Executor) Execute(settings *Parameters) (*generator.Generated, error) {
}
}

if len(cfg) == 0 {
return nil, ErrNoTargetableTypesFound
}

code, err := generator.Generate(generator.Config{
OutPkg: settings.OutPkg,
IgnoreNoReturnMethods: settings.IgnoreNoReturnMethods,
Expand Down
58 changes: 38 additions & 20 deletions internal/generator/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,46 @@ import (
)

func TestExecutor_Execute(t *testing.T) {
l := &mocks.Loader{}
l.On("LoadMatched", "./testpkg.go", []string{"MyService"}, loader.FileLoadMode).Return(
map[string]*loader.Result{
"LockService": &loader.Result{
Name: "LockService",
InterfaceType: createTestInterfaceType(),
Package: types.NewPackage("github.com/csueiras/testpkg", "testpkg"),
},
}, nil,
)
t.Run("Loads types", func(t *testing.T) {
l := &mocks.Loader{}
l.On("LoadMatched", "./testpkg.go", []string{"MyService"}, loader.FileLoadMode).Return(
map[string]*loader.Result{
"LockService": &loader.Result{
Name: "LockService",
InterfaceType: createTestInterfaceType(),
Package: types.NewPackage("github.com/csueiras/testpkg", "testpkg"),
},
}, nil,
)

exec := executor.New(l)
got, err := exec.Execute(&executor.Parameters{
Sources: []string{"./testpkg.go"},
Targets: []string{"MyService"},
OutPkg: "testpkg",
IgnoreNoReturnMethods: false,
exec := executor.New(l)
got, err := exec.Execute(&executor.Parameters{
Sources: []string{"./testpkg.go"},
Targets: []string{"MyService"},
OutPkg: "testpkg",
IgnoreNoReturnMethods: false,
})
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, 1, len(got.Files))
require.Equal(t, "LockService", got.Files[0].TypeName)
})

t.Run("No types found", func(t *testing.T) {
l := &mocks.Loader{}
l.On("LoadMatched", "./testpkg.go", []string{"MyService"}, loader.FileLoadMode).
Return(map[string]*loader.Result{}, nil)

exec := executor.New(l)
got, err := exec.Execute(&executor.Parameters{
Sources: []string{"./testpkg.go"},
Targets: []string{"MyService"},
OutPkg: "testpkg",
IgnoreNoReturnMethods: false,
})
require.EqualError(t, err, executor.ErrNoTargetableTypesFound.Error())
require.Nil(t, got)
})
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, 1, len(got.Files))
require.Equal(t, "LockService", got.Files[0].TypeName)
}

func createTestInterfaceType() *types.Interface {
Expand Down

0 comments on commit 5fe60e1

Please sign in to comment.