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

add build test #17

Merged
merged 7 commits into from
Dec 25, 2023
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
6 changes: 1 addition & 5 deletions cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"os"
)

func preRun() {
internal.CurProject().Setup(false)
}

// builderCmd represents the base command when called without any subcommands
var builderCmd = &cobra.Command{
Use: "gob",
Expand All @@ -28,7 +24,7 @@ var builderCmd = &cobra.Command{
}),
Args: cobra.MatchAll(cobra.OnlyValidArgs, cobra.MinimumNArgs(1)),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
preRun()
internal.CurProject().Setup(false)
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := build(cmd, args); err != nil {
Expand Down
7 changes: 5 additions & 2 deletions cmd/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
package cmd

import (
"github.com/kcmvp/gb/internal"
"fmt"
"github.com/kcmvp/gob/internal"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand All @@ -27,7 +28,7 @@ func TestBuilderTestSuit(t *testing.T) {
}

func (suite *BuilderTestSuit) TestPersistentPreRun() {
preRun()
builderCmd.PersistentPreRun(nil, nil)
hooks := lo.MapToSlice(internal.HookScripts, func(key string, _ string) string {
return key
})
Expand All @@ -37,6 +38,8 @@ func (suite *BuilderTestSuit) TestPersistentPreRun() {
assert.True(suite.T(), info.ModTime().UnixNano() > suite.start)
}
}
//fmt.Println(internal.CurProject().Configuration())
fmt.Println(internal.CurProject().Plugins())
// test the missing plugins installation
lo.ForEach(internal.CurProject().Plugins(), func(plugin lo.Tuple4[string, string, string, string], index int) {
_, name := internal.NormalizePlugin(plugin.D)
Expand Down
27 changes: 13 additions & 14 deletions cmd/initializer_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//go:build ignore

package cmd

import (
"bufio"
"github.com/kcmvp/gb/internal"
"github.com/kcmvp/gob/internal"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -48,17 +47,17 @@ func (suite *InitializationTestSuite) TestInitializeHook() {
assert.True(suite.T(), hasLint)
assert.True(suite.T(), hasAlias)
// verify plugin
//var installed bool
//filepath.WalkDir(filepath.Join(suite.gopath, "bin"), func(path string, d fs.DirEntry, err error) error {
// if err != nil {
// return err
// }
// if installed = strings.HasPrefix(d.Name(), "golangci-lint-"); installed {
// return filepath.SkipDir
// }
// return nil
//})
//assert.True(suite.T(), installed)
var installed bool
filepath.WalkDir(filepath.Join(suite.gopath, "bin"), func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if installed = strings.HasPrefix(d.Name(), "golangci-lint-"); installed {
return filepath.SkipDir
}
return nil
})
assert.True(suite.T(), installed)
// verify hook
hooks := lo.MapToSlice(internal.HookScripts, func(key string, _ string) string {
return key
Expand Down
1 change: 1 addition & 0 deletions cmd/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ func TestInstallPlugin(t *testing.T) {
assert.Equal(t, "lint-run", plugin.C)
assert.Equal(t, fiximports, plugin.D)
assert.Equal(t, 2, len(internal.CurProject().Plugins()))

}
12 changes: 5 additions & 7 deletions internal/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ func (project *Project) Setup(init bool) {
}
}
// install missing plugins
if ok, _ := TestCallee(); !ok {
lop.ForEach(CurProject().Plugins(), func(plugin lo.Tuple4[string, string, string, string], index int) {
if !CurProject().PluginInstalled(plugin.D) {
CurProject().InstallPlugin(plugin.D, plugin.B, plugin.C)
}
})
}
lop.ForEach(CurProject().Plugins(), func(plugin lo.Tuple4[string, string, string, string], index int) {
if !CurProject().PluginInstalled(plugin.D) {
CurProject().InstallPlugin(plugin.D, plugin.B, plugin.C)
}
})
}
12 changes: 6 additions & 6 deletions internal/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestCallee() (bool, string) {
frames := runtime.CallersFrames(callers[:n])
for {
frame, more := frames.Next()
fmt.Printf("file name %s:%d\n", frame.File, frame.Line)
//fmt.Printf("file name %s:%d\n", frame.File, frame.Line)
test = strings.HasSuffix(frame.File, "_test.go") && strings.HasPrefix(frame.Function, module)
if test || !more {
method, _ = lo.Last(strings.Split(frame.Function, "."))
Expand Down Expand Up @@ -245,18 +245,18 @@ func (project *Project) InstallPlugin(url string, aliasAndCommand ...string) err
var err error
if !installed {
// install only
//tempGoPath, _ := os.MkdirTemp("", base)
//os.Setenv("GOPATH", tempGoPath)
tempGoPath, _ := os.MkdirTemp("", base)
os.Setenv("GOPATH", tempGoPath)
fmt.Printf("Installing %s ...... \n", url)
_, err = exec.Command("go", "install", url).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to install %s: %v", url, err)
}
defer func() {
//os.Setenv("GOPATH", gopath)
//os.RemoveAll(tempGoPath)
os.Setenv("GOPATH", gopath)
os.RemoveAll(tempGoPath)
}()
if err = filepath.WalkDir(gopath, func(path string, d fs.DirEntry, err error) error {
if err = filepath.WalkDir(tempGoPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions testdata/gob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exec:
commit-msg-hook: ^#[0-9]+:\s*.{10,}$
pre-commit-hook:
- list
- test
pre-push-hook:
- list
- test
plugins:
html2article:
alias: lint
command: run, ./...
url: golang.org/x/tools/cmd/html2article@v0.16.1