github.com/zaquestion/lab@v0.25.1/internal/gitlab/gitlab_test.go (about)

     1  package gitlab
     2  
     3  import (
     4  	"math/rand"
     5  	"os"
     6  	"path/filepath"
     7  	"strconv"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/otiai10/copy"
    12  	"github.com/spf13/viper"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  	gitlab "github.com/xanzy/go-gitlab"
    16  )
    17  
    18  func TestMain(m *testing.M) {
    19  	rand.Seed(time.Now().UnixNano())
    20  	repo := copyTestRepo()
    21  	err := os.Chdir(repo)
    22  	if err != nil {
    23  		log.Fatal(err)
    24  	}
    25  
    26  	viper.SetConfigName("lab")
    27  	viper.SetConfigType("toml")
    28  	viper.AddConfigPath(".")
    29  	err = viper.ReadInConfig()
    30  	if err != nil {
    31  		log.Fatal(err)
    32  	}
    33  	host := viper.GetString("core.host")
    34  	token := viper.GetString("core.token")
    35  
    36  	lab, _ := gitlab.NewClient(token, gitlab.WithBaseURL(host+"/api/v4"))
    37  	u, _, err := lab.Users.CurrentUser()
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  
    42  	Init(host, u.Username, token, false)
    43  
    44  	code := m.Run()
    45  
    46  	if err := os.Chdir("../"); err != nil {
    47  		log.Fatalf("Error chdir to ../: %s", err)
    48  	}
    49  	if err := os.RemoveAll(repo); err != nil {
    50  		log.Fatalf("Error removing %s: %s", repo, err)
    51  	}
    52  	os.Exit(code)
    53  }
    54  
    55  func TestGetProject(t *testing.T) {
    56  	project, err := GetProject("lab-testing/test")
    57  	require.NoError(t, err)
    58  	assert.Equal(t, 5694926, project.ID, "Expected 'lab-testing/test' to be project 5694926")
    59  }
    60  
    61  func TestUser(t *testing.T) {
    62  	// Should get set by Init() after TestMain()
    63  	require.Equal(t, "lab-testing", User())
    64  }
    65  
    66  func TestLoadGitLabTmplMR(t *testing.T) {
    67  	mrTmpl := LoadGitLabTmpl(TmplMR)
    68  	require.Equal(t, "I am the default merge request template for lab", mrTmpl)
    69  }
    70  
    71  func TestLoadGitLabTmplIssue(t *testing.T) {
    72  	issueTmpl := LoadGitLabTmpl(TmplIssue)
    73  	require.Equal(t, "This is the default issue template for lab", issueTmpl)
    74  }
    75  
    76  func TestLint(t *testing.T) {
    77  	tests := []struct {
    78  		desc     string
    79  		content  string
    80  		expected bool
    81  	}{
    82  		{
    83  			"Valid",
    84  			`build1:
    85    stage: build
    86    script:
    87      - echo "Do your build here"`,
    88  			true,
    89  		},
    90  		{
    91  			"Invalid",
    92  			`build1:
    93      - echo "Do your build here"`,
    94  			false,
    95  		},
    96  		{
    97  			"Empty",
    98  			``,
    99  			false,
   100  		},
   101  	}
   102  	for _, test := range tests {
   103  		t.Run(test.desc, func(t *testing.T) {
   104  			test := test
   105  			t.Parallel()
   106  			ok, _ := Lint(test.content)
   107  			require.Equal(t, test.expected, ok)
   108  		})
   109  	}
   110  }
   111  
   112  func TestGetCommit(t *testing.T) {
   113  	tests := []struct {
   114  		desc     string
   115  		ref      string
   116  		ok       bool
   117  		expectID string
   118  	}{
   119  		{
   120  			"not pushed",
   121  			"not_a_branch",
   122  			false,
   123  			"",
   124  		},
   125  		{
   126  			"pushed branch",
   127  			"mrtest", // branch name
   128  			true,
   129  			"54fd49a2ac60aeeef5ddc75efecd49f85f7ba9b0",
   130  		},
   131  		{
   132  			"pushed branch, neeeds encoding",
   133  			"needs/encode", // branch name
   134  			true,
   135  			"381f2b123dd404e8046ea42d5785061aa3b6674b",
   136  		},
   137  		{
   138  			"pushed sha",
   139  			"700e056463504690c11d63727bf25a380f303be9",
   140  			true,
   141  			"700e056463504690c11d63727bf25a380f303be9",
   142  		},
   143  	}
   144  	for _, test := range tests {
   145  		t.Run(test.desc, func(t *testing.T) {
   146  			test := test
   147  			t.Parallel()
   148  			b, err := GetCommit(4181224, test.ref)
   149  			if test.ok {
   150  				require.NoError(t, err)
   151  				require.Equal(t, test.expectID, b.ID)
   152  			} else {
   153  				require.Error(t, err)
   154  				require.Nil(t, b)
   155  			}
   156  		})
   157  	}
   158  }
   159  
   160  // copyTestRepo creates a copy of the testdata directory (contains a Git repo) in
   161  // the project root with a random dir name. It returns the absolute path of the
   162  // new testdata dir.
   163  // Note: testdata-* must be in the .gitignore or the copies will create write
   164  // errors as Git attempts to add the Git repo to the the project repo's index.
   165  func copyTestRepo() string {
   166  	dst, err := filepath.Abs(os.ExpandEnv("$GOPATH/src/github.com/zaquestion/lab/testdata-" + strconv.Itoa(int(rand.Uint64()))))
   167  	if err != nil {
   168  		log.Fatal(err)
   169  	}
   170  	src, err := filepath.Abs(os.ExpandEnv("$GOPATH/src/github.com/zaquestion/lab/testdata"))
   171  	if err != nil {
   172  		log.Fatal(err)
   173  	}
   174  	if err := copy.Copy(src, dst); err != nil {
   175  		log.Fatal(err)
   176  	}
   177  	// Move the test.git dir into the expected path at .git
   178  	if err := copy.Copy(dst+"/test.git", dst+"/.git"); err != nil {
   179  		log.Fatal(err)
   180  	}
   181  	return dst
   182  }