Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#81: add test coverage for awesome list #82

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ gob plugin list
List all the installed plugins

### gob setup version
```shell
gob setup version
```
This command will generate file `version.go` in `infra` folder, and project version informatill
will be injected into `buildVersion` when build the project with command `gob build`
```go
// buildVersion don't change this
var buildVersion string
```

## FAQ

Expand Down
21 changes: 0 additions & 21 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ import (
"github.com/spf13/cobra"
)

var (
CleanCache bool
CleanTestCache bool
CleanModCache bool
)

const (
cleanCacheFlag = "cache"
cleanTestCacheFlag = "testcache"
cleanModCacheFlag = "modcache"
)

type (
Execution func(cmd *cobra.Command, args ...string) error
Action lo.Tuple2[string, Execution]
Expand Down Expand Up @@ -128,15 +116,6 @@ func cleanAction(_ *cobra.Command, _ ...string) error {
fmt.Println("Clean target folder successfully !")
// clean cache
args := []string{"clean"}
if CleanCache {
args = append(args, fmt.Sprintf("--%s", cleanCacheFlag))
}
if CleanTestCache {
args = append(args, fmt.Sprintf("--%s", cleanTestCacheFlag))
}
if CleanModCache {
args = append(args, fmt.Sprintf("--%s", cleanModCacheFlag))
}
_, err := exec.Command("go", args...).CombinedOutput()
if len(args) > 1 && err == nil {
fmt.Println("Clean cache successfully !")
Expand Down
49 changes: 43 additions & 6 deletions cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
Expand All @@ -14,23 +16,36 @@ import (

type ActionTestSuite struct {
suite.Suite
binary string
}

func TestActionSuite(t *testing.T) {
suite.Run(t, &ActionTestSuite{
binary: filepath.Join(internal.CurProject().Target(), lo.If(internal.Windows(), "gob.exe").Else("gob")),
suite.Run(t, &ActionTestSuite{})
}

func TearDownSuite(prefix string) {
filepath.WalkDir(os.TempDir(), func(path string, d fs.DirEntry, err error) error {
if d.IsDir() && strings.HasPrefix(d.Name(), prefix) {
os.RemoveAll(path)
}
return nil
})
filepath.WalkDir(filepath.Join(internal.CurProject().Root(), "target"), func(path string, d fs.DirEntry, err error) error {
if d.IsDir() && strings.HasPrefix(d.Name(), prefix) {
os.RemoveAll(path)
}
return nil
})
}

func (suite *ActionTestSuite) SetupSuite() {
os.Remove(suite.binary)
func (suite *ActionTestSuite) TearDownSuite() {
TearDownSuite("cmd_action_test_")
}

func (suite *ActionTestSuite) TestActionBuild() {
err := buildAction(nil)
assert.NoError(suite.T(), err)
_, err = os.Stat(suite.binary)
binary := filepath.Join(internal.CurProject().Target(), lo.If(internal.Windows(), "gob.exe").Else("gob"))
_, err = os.Stat(binary)
assert.NoError(suite.T(), err)
}

Expand Down Expand Up @@ -63,9 +78,18 @@ func (suite *ActionTestSuite) TestExecute() {
}

func (suite *ActionTestSuite) TestCoverage() {
s, _ := os.Open(filepath.Join(internal.CurProject().Root(), "testdata", "cover.out"))
t, _ := os.Create(filepath.Join(internal.CurProject().Target(), "cover.out"))
io.Copy(t, s)
s.Close()
t.Close()
_, err1 := os.Stat(filepath.Join(internal.CurProject().Target(), "cover.out"))
assert.NoError(suite.T(), err1)
err2 := coverReport(nil, "")
assert.True(suite.T(), (err1 == nil) == (err2 == nil))
_, err2 = os.Stat(filepath.Join(internal.CurProject().Target(), "cover.html"))
assert.NoError(suite.T(), err2)

}

func (suite *ActionTestSuite) TestSetupActions() {
Expand All @@ -84,3 +108,16 @@ func (suite *ActionTestSuite) TestSetupVersion() {
_, err = os.Stat(version)
assert.NoError(suite.T(), err)
}

func (suite *ActionTestSuite) TestBuildAndClean() {
target := internal.CurProject().Target()
err := buildAction(nil, "")
assert.NoError(suite.T(), err)
entry, err := os.ReadDir(target)
assert.Truef(suite.T(), len(entry) > 0, "target should not empty")
err = cleanAction(nil, "")
assert.NoError(suite.T(), err)
entry, err = os.ReadDir(target)
assert.Truef(suite.T(), len(entry) == 0, "target should empty")

}
3 changes: 0 additions & 3 deletions cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,4 @@ func init() {
}).Else(nil)
})
builderCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
builderCmd.Flags().BoolVar(&CleanCache, cleanCacheFlag, false, "to remove the entire go build cache")
builderCmd.Flags().BoolVar(&CleanTestCache, cleanTestCacheFlag, false, "to expire all test results in the go build cache")
builderCmd.Flags().BoolVar(&CleanModCache, cleanModCacheFlag, false, "to remove the entire module download cache")
}
59 changes: 28 additions & 31 deletions cmd/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,14 @@ import (

type BuilderTestSuit struct {
suite.Suite
gopath string
testDir string
}

func TestBuilderTestSuit(t *testing.T) {
_, file := internal.TestCallee()
suite.Run(t, &BuilderTestSuit{
gopath: internal.GoPath(),
testDir: filepath.Join(internal.CurProject().Target(), file),
})
}

func (suite *BuilderTestSuit) SetupSuite() {
os.RemoveAll(suite.gopath)
os.RemoveAll(suite.testDir)
suite.Run(t, &BuilderTestSuit{})
}

func (suite *BuilderTestSuit) TearDownTest() {
os.RemoveAll(suite.gopath)
os.RemoveAll(suite.testDir)
func (suite *BuilderTestSuit) TearDownSuite() {
TearDownSuite("cmd_builder_test_")
}

func (suite *BuilderTestSuit) TestValidArgs() {
Expand Down Expand Up @@ -73,14 +61,18 @@ func (suite *BuilderTestSuit) TestArgs() {
},
}
for _, test := range tests {
suite.T().Run(test.name, func(t *testing.T) {
err := builderCmd.Args(nil, test.args)
assert.True(t, test.wantErr == (err != nil))
})
err := builderCmd.Args(nil, test.args)
assert.True(suite.T(), test.wantErr == (err != nil))
}

}

func (suite *BuilderTestSuit) TestExecute() {
builderCmd.SetArgs([]string{"cd"})
err := Execute()
assert.Error(suite.T(), err)
}

func (suite *BuilderTestSuit) TestBuild() {
tests := []struct {
name string
Expand All @@ -99,19 +91,13 @@ func (suite *BuilderTestSuit) TestBuild() {
},
}
for _, test := range tests {
suite.T().Run(test.name, func(t *testing.T) {
builderCmd.SetArgs(test.args)
if !test.wantErr {
os.Chdir(internal.CurProject().Root())
}
err := Execute()
assert.True(t, test.wantErr == (err != nil))
if test.wantErr {
assert.True(suite.T(), strings.Contains(err.Error(), color.RedString("")))
}
})
builderCmd.SetArgs(test.args)
err := execute(builderCmd, test.args[0])
assert.True(suite.T(), test.wantErr == (err != nil))
if test.wantErr {
assert.True(suite.T(), strings.Contains(err.Error(), color.RedString("")))
}
}

}

func (suite *BuilderTestSuit) TestPersistentPreRun() {
Expand Down Expand Up @@ -150,3 +136,14 @@ func (suite *BuilderTestSuit) TestBuiltinPlugins() {
assert.Equal(suite.T(), "gotest.tools/gotestsum", plugin.Module())
assert.Equal(suite.T(), "test", plugin.Alias)
}

func (suite *BuilderTestSuit) TestRunE() {
target := internal.CurProject().Target()
err := builderCmd.RunE(builderCmd, []string{"build"})
assert.NoError(suite.T(), err)
_, err = os.Stat(filepath.Join(target, lo.If(internal.Windows(), "gob.exe").Else("gob")))
assert.NoError(suite.T(), err, "binary should be generated")
err = builderCmd.RunE(builderCmd, []string{"build", "clean"})
assert.NoError(suite.T(), err)
assert.NoFileExistsf(suite.T(), filepath.Join(target, lo.If(internal.Windows(), "gob.exe").Else("gob")), "binary should be deleted")
}
2 changes: 1 addition & 1 deletion cmd/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func TestDependency(t *testing.T) {
})
assert.True(t, contains)
})

depCmd.RunE(nil, []string{""})
}
22 changes: 9 additions & 13 deletions cmd/exec_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"github.com/kcmvp/gob/internal"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand All @@ -13,27 +14,24 @@ import (

type ExecTestSuite struct {
suite.Suite
testDir string
}

func (suite *ExecTestSuite) SetupSuite() {
func (suite *ExecTestSuite) BeforeTest(_, testName string) {
s, _ := os.Open(filepath.Join(internal.CurProject().Root(), "testdata", "gob.yaml"))
os.MkdirAll(suite.testDir, os.ModePerm)
t, _ := os.Create(filepath.Join(suite.testDir, "gob.yaml"))
root := filepath.Join(internal.CurProject().Root(), "target", fmt.Sprintf("cmd_exec_test_%s", testName))
os.MkdirAll(root, os.ModePerm)
t, _ := os.Create(filepath.Join(root, "gob.yaml"))
io.Copy(t, s)
s.Close()
t.Close()
}

func (suite *ExecTestSuite) TearDownSuite() {
os.RemoveAll(suite.testDir)
TearDownSuite("cmd_exec_test_")
}

func TestExecSuite(t *testing.T) {
_, file := internal.TestCallee()
suite.Run(t, &ExecTestSuite{
testDir: filepath.Join(internal.CurProject().Target(), file),
})
suite.Run(t, &ExecTestSuite{})
}

func (suite *ExecTestSuite) TestActions() {
Expand All @@ -58,10 +56,8 @@ func (suite *ExecTestSuite) TestCmdArgs() {
},
}
for _, test := range tests {
suite.T().Run(test.name, func(t *testing.T) {
err := execCmd.Args(execCmd, test.args)
assert.True(t, test.wantErr == (err != nil))
})
err := execCmd.Args(execCmd, test.args)
assert.True(suite.T(), test.wantErr == (err != nil))
}
}

Expand Down
1 change: 0 additions & 1 deletion cmd/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func builtinPlugins() []internal.Plugin {

func initializerFunc(_ *cobra.Command, _ []string) {
fmt.Println("Initialize configuration ......")
// initBuildVersion()
lo.ForEach(builtinPlugins(), func(plugin internal.Plugin, index int) {
internal.CurProject().SetupPlugin(plugin)
if len(plugin.Config) > 0 {
Expand Down
17 changes: 6 additions & 11 deletions cmd/initializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@ const testsum = "gotest.tools/gotestsum"

type InitializationTestSuite struct {
suite.Suite
testDir string
goPath string
}

func TestInitializationTestSuit(t *testing.T) {
_, dir := internal.TestCallee()
suite.Run(t, &InitializationTestSuite{
testDir: filepath.Join(internal.CurProject().Target(), dir),
goPath: internal.GoPath(),
})
suite.Run(t, &InitializationTestSuite{})
}

func (suite *InitializationTestSuite) TearDownSuite() {
os.RemoveAll(suite.goPath)
os.RemoveAll(suite.testDir)
TearDownSuite("cmd_initializer_test_")
}

func (suite *InitializationTestSuite) TestBuiltInPlugins() {
Expand All @@ -45,6 +38,8 @@ func (suite *InitializationTestSuite) TestBuiltInPlugins() {
}

func (suite *InitializationTestSuite) TestInitializerFunc() {
gopath := internal.GoPath()
target := internal.CurProject().Target()
initializerFunc(nil, nil)
plugins := internal.CurProject().Plugins()
assert.Equal(suite.T(), 2, len(plugins))
Expand All @@ -58,10 +53,10 @@ func (suite *InitializationTestSuite) TestInitializerFunc() {
})
assert.True(suite.T(), ok)
lo.ForEach(plugins, func(plugin internal.Plugin, _ int) {
_, err := os.Stat(filepath.Join(suite.goPath, plugin.Binary()))
_, err := os.Stat(filepath.Join(gopath, plugin.Binary()))
assert.NoError(suite.T(), err)
})
_, err1 := os.Stat(filepath.Join(suite.testDir, "gob.yaml"))
_, err1 := os.Stat(filepath.Join(target, "gob.yaml"))
assert.NoError(suite.T(), err1)
assert.Equal(suite.T(), []string{"build", "clean", "test", "lint"}, validBuilderArgs())
}
Loading
Loading