github.com/kcmvp/gob@v1.0.17/cmd/gbc/artifact/hook_test.go (about)

     1  package artifact
     2  
     3  import (
     4  	"github.com/kcmvp/gob/utils"
     5  	"github.com/samber/lo"
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/suite"
     8  	"io/fs"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  type GitHookTestSuite struct {
    16  	suite.Suite
    17  }
    18  
    19  func TearDownSuite(prefix string) {
    20  	filepath.WalkDir(os.TempDir(), func(path string, d fs.DirEntry, err error) error {
    21  		if d.IsDir() && strings.HasPrefix(d.Name(), prefix) {
    22  			os.RemoveAll(path)
    23  		}
    24  		return nil
    25  	})
    26  	filepath.WalkDir(filepath.Join(CurProject().Root(), "target"), func(path string, d fs.DirEntry, err error) error {
    27  		if d.IsDir() && strings.HasPrefix(d.Name(), prefix) {
    28  			os.RemoveAll(path)
    29  		}
    30  		return nil
    31  	})
    32  }
    33  
    34  func (suite *GitHookTestSuite) TearDownSuite() {
    35  	_, method := utils.TestCaller()
    36  	TearDownSuite(strings.Join(lo.DropRight(strings.Split(method, "_"), 1), "_"))
    37  }
    38  
    39  func TestGitHookSuite(t *testing.T) {
    40  	suite.Run(t, &GitHookTestSuite{})
    41  }
    42  
    43  func (suite *GitHookTestSuite) TestSetupHook() {
    44  	CurProject().SetupHooks(true)
    45  	info1, err := os.Stat(filepath.Join(CurProject().Target(), "gob.yaml"))
    46  	assert.NoError(suite.T(), err)
    47  	executions := CurProject().Executions()
    48  	assert.Equal(suite.T(), 3, len(executions))
    49  	rs := lo.Every([]string{"commit-msg", "pre-commit", "pre-push"}, lo.Map(executions, func(item Execution, _ int) string {
    50  		return item.CmdKey
    51  	}))
    52  	assert.True(suite.T(), rs)
    53  	hook := CurProject().GitHook()
    54  	assert.NotEmpty(suite.T(), hook.CommitMsg)
    55  	assert.Equal(suite.T(), []string([]string{"lint", "test"}), hook.PreCommit)
    56  	assert.Equal(suite.T(), []string([]string{"test"}), hook.PrePush)
    57  	CurProject().SetupHooks(false)
    58  	info2, err := os.Stat(filepath.Join(CurProject().Target(), "gob.yaml"))
    59  	assert.NoError(suite.T(), err)
    60  	assert.Equal(suite.T(), info1.ModTime(), info2.ModTime())
    61  }