github.com/kcmvp/gob@v1.0.17/cmd/gbc/command/root_test.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "github.com/fatih/color" 6 "github.com/kcmvp/gob/cmd/gbc/artifact" 7 "github.com/kcmvp/gob/utils" 8 "github.com/samber/lo" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/suite" 11 "io" 12 "io/fs" 13 "os" 14 "path/filepath" 15 "strings" 16 "testing" 17 ) 18 19 type RootTestSuit struct { 20 suite.Suite 21 } 22 23 func TestRootTestSuit(t *testing.T) { 24 suite.Run(t, &RootTestSuit{}) 25 } 26 27 func (suite *RootTestSuit) BeforeTest(_, testName string) { 28 os.Chdir(artifact.CurProject().Root()) 29 s, _ := os.Open(filepath.Join(artifact.CurProject().Root(), "cmd", "gbc", "testdata", "config.json")) 30 _, method := utils.TestCaller() 31 root := filepath.Join(artifact.CurProject().Root(), "target", strings.ReplaceAll(method, "_BeforeTest", fmt.Sprintf("_%s", testName))) 32 os.MkdirAll(root, os.ModePerm) 33 t, _ := os.Create(filepath.Join(root, "config.json")) 34 io.Copy(t, s) 35 t.Close() 36 s.Close() 37 } 38 39 func (suite *RootTestSuit) TearDownSuite() { 40 _, method := utils.TestCaller() 41 TearDownSuite(strings.TrimRight(method, "TearDownSuite")) 42 } 43 44 func (suite *RootTestSuit) TestValidArgs() { 45 args := rootCmd.ValidArgs 46 assert.Equal(suite.T(), 4, len(args)) 47 assert.True(suite.T(), lo.Every(args, []string{"build", "clean", "test", "lint"})) 48 } 49 50 func (suite *RootTestSuit) TestArgs() { 51 artifact.CurProject().SetupHooks(true) 52 tests := []struct { 53 name string 54 args []string 55 wantErr bool 56 }{ 57 { 58 name: "not in valid args list", 59 args: []string{"def"}, 60 wantErr: true, 61 }, 62 { 63 name: "partial valid args", 64 args: []string{"build", "def"}, 65 wantErr: true, 66 }, 67 { 68 name: "no args", 69 args: []string{}, 70 wantErr: true, 71 }, 72 { 73 name: "positive case", 74 args: []string{"clean", "build"}, 75 wantErr: false, 76 }, 77 } 78 for _, test := range tests { 79 rootCmd.SetArgs(test.args) 80 err := Execute() 81 assert.True(suite.T(), test.wantErr == (err != nil)) 82 } 83 84 } 85 86 func (suite *RootTestSuit) TestExecute() { 87 artifact.CurProject().SetupHooks(true) 88 os.Chdir(artifact.CurProject().Target()) 89 rootCmd.SetArgs([]string{"build"}) 90 err := Execute() 91 assert.Equal(suite.T(), "Please execute the command in the project root dir", err.Error()) 92 rootCmd.SetArgs([]string{"cd"}) 93 err = Execute() 94 lo.ForEach([]string{"build", "clean", "test", "lint", "depth"}, func(item string, _ int) { 95 assert.Equal(suite.T(), "invalid argument \"cd\" for gbc", err.Error()) 96 }) 97 os.Chdir(artifact.CurProject().Root()) 98 rootCmd.SetArgs([]string{"build"}) 99 err = Execute() 100 assert.NoError(suite.T(), err) 101 } 102 103 func (suite *RootTestSuit) TestBuild() { 104 artifact.CurProject().SetupHooks(true) 105 tests := []struct { 106 name string 107 args []string 108 wantErr bool 109 }{ 110 { 111 name: "no args", 112 wantErr: true, 113 }, 114 { 115 name: "invalid", 116 args: []string{"cd"}, 117 wantErr: true, 118 }, 119 { 120 name: "valid", 121 args: []string{"build"}, 122 wantErr: false, 123 }, 124 } 125 for _, test := range tests { 126 rootCmd.SetArgs(test.args) 127 err := rootCmd.Execute() 128 assert.True(suite.T(), test.wantErr == (err != nil)) 129 if test.wantErr { 130 assert.True(suite.T(), strings.Contains(err.Error(), color.RedString(""))) 131 } 132 } 133 } 134 135 func (suite *RootTestSuit) TestPersistentPreRun() { 136 artifact.CurProject().SetupHooks(true) 137 rootCmd.SetArgs([]string{"build"}) 138 Execute() 139 hooks := lo.MapToSlice(artifact.HookScripts(), func(key string, _ string) string { 140 return key 141 }) 142 for _, hook := range hooks { 143 _, err := os.Stat(filepath.Join(artifact.CurProject().HookDir(), hook)) 144 assert.NoError(suite.T(), err) 145 } 146 } 147 148 func (suite *RootTestSuit) TestRunE() { 149 target := artifact.CurProject().Target() 150 err := rootCmd.RunE(rootCmd, []string{"build"}) 151 assert.NoError(suite.T(), err) 152 _, err = os.Stat(filepath.Join(target, lo.If(artifact.Windows(), "gbc.exe").Else("gbc"))) 153 assert.NoError(suite.T(), err, "binary should be generated") 154 err = rootCmd.RunE(rootCmd, []string{"build", "clean"}) 155 assert.NoError(suite.T(), err) 156 assert.NoFileExistsf(suite.T(), filepath.Join(target, lo.If(artifact.Windows(), "gob.exe").Else("gob")), "binary should be deleted") 157 err = rootCmd.RunE(rootCmd, []string{"def"}) 158 assert.Errorf(suite.T(), err, "can not find the command def") 159 } 160 161 func (suite *RootTestSuit) TestOutOfRoot() { 162 os.Chdir(artifact.CurProject().Target()) 163 err := Execute() 164 assert.Error(suite.T(), err) 165 assert.True(suite.T(), strings.Contains(err.Error(), "Please execute the command in the project root dir")) 166 } 167 168 func TearDownSuite(prefix string) { 169 filepath.WalkDir(os.TempDir(), func(path string, d fs.DirEntry, err error) error { 170 if d.IsDir() && strings.HasPrefix(d.Name(), prefix) { 171 os.RemoveAll(path) 172 } 173 return nil 174 }) 175 filepath.WalkDir(filepath.Join(artifact.CurProject().Root(), "target"), func(path string, d fs.DirEntry, err error) error { 176 if d.IsDir() && strings.HasPrefix(d.Name(), prefix) { 177 os.RemoveAll(path) 178 } 179 return nil 180 }) 181 }