gitlab.com/zaquestion/lab@v0.14.0/internal/gitlab/gitlab_test.go (about) 1 package gitlab 2 3 import ( 4 "log" 5 "os" 6 "testing" 7 8 "github.com/spf13/viper" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestMain(m *testing.M) { 13 err := os.Chdir(os.ExpandEnv("$GOPATH/src/github.com/zaquestion/lab/testdata")) 14 if err != nil { 15 log.Fatal(err) 16 } 17 18 viper.SetConfigName("lab") 19 viper.SetConfigType("hcl") 20 viper.AddConfigPath(".") 21 err = viper.ReadInConfig() 22 if err != nil { 23 log.Fatal(err) 24 } 25 c := viper.AllSettings()["core"] 26 config := c.([]map[string]interface{})[0] 27 28 Init( 29 config["host"].(string), 30 config["user"].(string), 31 config["token"].(string)) 32 os.Exit(m.Run()) 33 } 34 35 func TestLoadGitLabTmplMR(t *testing.T) { 36 mrTmpl := LoadGitLabTmpl(TmplMR) 37 require.Equal(t, mrTmpl, "I am the default merge request template for lab") 38 } 39 40 func TestLoadGitLabTmplIssue(t *testing.T) { 41 issueTmpl := LoadGitLabTmpl(TmplIssue) 42 require.Equal(t, issueTmpl, "This is the default issue template for lab") 43 } 44 45 func TestLint(t *testing.T) { 46 tests := []struct { 47 desc string 48 content string 49 expected bool 50 }{ 51 { 52 "Valid", 53 `build1: 54 stage: build 55 script: 56 - echo "Do your build here"`, 57 true, 58 }, 59 { 60 "Invalid", 61 `build1: 62 - echo "Do your build here"`, 63 false, 64 }, 65 { 66 "Empty", 67 ``, 68 false, 69 }, 70 } 71 for _, test := range tests { 72 t.Run(test.desc, func(t *testing.T) { 73 test := test 74 t.Parallel() 75 ok, _ := Lint(test.content) 76 require.Equal(t, test.expected, ok) 77 }) 78 } 79 } 80 81 func TestBranchPushed(t *testing.T) { 82 tests := []struct { 83 desc string 84 branch string 85 expected bool 86 }{ 87 { 88 "alpha is pushed", 89 "mrtest", 90 true, 91 }, 92 { 93 "needs encoding is pushed", 94 "needs/encode", 95 true, 96 }, 97 { 98 "alpha not pushed", 99 "not_a_branch", 100 false, 101 }, 102 } 103 for _, test := range tests { 104 t.Run(test.desc, func(t *testing.T) { 105 test := test 106 t.Parallel() 107 ok := BranchPushed(4181224, test.branch) 108 require.Equal(t, test.expected, ok) 109 }) 110 } 111 }