github.com/zaquestion/lab@v0.25.1/cmd/ci_run_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os/exec"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func Test_ciRun(t *testing.T) {
    12  	t.Parallel()
    13  	repo := copyTestRepo(t)
    14  	cmd := exec.Command(labBinaryPath, "ci", "run")
    15  	cmd.Dir = repo
    16  
    17  	b, err := cmd.CombinedOutput()
    18  	if err != nil {
    19  		t.Log(string(b))
    20  		t.Fatal(err)
    21  	}
    22  	require.Regexp(t, `^https://gitlab.com/lab-testing/test/-/pipelines/\d+`, string(b))
    23  }
    24  
    25  func Test_parseCIVariables(t *testing.T) {
    26  	t.Parallel()
    27  	tests := []struct {
    28  		desc        string
    29  		vars        []string
    30  		expected    map[string]string
    31  		expectedErr string
    32  	}{
    33  		{
    34  			"happy",
    35  			[]string{"foo=bar", "fizz=buzz"},
    36  			map[string]string{
    37  				"foo":  "bar",
    38  				"fizz": "buzz",
    39  			},
    40  			"",
    41  		},
    42  		{
    43  			"multi equals",
    44  			[]string{"foo=bar", "fizz=buzz=baz"},
    45  			map[string]string{
    46  				"foo":  "bar",
    47  				"fizz": "buzz=baz",
    48  			},
    49  			"",
    50  		},
    51  		{
    52  			"bad vars",
    53  			[]string{"foo=bar", "fizzbuzz"},
    54  			nil,
    55  			"Invalid Variable: \"fizzbuzz\", Variables must be in the format key=value",
    56  		},
    57  	}
    58  
    59  	for _, test := range tests {
    60  		test := test
    61  		t.Run(test.desc, func(t *testing.T) {
    62  			t.Parallel()
    63  			ciVars, err := parseCIVariables(test.vars)
    64  			assert.Equal(t, test.expected, ciVars)
    65  			if test.expectedErr == "" {
    66  				assert.NoError(t, err)
    67  			} else {
    68  				assert.EqualError(t, err, test.expectedErr)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func Test_getCIRunOptions(t *testing.T) {
    75  	t.Parallel()
    76  	tests := []struct {
    77  		desc            string
    78  		cmdFunc         func()
    79  		args            []string
    80  		expectedProject string
    81  		expectedBranch  string
    82  		expectedErr     string
    83  	}{
    84  		{
    85  			"noargs",
    86  			nil,
    87  			[]string{},
    88  			"zaquestion/test",
    89  			"master",
    90  			"",
    91  		},
    92  		{
    93  			"branch arg",
    94  			nil,
    95  			[]string{"mybranch"},
    96  			"zaquestion/test",
    97  			"mybranch",
    98  			"",
    99  		},
   100  		{
   101  			"fork branch arg",
   102  			nil,
   103  			[]string{"mrtest"},
   104  			"lab-testing/test",
   105  			"mrtest",
   106  			"",
   107  		},
   108  		{
   109  			"project flag",
   110  			func() {
   111  				ciTriggerCmd.Flags().Set("project", "zaquestion/test")
   112  			},
   113  			[]string{},
   114  			"zaquestion/test", // https://gitlab.com/zaquestion/test project name
   115  			"master",
   116  			"",
   117  		},
   118  		{
   119  			"bad project",
   120  			func() {
   121  				ciTriggerCmd.Flags().Set("project", "barfasdfasdf")
   122  			},
   123  			[]string{},
   124  			"", // https://gitlab.com/zaquestion/test project name
   125  			"",
   126  			"GitLab project not found, verify you have access to the requested resource",
   127  		},
   128  	}
   129  
   130  	for _, test := range tests {
   131  		test := test
   132  		t.Run(test.desc, func(t *testing.T) {
   133  			ciTriggerCmd.Flags().Set("project", "")
   134  			if test.cmdFunc != nil {
   135  				test.cmdFunc()
   136  			}
   137  
   138  			p, b, err := getCIRunOptions(ciTriggerCmd, test.args)
   139  			assert.Equal(t, test.expectedProject, p)
   140  			assert.Equal(t, test.expectedBranch, b)
   141  			if test.expectedErr == "" {
   142  				assert.NoError(t, err)
   143  			} else {
   144  				assert.EqualError(t, err, test.expectedErr)
   145  			}
   146  		})
   147  	}
   148  }