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

     1  package artifact
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/kcmvp/gob/utils"
     7  	"github.com/samber/lo"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/suite"
    10  	"github.com/tidwall/gjson"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  type InternalPluginTestSuit struct {
    18  	suite.Suite
    19  	lintLatestVersion string
    20  }
    21  
    22  func (suite *InternalPluginTestSuit) TearDownSuite() {
    23  	_, method := utils.TestCaller()
    24  	TearDownSuite(strings.Trim(method, "TearDownSuite"))
    25  }
    26  
    27  func TestInternalPluginSuite(t *testing.T) {
    28  	suite.Run(t, &InternalPluginTestSuit{
    29  		lintLatestVersion: LatestVersion("github.com/golangci/golangci-lint")[0].B,
    30  	})
    31  }
    32  
    33  func (suite *InternalPluginTestSuit) TestNewPlugin() {
    34  	gopath := GoPath()
    35  	defer func() {
    36  		os.RemoveAll(gopath)
    37  	}()
    38  	tests := []struct {
    39  		name    string
    40  		url     string
    41  		module  string
    42  		logName string
    43  		binary  string
    44  		wantErr bool
    45  	}{
    46  		{
    47  			name:    "without version",
    48  			url:     "github.com/golangci/golangci-lint/cmd/golangci-lint",
    49  			module:  "github.com/golangci/golangci-lint",
    50  			logName: "golangci-lint",
    51  			binary:  fmt.Sprintf("%s-%s", "golangci-lint", suite.lintLatestVersion),
    52  			wantErr: false,
    53  		},
    54  		{
    55  			name:    "latest version",
    56  			url:     "github.com/golangci/golangci-lint/cmd/golangci-lint@latest",
    57  			module:  "github.com/golangci/golangci-lint",
    58  			logName: "golangci-lint",
    59  			binary:  fmt.Sprintf("%s-%s", "golangci-lint", suite.lintLatestVersion),
    60  			wantErr: false,
    61  		},
    62  		{
    63  			name:    "specific version",
    64  			url:     "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.1.1",
    65  			module:  "github.com/golangci/golangci-lint",
    66  			logName: "golangci-lint",
    67  			binary:  "golangci-lint-v1.1.1",
    68  			wantErr: false,
    69  		},
    70  		{
    71  			name:    "has @ but no version",
    72  			url:     "github.com/golangci/golangci-lint/cmd/golangci-lint@",
    73  			module:  "github.com/golangci/golangci-lint",
    74  			logName: "",
    75  			binary:  "-",
    76  			wantErr: true,
    77  		},
    78  		{
    79  			name:    "at the beginning of the url",
    80  			url:     "@github.com/golangci/golangci-lint/cmd/golangci-lint",
    81  			module:  "github.com/golangci/golangci-lint",
    82  			logName: "",
    83  			binary:  "-",
    84  			wantErr: true,
    85  		},
    86  		{
    87  			name:    "multiple @",
    88  			url:     "github.com/golangci/golangci-lint/cmd/golangci@-lint@v1",
    89  			module:  "github.com/golangci/golangci-lint",
    90  			logName: "",
    91  			binary:  "-",
    92  			wantErr: true,
    93  		},
    94  		{
    95  			name:    "gotestsum",
    96  			url:     "gotest.tools/gotestsum",
    97  			module:  "gotest.tools/gotestsum",
    98  			logName: "gotestsum",
    99  			binary:  "gotestsum-v1.11.0",
   100  			wantErr: false,
   101  		},
   102  	}
   103  	for _, test := range tests {
   104  		suite.T().Run(test.name, func(t *testing.T) {
   105  			plugin, err := NewPlugin(test.url)
   106  			assert.Equal(t, plugin.taskName(), test.logName)
   107  			assert.Equal(t, plugin.Binary(), test.binary)
   108  			assert.True(t, test.wantErr == (err != nil))
   109  			if !test.wantErr {
   110  				assert.Equal(t, test.module, plugin.module)
   111  				assert.True(t, lo.Contains([]string{"v1.58.1", "v1.57.2", "v1.1.1", "v1.11.0"}, plugin.Version()))
   112  			}
   113  		})
   114  	}
   115  }
   116  
   117  func (suite *InternalPluginTestSuit) TestUnmarshalJSON() {
   118  	gopath := GoPath()
   119  	defer func() {
   120  		os.RemoveAll(gopath)
   121  	}()
   122  	data, _ := os.ReadFile(filepath.Join(CurProject().Root(), "cmd", "gbc", "command", "resources", "config.json"))
   123  	v := gjson.GetBytes(data, "gbc_init.plugins")
   124  	var plugins []Plugin
   125  	err := json.Unmarshal([]byte(v.Raw), &plugins)
   126  	t := suite.T()
   127  	assert.NoError(t, err)
   128  	assert.Equal(t, 2, len(plugins))
   129  	plugin, ok := lo.Find(plugins, func(plugin Plugin) bool {
   130  		return plugin.Url == "github.com/golangci/golangci-lint/cmd/golangci-lint"
   131  	})
   132  	assert.True(t, ok)
   133  	assert.Equal(t, suite.lintLatestVersion, plugin.Version())
   134  	assert.Equal(t, "golangci-lint", plugin.Name())
   135  	assert.Equal(t, "github.com/golangci/golangci-lint", plugin.Module())
   136  	assert.Equal(t, "lint", plugin.Alias)
   137  	// no command
   138  	plugin, ok = lo.Find(plugins, func(plugin Plugin) bool {
   139  		return plugin.Url == "gotest.tools/gotestsum"
   140  	})
   141  	assert.True(t, ok)
   142  	assert.Equal(t, "v1.11.0", plugin.Version())
   143  	assert.Equal(t, "gotestsum", plugin.Name())
   144  	assert.Equal(t, "gotest.tools/gotestsum", plugin.Module())
   145  	assert.Equal(t, "test", plugin.Alias)
   146  }
   147  
   148  func (suite *InternalPluginTestSuit) TestInstallPlugin() {
   149  	gopath := GoPath()
   150  	defer func() {
   151  		os.RemoveAll(gopath)
   152  	}()
   153  	t := suite.T()
   154  	plugin, err := NewPlugin("gotest.tools/gotestsum")
   155  	assert.NoError(t, err)
   156  	path, err := plugin.install()
   157  	assert.NoError(t, err)
   158  	assert.NoFileExistsf(t, path, "temporay go path should be deleted")
   159  	binary := filepath.Join(GoPath(), plugin.Binary())
   160  	info1, err := os.Stat(binary)
   161  	assert.NoErrorf(t, err, "testsum should be installed successfully")
   162  	path, _ = plugin.install()
   163  	assert.NoFileExistsf(t, path, "temporay go path should be deleted")
   164  	info2, _ := os.Stat(binary)
   165  	assert.Equal(t, info1.ModTime(), info2.ModTime())
   166  }
   167  
   168  func (suite *InternalPluginTestSuit) TestExecute() {
   169  	gopath := GoPath()
   170  	defer func() {
   171  		os.RemoveAll(gopath)
   172  	}()
   173  	t := suite.T()
   174  	plugin, err := NewPlugin("golang.org/x/tools/cmd/guru@v0.17.0")
   175  	assert.NoError(t, err)
   176  	err = plugin.Execute()
   177  	fmt.Println(err.Error())
   178  	assert.Error(t, err)
   179  	//'exit status 2' means the plugin is executed but no parameters,
   180  	assert.Equal(t, "exit status 2", err.Error())
   181  	_, err = os.Stat(filepath.Join(CurProject().Target(), "guru.log"))
   182  	assert.NoError(suite.T(), err)
   183  }