github.com/gofiber/fiber-cli@v0.0.3/cmd/root_test.go (about) 1 package cmd 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "net/http" 8 "testing" 9 "time" 10 11 "github.com/jarcoal/httpmock" 12 "github.com/spf13/cobra" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func Test_Root_Execute(t *testing.T) { 17 setupOsExit() 18 defer teardownOsExit() 19 20 at, b := setupRootCmd(t) 21 22 oldRunE := rootCmd.RunE 23 24 rootCmd.RunE = func(_ *cobra.Command, _ []string) error { 25 return fmt.Errorf("fake error") 26 } 27 28 Execute() 29 30 rootCmd.RunE = oldRunE 31 32 at.Contains(b.String(), "fake error") 33 } 34 35 func Test_Root_RunE(t *testing.T) { 36 at, b := setupRootCmd(t) 37 38 at.Nil(rootRunE(rootCmd, nil)) 39 40 at.Contains(b.String(), "fiber") 41 } 42 43 func Test_Root_RootPersistentPreRun(t *testing.T) { 44 at, b := setupRootCmd(t) 45 46 homeDir = setupHomeDir(t, "RootPersistentPreRun") 47 defer teardownHomeDir(homeDir) 48 49 oldFileExist := fileExist 50 fileExist = func(_ string) bool { return true } 51 defer func() { fileExist = oldFileExist }() 52 53 rootPersistentPreRun(rootCmd, nil) 54 55 at.Contains(b.String(), "failed to load") 56 } 57 58 func Test_Root_RootPersistentPostRun(t *testing.T) { 59 at, b := setupRootCmd(t) 60 61 rc.CliVersionCheckedAt = time.Now().Unix() 62 63 rootPersistentPostRun(rootCmd, nil) 64 65 rc.CliVersionCheckedAt = 0 66 67 at.Equal(0, b.Len()) 68 } 69 70 func Test_Root_CheckCliVersion(t *testing.T) { 71 at, b := setupRootCmd(t) 72 73 httpmock.Activate() 74 defer httpmock.DeactivateAndReset() 75 76 httpmock.RegisterResponder(http.MethodGet, latestCliVersionUrl, httpmock.NewErrorResponder(errors.New("network error"))) 77 78 checkCliVersion(rootCmd) 79 80 at.Equal(0, b.Len()) 81 82 homeDir = setupHomeDir(t, "CheckCliVersion") 83 defer teardownHomeDir(homeDir) 84 85 httpmock.RegisterResponder(http.MethodGet, latestCliVersionUrl, httpmock.NewBytesResponder(200, fakeCliVersionResponse())) 86 87 checkCliVersion(rootCmd) 88 89 at.Contains(b.String(), "WARNING") 90 91 at.InDelta(time.Now().Unix(), rc.CliVersionCheckedAt, 1) 92 } 93 94 func Test_Root_NeedCheckCliVersion(t *testing.T) { 95 rc.CliVersionCheckedAt = 0 96 97 assert.True(t, needCheckCliVersion()) 98 } 99 100 func setupRootCmd(t *testing.T) (*assert.Assertions, *bytes.Buffer) { 101 at := assert.New(t) 102 103 b := &bytes.Buffer{} 104 rootCmd.SetErr(b) 105 rootCmd.SetOut(b) 106 107 return at, b 108 } 109 110 var latestCliVersionUrl = "https://api.github.com/repos/gofiber/fiber-cli/releases/latest" 111 112 var fakeCliVersionResponse = func(version ...string) []byte { 113 v := "99.99.99" 114 if len(version) > 0 { 115 v = version[0] 116 } 117 return []byte(fmt.Sprintf(`{ "assets": [], "assets_url": "https://api.github.com/repos/gofiber/fiber-cli/releases/32630724/assets", "author": { "avatar_url": "https://avatars1.githubusercontent.com/u/1214670?v=4", "events_url": "https://api.github.com/users/kiyonlin/events{/privacy}", "followers_url": "https://api.github.com/users/kiyonlin/followers", "following_url": "https://api.github.com/users/kiyonlin/following{/other_user}", "gists_url": "https://api.github.com/users/kiyonlin/gists{/gist_id}", "gravatar_id": "", "html_url": "https://github.com/kiyonlin", "id": 1214670, "login": "kiyonlin", "node_id": "MDQ6VXNlcjEyMTQ2NzA=", "organizations_url": "https://api.github.com/users/kiyonlin/orgs", "received_events_url": "https://api.github.com/users/kiyonlin/received_events", "repos_url": "https://api.github.com/users/kiyonlin/repos", "site_admin": false, "starred_url": "https://api.github.com/users/kiyonlin/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/kiyonlin/subscriptions", "type": "User", "url": "https://api.github.com/users/kiyonlin" }, "created_at": "2020-10-15T15:58:55Z", "draft": false, "html_url": "https://github.com/gofiber/fiber-cli/releases/tag/v99.99.99", "id": 32630724, "name": "v%s", "node_id": "MDc6UmVsZWFzZTMyNjMwNzI0", "prerelease": false, "published_at": "2020-10-15T16:09:05Z", "tag_name": "v99.99.99", "tarball_url": "https://api.github.com/repos/gofiber/fiber-cli/tarball/v99.99.99", "target_commitish": "master", "upload_url": "https://uploads.github.com/repos/gofiber/fiber-cli/releases/32630724/assets{?name,label}", "url": "https://api.github.com/repos/gofiber/fiber-cli/releases/32630724", "zipball_url": "https://api.github.com/repos/gofiber/fiber-cli/zipball/v99.99.99"}`, v)) 118 }