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

     1  package artifact
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/kcmvp/gob/utils"
     6  	"github.com/samber/lo"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/suite"
     9  	"io"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  const depth = "github.com/KyleBanks/depth/cmd/depth"
    17  const testsum = "gotest.tools/gotestsum"
    18  const v6 = "golang.org/x/tools/cmd/digraph@v0.16.0"
    19  
    20  type ProjectTestSuite struct {
    21  	suite.Suite
    22  }
    23  
    24  func (suite *ProjectTestSuite) BeforeTest(_, testName string) {
    25  	s, _ := os.Open(filepath.Join(CurProject().Root(), "cmd", "gbc", "testdata", "gob.yaml"))
    26  	root := filepath.Join(CurProject().Root(), "target", fmt.Sprintf("artifact_ProjectTestSuite_%s", testName))
    27  	os.MkdirAll(root, os.ModePerm)
    28  	t, _ := os.Create(filepath.Join(root, "gob.yaml"))
    29  	io.Copy(t, s)
    30  	s.Close()
    31  	t.Close()
    32  }
    33  
    34  func (suite *ProjectTestSuite) TearDownSuite() {
    35  	_, method := utils.TestCaller()
    36  	prefix := strings.TrimRight(method, "TearDownSuite")
    37  	TearDownSuite(prefix)
    38  }
    39  
    40  func TestProjectSuite(t *testing.T) {
    41  	suite.Run(t, &ProjectTestSuite{})
    42  }
    43  
    44  func TestBasic(t *testing.T) {
    45  	plugins := CurProject().Plugins()
    46  	assert.Equal(t, 0, len(plugins))
    47  	assert.Equal(t, "github.com/kcmvp/gob", project.Module())
    48  }
    49  
    50  func (suite *ProjectTestSuite) TestDeps() {
    51  	deps := CurProject().Dependencies()
    52  	assert.Equal(suite.T(), 58, len(deps))
    53  	assert.True(suite.T(), lo.Contains(deps, "github.com/spf13/viper"))
    54  }
    55  
    56  func (suite *ProjectTestSuite) TestPlugins() {
    57  	_, err := os.Stat(filepath.Join(CurProject().Target(), "gob.yaml"))
    58  	assert.NoError(suite.T(), err)
    59  	plugins := CurProject().Plugins()
    60  	fmt.Println(plugins)
    61  	assert.Equal(suite.T(), 3, len(plugins))
    62  	plugin, ok := lo.Find(plugins, func(plugin Plugin) bool {
    63  		return plugin.Url == testsum
    64  	})
    65  	assert.True(suite.T(), ok)
    66  	assert.Equal(suite.T(), testsum, plugin.Module())
    67  	assert.Equal(suite.T(), "v1.11.0", plugin.Version())
    68  	assert.Equal(suite.T(), "gotestsum", plugin.Name())
    69  	assert.Equal(suite.T(), "test", plugin.Alias)
    70  	plugin, ok = lo.Find(plugins, func(plugin Plugin) bool {
    71  		return plugin.Url == depth
    72  	})
    73  	assert.True(suite.T(), ok)
    74  	assert.Equal(suite.T(), "github.com/KyleBanks/depth", plugin.Module())
    75  	assert.Equal(suite.T(), "v1.2.1", plugin.Version())
    76  	assert.Equal(suite.T(), "depth", plugin.Name())
    77  	assert.Equal(suite.T(), "depth", plugin.Alias)
    78  }
    79  
    80  func (suite *ProjectTestSuite) TestIsSetup() {
    81  	tests := []struct {
    82  		name    string
    83  		url     string
    84  		settled bool
    85  	}{
    86  		{
    87  			name:    "no version",
    88  			url:     depth,
    89  			settled: true,
    90  		},
    91  		{
    92  			name:    "with version",
    93  			url:     fmt.Sprintf("%s@latest", depth),
    94  			settled: true,
    95  		},
    96  		{
    97  			name:    "specified version",
    98  			url:     fmt.Sprintf("%s@v1.1.1", depth),
    99  			settled: true,
   100  		},
   101  		{
   102  			name:    "no settled",
   103  			url:     "entgo.io/ent/cmd/ent",
   104  			settled: false,
   105  		},
   106  	}
   107  	for _, test := range tests {
   108  		plugin, _ := NewPlugin(test.url)
   109  		v := CurProject().settled(plugin)
   110  		assert.Equal(suite.T(), test.settled, v)
   111  	}
   112  }
   113  
   114  func (suite *ProjectTestSuite) TestValidate() {
   115  	CurProject().Validate()
   116  	CurProject().GitHook()
   117  	for name, _ := range HookScripts() {
   118  		_, err := os.Stat(filepath.Join(CurProject().HookDir(), name))
   119  		assert.NoError(suite.T(), err)
   120  	}
   121  }
   122  
   123  func (suite *ProjectTestSuite) TestMainFiles() {
   124  	mainFiles := CurProject().MainFiles()
   125  	assert.Equal(suite.T(), 1, len(mainFiles))
   126  	assert.True(suite.T(), lo.Contains(mainFiles, filepath.Join(CurProject().Root(), "cmd", "gbc", "gbc.go")))
   127  }
   128  
   129  func (suite *ProjectTestSuite) TestVersion() {
   130  	assert.NotNil(suite.T(), Version())
   131  }
   132  
   133  func (suite *ProjectTestSuite) Test_Callee() {
   134  	test, method := utils.TestCaller()
   135  	assert.True(suite.T(), test)
   136  	assert.Equal(suite.T(), "artifact_ProjectTestSuite_Test_Callee", method)
   137  }
   138  
   139  func (suite *ProjectTestSuite) TestHookDir() {
   140  	hookDir := CurProject().HookDir()
   141  	_, dir := utils.TestCaller()
   142  	assert.True(suite.T(), strings.Contains(hookDir, dir))
   143  	_, err := os.Stat(hookDir)
   144  	assert.NoError(suite.T(), err)
   145  }
   146  
   147  func (suite *ProjectTestSuite) TestSetupPlugin() {
   148  	plugin, _ := NewPlugin(v6)
   149  	project.InstallPlugin(plugin)
   150  	gopath := GoPath()
   151  	entry, err := os.ReadDir(gopath)
   152  	_, suffix := utils.TestCaller()
   153  	assert.True(suite.T(), strings.HasSuffix(gopath, suffix))
   154  	assert.NoErrorf(suite.T(), err, "GOPATH should be created")
   155  	assert.True(suite.T(), len(entry) == 1, "plugin should be installed to GOPATH")
   156  }