From fef3800469e32ea4df48ad84fef098eef74cf51d Mon Sep 17 00:00:00 2001 From: Christian Sueiras Date: Sat, 20 Feb 2021 11:08:07 -0500 Subject: [PATCH] Fix error regarding finding no targetable types --- cmd/reinforcer/cmd/root_test.go | 19 +++++++ internal/generator/executor/executor.go | 7 +++ internal/generator/executor/executor_test.go | 58 +++++++++++++------- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/cmd/reinforcer/cmd/root_test.go b/cmd/reinforcer/cmd/root_test.go index f179978..dde5205 100644 --- a/cmd/reinforcer/cmd/root_test.go +++ b/cmd/reinforcer/cmd/root_test.go @@ -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") + }) } diff --git a/internal/generator/executor/executor.go b/internal/generator/executor/executor.go index d29d53d..3f7deca 100644 --- a/internal/generator/executor/executor.go +++ b/internal/generator/executor/executor.go @@ -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) @@ -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, diff --git a/internal/generator/executor/executor_test.go b/internal/generator/executor/executor_test.go index bb15f16..0188d06 100644 --- a/internal/generator/executor/executor_test.go +++ b/internal/generator/executor/executor_test.go @@ -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 {