github.com/lunny/gop@v0.0.0-20190322013459-2be48bbe64f7/cmd/integration_test.go (about)

     1  // Copyright 2017 The Gop Authors. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cmd
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/Unknwon/com"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/urfave/cli"
    18  )
    19  
    20  func runCommand(cmd cli.Command, dir string, args ...string) error {
    21  	if dir != "" {
    22  		os.Chdir(dir)
    23  	}
    24  
    25  	app := cli.NewApp()
    26  	app.Name = "Gop"
    27  	app.Usage = "Build golang applications out of GOPATH"
    28  	app.Commands = []cli.Command{
    29  		cmd,
    30  	}
    31  
    32  	var cmdArgs = []string{"gop", cmd.Name}
    33  
    34  	return app.Run(append(cmdArgs, args...))
    35  }
    36  
    37  func TestMain(m *testing.M) {
    38  	cli.OsExiter = func(code int) {
    39  		fmt.Printf("exit with %d\n", code)
    40  	}
    41  
    42  	m.Run()
    43  }
    44  
    45  func TestInit(t *testing.T) {
    46  	tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano()))
    47  	os.MkdirAll(tmpDir, os.ModePerm)
    48  
    49  	err := runCommand(CmdInit, tmpDir)
    50  	assert.NoError(t, err)
    51  	assert.True(t, com.IsExist(filepath.Join(tmpDir, "src")))
    52  	assert.True(t, com.IsExist(filepath.Join(tmpDir, "src", "main")))
    53  	assert.True(t, com.IsExist(filepath.Join(tmpDir, "src", "vendor")))
    54  	assert.True(t, com.IsExist(filepath.Join(tmpDir, "src", "main", "main.go")))
    55  	assert.True(t, com.IsExist(filepath.Join(tmpDir, "gop.yml")))
    56  
    57  	err = runCommand(CmdBuild, filepath.Join(tmpDir, "src"))
    58  	assert.NoError(t, err)
    59  
    60  	err = runCommand(CmdEnsure, filepath.Join(tmpDir, "src"))
    61  	assert.NoError(t, err)
    62  
    63  	err = runCommand(CmdStatus, filepath.Join(tmpDir, "src"))
    64  	assert.NoError(t, err)
    65  }
    66  
    67  func assertExist(t *testing.T, rootPath, relPath string) {
    68  	paths := strings.Split(relPath, "/")
    69  	assert.True(t, com.IsExist(filepath.Join(append([]string{rootPath}, paths...)...)))
    70  }
    71  
    72  func assertNotExist(t *testing.T, rootPath, relPath string) {
    73  	paths := strings.Split(relPath, "/")
    74  	assert.False(t, com.IsExist(filepath.Join(append([]string{rootPath}, paths...)...)))
    75  }
    76  
    77  func TestAddAndRm(t *testing.T) {
    78  	tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano()))
    79  	os.MkdirAll(tmpDir, os.ModePerm)
    80  
    81  	err := runCommand(CmdInit, tmpDir)
    82  	assert.NoError(t, err)
    83  	assertExist(t, tmpDir, "src")
    84  	assertExist(t, tmpDir, "src/main")
    85  	assertExist(t, tmpDir, "src/vendor")
    86  	assertExist(t, tmpDir, "src/main/main.go")
    87  	assertExist(t, tmpDir, "gop.yml")
    88  
    89  	cmdGet := NewCommand("get", "github.com/lunny/tango")
    90  	_, err = cmdGet.Run()
    91  	assert.NoError(t, err)
    92  
    93  	err = runCommand(CmdAdd, filepath.Join(tmpDir, "src"), "./cmd")
    94  	assert.Error(t, err)
    95  	assertNotExist(t, tmpDir, "src/vendor/cmd")
    96  
    97  	err = runCommand(CmdAdd, filepath.Join(tmpDir, "src"), "github.com/noexist/cmd")
    98  	assert.Error(t, err)
    99  	assertNotExist(t, tmpDir, "src/vendor/github.com/noexist/cmd")
   100  
   101  	err = runCommand(CmdAdd, filepath.Join(tmpDir, "src"), "github.com/lunny/tango")
   102  	assert.NoError(t, err)
   103  	assertExist(t, tmpDir, "src/vendor/github.com/lunny/tango")
   104  	assertExist(t, tmpDir, "src/vendor/github.com/lunny/log")
   105  
   106  	err = runCommand(CmdAdd, filepath.Join(tmpDir, "src"), "github.com/lunny/tango")
   107  	assert.NoError(t, err)
   108  	assertExist(t, tmpDir, "src/vendor/github.com/lunny/tango")
   109  	assertExist(t, tmpDir, "src/vendor/github.com/lunny/log")
   110  
   111  	err = runCommand(CmdRemove, filepath.Join(tmpDir, "src"), "github.com/lunny/tango")
   112  	assert.NoError(t, err)
   113  
   114  	assertExist(t, tmpDir, "src/vendor/github.com/lunny")
   115  	assertNotExist(t, tmpDir, "src/vendor/github.com/lunny/tango")
   116  	assertExist(t, tmpDir, "src/vendor/github.com/lunny/log")
   117  
   118  	err = runCommand(CmdRemove, filepath.Join(tmpDir, "src"), "github.com/lunny/log")
   119  	assert.NoError(t, err)
   120  
   121  	assertExist(t, tmpDir, "src/vendor/github.com/lunny")
   122  	assertNotExist(t, tmpDir, "src/vendor/github.com/lunny/tango")
   123  	assertNotExist(t, tmpDir, "src/vendor/github.com/lunny/log")
   124  }