github.com/youminxue/odin@v0.0.0-20230216022911-c2c8b05d3a41/cmd/version_test.go (about) 1 package cmd_test 2 3 import ( 4 "github.com/golang/mock/gomock" 5 "github.com/pkg/errors" 6 . "github.com/smartystreets/goconvey/convey" 7 "github.com/youminxue/odin/cmd" 8 "github.com/youminxue/odin/cmd/internal/svc" 9 "github.com/youminxue/odin/cmd/mock" 10 "testing" 11 ) 12 13 func Test_versionCmd_Yes(t *testing.T) { 14 Convey("Should not panic and succeed to upgrade when run version command", t, func() { 15 ctrl := gomock.NewController(t) 16 defer ctrl.Finish() 17 prompt := mock.NewMockISelect(ctrl) 18 prompt. 19 EXPECT(). 20 Run(). 21 AnyTimes(). 22 Return(0, "Yes", nil) 23 24 cmd.Prompt = prompt 25 26 runner := mock.NewMockRunner(ctrl) 27 runner. 28 EXPECT(). 29 Run(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). 30 AnyTimes(). 31 Return(nil) 32 33 cmd.VersionSvc = func(dir string, opts ...svc.SvcOption) svc.ISvc { 34 return svc.NewSvc("", svc.WithRunner(runner)) 35 } 36 37 cmd.LatestReleaseVerFunc = func() string { 38 return "v999999.0.0" 39 } 40 defer func() { 41 cmd.LatestReleaseVerFunc = cmd.LatestReleaseVer 42 }() 43 44 So(func() { 45 ExecuteCommandC(cmd.GetRootCmd(), []string{"version"}...) 46 }, ShouldNotPanic) 47 }) 48 } 49 50 func Test_versionCmd_Yes_Panic(t *testing.T) { 51 Convey("Should panic and fail to upgrade when run version command", t, func() { 52 ctrl := gomock.NewController(t) 53 defer ctrl.Finish() 54 prompt := mock.NewMockISelect(ctrl) 55 prompt. 56 EXPECT(). 57 Run(). 58 AnyTimes(). 59 Return(0, "Yes", nil) 60 61 cmd.Prompt = prompt 62 63 runner := mock.NewMockRunner(ctrl) 64 runner. 65 EXPECT(). 66 Run(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). 67 AnyTimes(). 68 Return(errors.New("mock runner error")) 69 70 cmd.VersionSvc = func(dir string, opts ...svc.SvcOption) svc.ISvc { 71 return svc.NewSvc("", svc.WithRunner(runner)) 72 } 73 74 cmd.LatestReleaseVerFunc = func() string { 75 return "v999999.0.0" 76 } 77 defer func() { 78 cmd.LatestReleaseVerFunc = cmd.LatestReleaseVer 79 }() 80 81 So(func() { 82 ExecuteCommandC(cmd.GetRootCmd(), []string{"version"}...) 83 }, ShouldPanic) 84 }) 85 }