@@ -6,6 +6,90 @@ import (
66 "testing"
77)
88
9+ func TestInitGitHubSettings (t * testing.T ) {
10+ t .Run ("github action pull request" , func (t * testing.T ) {
11+ // Set this vars if running locally, othersie just use the ones from the workflow
12+ if _ , ok := os .LookupEnv ("GITHUB_ACTIONS" ); ! ok {
13+ _ = os .Setenv ("GITHUB_ACTIONS" , "true" )
14+ defer os .Unsetenv ("GITHUB_ACTIONS" )
15+ _ = os .Setenv ("GITHUB_TOKEN" , "foo" )
16+ defer os .Unsetenv ("GITHUB_TOKEN" )
17+ _ = os .Setenv ("GITHUB_REPOSITORY" , "coolapso/convcommitlint" )
18+ defer os .Unsetenv ("GITHUB_REPOSITORY" )
19+ defer func (){ repository = "" }()
20+ _ = os .Setenv ("GITHUB_REF_NAME" , "25/merge" )
21+ defer os .Unsetenv ("GITHUB_REF_NAME" )
22+ }
23+
24+ var tempType string
25+ if v , ok := os .LookupEnv ("GITHUB_EVENT_NAME" ); ok {
26+ if v != "pull_request" {
27+ tempType = v
28+ os .Setenv ("GITHUB_EVENT_NAME" , "pull_request" )
29+ defer os .Setenv ("GITHUB_EVENT_NAME" , tempType )
30+ }
31+ } else {
32+ _ = os .Setenv ("GITHUB_EVENT_NAME" , "pull_request" )
33+ defer os .Unsetenv ("GITHUB_EVENT_NAME" )
34+ }
35+
36+ got := initGHSettings ()
37+ if got != nil {
38+ t .Fatal ("Expected nil, got:" , got )
39+ }
40+ })
41+
42+ t .Run ("github action push" , func (t * testing.T ) {
43+ if _ , ok := os .LookupEnv ("GITHUB_ACTIONS" ); ! ok {
44+ _ = os .Setenv ("GITHUB_ACTIONS" , "true" )
45+ defer os .Unsetenv ("GITHUB_ACTIONS" )
46+ _ = os .Setenv ("GITHUB_TOKEN" , "foo" )
47+ defer os .Unsetenv ("GITHUB_REPOSITORY" )
48+ _ = os .Setenv ("GITHUB_REPOSITORY" , "coolapso/convcommitlint" )
49+ defer os .Unsetenv ("GITHUB_TOKEN" )
50+ defer func (){ repository = "" }()
51+ }
52+
53+ var eventTemp string
54+ if v , ok := os .LookupEnv ("GITHUB_EVENT_NAME" ); ok {
55+ eventTemp = v
56+ os .Setenv ("GITHUB_EVENT_NAME" , "push" )
57+ defer os .Setenv ("GITHUB_EVENT_NAME" , eventTemp )
58+ } else {
59+ os .Setenv ("GITHUB_EVENT_NAME" , "push" )
60+ defer os .Setenv ("GITHUB_EVENT_NAME" , eventTemp )
61+ }
62+
63+ got := initGHSettings ()
64+ if got != nil {
65+ t .Fatal ("Expected nil, got:" , got )
66+ }
67+ })
68+
69+ t .Run ("missing github repository" , func (t * testing.T ) {
70+ got := initGHSettings ()
71+ if got != errMissingRepository {
72+ t .Fatalf ("Expected [ %v ] error, got [ %v ]" , errMissingRepository , got )
73+ }
74+ })
75+
76+
77+ t .Run ("Run locally" , func (t * testing.T ) {
78+ if _ , ok := os .LookupEnv ("GITHUB_ACTIONS" ); ! ok {
79+ _ = os .Setenv ("GITHUB_TOKEN" , "foo" )
80+ defer os .Unsetenv ("GITHUB_TOKEN" )
81+ _ = os .Setenv ("GITHUB_REPOSITORY" , "coolapso/convcommitlint" )
82+ defer os .Unsetenv ("GITHUB_REPOSITORY" )
83+ func (){ repository = "" }()
84+ }
85+ prNumber = 25
86+ got := initGHSettings ()
87+ if got != nil {
88+ t .Fatal ("Expected nil, got:" , got )
89+ }
90+ })
91+ }
92+
993func TestGetGHToken (t * testing.T ) {
1094 t .Run ("missing token" , func (t * testing.T ) {
1195 want := errMissingGHToken
@@ -132,7 +216,20 @@ func TestSplitOwnerRepo(t *testing.T) {
132216
133217func TestPullRequest (t * testing.T ) {
134218 t .Run ("Is pull request" , func (t * testing.T ) {
135- _ = os .Setenv ("GITHUB_EVENT_TYPE" , "pull_request" )
219+ if _ , ok := os .LookupEnv ("GITHUB_ACTIONS" ); ! ok {
220+ _ = os .Setenv ("GITHUB_EVENT_NAME" , "pull_request" )
221+ defer os .Unsetenv ("GITHUB_EVENT_NAME" )
222+ }
223+
224+ var eventTemp string
225+ if _ , ok := os .LookupEnv ("GITHUB_ACTIONS" ); ok {
226+ if v := os .Getenv ("GITHUB_EVENT_NAME" ); v != "pull_request" {
227+ eventTemp = v
228+ os .Setenv ("GITHUB_EVENT_NAME" , "pull_request" )
229+ defer func (){ os .Setenv ("GITHUB_EVENT_NAME" , eventTemp ) }()
230+ }
231+ }
232+
136233 want := true
137234 got := pullRequest ()
138235 if got != want {
@@ -141,7 +238,7 @@ func TestPullRequest(t *testing.T) {
141238 })
142239
143240 t .Run ("Is not pull request" , func (t * testing.T ) {
144- _ = os .Setenv ("GITHUB_EVENT_TYPE " , "push" )
241+ _ = os .Setenv ("GITHUB_EVENT_NAME " , "push" )
145242 want := false
146243 got := pullRequest ()
147244 if got != want {
0 commit comments