github.com/ianfoo/lab@v0.9.5-0.20180123060006-5ed79f2ccfc7/cmd/root_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"log"
     5  	"math/rand"
     6  	"os"
     7  	"os/exec"
     8  	"path"
     9  	"path/filepath"
    10  	"strconv"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/spf13/viper"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  	"github.com/zaquestion/lab/internal/git"
    18  )
    19  
    20  func TestMain(m *testing.M) {
    21  	wd, err := git.WorkingDir()
    22  	if err != nil {
    23  		log.Fatal(err)
    24  	}
    25  	os.Chdir(wd)
    26  	err = exec.Command("go", "test", "-c", "-coverpkg", "./...", "-covermode", "count", "-o", "lab_bin").Run()
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  	rand.Seed(time.Now().UnixNano())
    31  
    32  	// Load config for non-testbinary based tests
    33  	os.Chdir(path.Join(wd, "testdata"))
    34  	viper.SetConfigName("lab")
    35  	viper.SetConfigType("hcl")
    36  	viper.AddConfigPath(".")
    37  	err = viper.ReadInConfig()
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  
    42  	code := m.Run()
    43  	os.Chdir(wd)
    44  	os.Remove("lab_bin")
    45  	testdirs, err := filepath.Glob("testdata-*")
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  	for _, dir := range testdirs {
    50  		err := os.RemoveAll(dir)
    51  		if err != nil {
    52  			log.Fatal(err)
    53  		}
    54  	}
    55  
    56  	os.Exit(code)
    57  }
    58  
    59  func copyTestRepo(t *testing.T) string {
    60  	dir := "../testdata-" + strconv.Itoa(int(rand.Uint64()))
    61  	t.Log(dir)
    62  	err := exec.Command("cp", "-r", "../testdata", dir).Run()
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	wd, err := os.Getwd()
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	dir = path.Join(wd, dir)
    71  	return dir
    72  }
    73  
    74  func TestRootCloneNoArg(t *testing.T) {
    75  	cmd := exec.Command("../lab_bin", "clone")
    76  	b, _ := cmd.CombinedOutput()
    77  	require.Contains(t, string(b), "You must specify a repository to clone.")
    78  }
    79  
    80  func TestRootGitCmd(t *testing.T) {
    81  	cmd := exec.Command("../lab_bin", "log", "-n", "1")
    82  	b, _ := cmd.CombinedOutput()
    83  	require.Contains(t, string(b), `commit cd64a7caea4f3ee5696a190379aff1a7f636e598
    84  Author: Zaq? Wiedmann <zaquestion@gmail.com>
    85  Date:   Sat Sep 2 20:58:39 2017 -0700
    86  
    87      Added additional commit for LastCommitMessage and meeting requirements for Log test (>1 commit)`)
    88  }
    89  
    90  func TestRootNoArg(t *testing.T) {
    91  	cmd := exec.Command("../lab_bin")
    92  	b, _ := cmd.CombinedOutput()
    93  	assert.Contains(t, string(b), "usage: git [--version] [--help] [-C <path>] [-c name=value]")
    94  	assert.Contains(t, string(b), `These GitLab commands are provided by lab:
    95  
    96    fork          Fork a remote repository on GitLab and add as remote`)
    97  }
    98  
    99  func Test_parseArgsRemote(t *testing.T) {
   100  	tests := []struct {
   101  		Name           string
   102  		Args           []string
   103  		ExpectedString string
   104  		ExpectedInt    int64
   105  		ExpectedErr    string
   106  	}{
   107  		{
   108  			Name:           "No Args",
   109  			Args:           nil,
   110  			ExpectedString: "",
   111  			ExpectedInt:    0,
   112  			ExpectedErr:    "",
   113  		},
   114  		{
   115  			Name:           "1 arg remote",
   116  			Args:           []string{"origin"},
   117  			ExpectedString: "origin",
   118  			ExpectedInt:    0,
   119  			ExpectedErr:    "",
   120  		},
   121  		{
   122  			Name:           "1 arg non remote",
   123  			Args:           []string{"foo"},
   124  			ExpectedString: "",
   125  			ExpectedInt:    0,
   126  			ExpectedErr:    "foo is not a valid remote or number",
   127  		},
   128  		{
   129  			Name:           "1 arg page",
   130  			Args:           []string{"100"},
   131  			ExpectedString: "",
   132  			ExpectedInt:    100,
   133  			ExpectedErr:    "",
   134  		},
   135  		{
   136  			Name:           "1 arg invalid page",
   137  			Args:           []string{"asdf100"},
   138  			ExpectedString: "",
   139  			ExpectedInt:    0,
   140  			ExpectedErr:    "asdf100 is not a valid remote or number",
   141  		},
   142  		{
   143  			Name:           "2 arg remote page",
   144  			Args:           []string{"origin", "100"},
   145  			ExpectedString: "origin",
   146  			ExpectedInt:    100,
   147  			ExpectedErr:    "",
   148  		},
   149  		{
   150  			Name:           "2 arg invalid remote valid page",
   151  			Args:           []string{"foo", "100"},
   152  			ExpectedString: "",
   153  			ExpectedInt:    0,
   154  			ExpectedErr:    "foo is not a valid remote",
   155  		},
   156  		{
   157  			Name:           "2 arg valid remote invalid page",
   158  			Args:           []string{"foo", "asdf100"},
   159  			ExpectedString: "",
   160  			ExpectedInt:    0,
   161  			ExpectedErr:    "strconv.ParseInt: parsing \"asdf100\": invalid syntax",
   162  		},
   163  	}
   164  	for _, test := range tests {
   165  		t.Run(test.Name, func(t *testing.T) {
   166  			test := test
   167  			t.Parallel()
   168  			s, i, err := parseArgsRemote(test.Args)
   169  			if err != nil {
   170  				assert.EqualError(t, err, test.ExpectedErr)
   171  			}
   172  			assert.Equal(t, test.ExpectedString, s)
   173  			assert.Equal(t, test.ExpectedInt, i)
   174  		})
   175  	}
   176  }
   177  
   178  func Test_parseArgs(t *testing.T) {
   179  	tests := []struct {
   180  		Name           string
   181  		Args           []string
   182  		ExpectedString string
   183  		ExpectedInt    int64
   184  		ExpectedErr    string
   185  	}{
   186  		{
   187  			Name:           "No Args",
   188  			Args:           nil,
   189  			ExpectedString: "zaquestion/test",
   190  			ExpectedInt:    0,
   191  			ExpectedErr:    "",
   192  		},
   193  		{
   194  			Name:           "1 arg remote",
   195  			Args:           []string{"lab-testing"},
   196  			ExpectedString: "lab-testing/test",
   197  			ExpectedInt:    0,
   198  			ExpectedErr:    "",
   199  		},
   200  		{
   201  			Name:           "1 arg non remote",
   202  			Args:           []string{"foo"},
   203  			ExpectedString: "",
   204  			ExpectedInt:    0,
   205  			ExpectedErr:    "foo is not a valid remote or number",
   206  		},
   207  		{
   208  			Name:           "1 arg page",
   209  			Args:           []string{"100"},
   210  			ExpectedString: "zaquestion/test",
   211  			ExpectedInt:    100,
   212  			ExpectedErr:    "",
   213  		},
   214  		{
   215  			Name:           "1 arg invalid page",
   216  			Args:           []string{"asdf100"},
   217  			ExpectedString: "",
   218  			ExpectedInt:    0,
   219  			ExpectedErr:    "asdf100 is not a valid remote or number",
   220  		},
   221  		{
   222  			Name:           "2 arg remote page",
   223  			Args:           []string{"origin", "100"},
   224  			ExpectedString: "zaquestion/test",
   225  			ExpectedInt:    100,
   226  			ExpectedErr:    "",
   227  		},
   228  		{
   229  			Name:           "2 arg invalid remote valid page",
   230  			Args:           []string{"foo", "100"},
   231  			ExpectedString: "",
   232  			ExpectedInt:    0,
   233  			ExpectedErr:    "foo is not a valid remote",
   234  		},
   235  		{
   236  			Name:           "2 arg valid remote invalid page",
   237  			Args:           []string{"foo", "asdf100"},
   238  			ExpectedString: "",
   239  			ExpectedInt:    0,
   240  			ExpectedErr:    "strconv.ParseInt: parsing \"asdf100\": invalid syntax",
   241  		},
   242  	}
   243  	for _, test := range tests {
   244  		t.Run(test.Name, func(t *testing.T) {
   245  			test := test
   246  			t.Parallel()
   247  			s, i, err := parseArgs(test.Args)
   248  			if err != nil {
   249  				assert.EqualError(t, err, test.ExpectedErr)
   250  			}
   251  			assert.Equal(t, test.ExpectedString, s)
   252  			assert.Equal(t, test.ExpectedInt, i)
   253  		})
   254  	}
   255  }