11package main
22
33import (
4+ "io/ioutil"
45 "os"
56 "reflect"
67 "testing"
@@ -708,11 +709,32 @@ func TestGitLabCI(t *testing.T) {
708709 }
709710}
710711
712+ func createTempfile (content string ) (* os.File , error ) {
713+ tempfile , err := ioutil .TempFile ("" , "" )
714+ if err != nil {
715+ return tempfile , err
716+ }
717+
718+ _ , err = tempfile .Write ([]byte (content ))
719+ if err != nil {
720+ return tempfile , err
721+ }
722+
723+ err = tempfile .Close ()
724+ if err != nil {
725+ return tempfile , err
726+ }
727+
728+ return tempfile , nil
729+ }
730+
711731func TestGitHubActions (t * testing.T ) {
732+
712733 envs := []string {
713734 "GITHUB_SHA" ,
714735 "GITHUB_REPOSITORY" ,
715736 "GITHUB_RUN_ID" ,
737+ "GITHUB_EVENT_PATH" ,
716738 }
717739 saveEnvs := make (map [string ]string )
718740 for _ , key := range envs {
@@ -727,15 +749,40 @@ func TestGitHubActions(t *testing.T) {
727749
728750 // https://help.github.com/ja/actions/configuring-and-managing-workflows/using-environment-variables
729751 testCases := []struct {
730- fn func ()
752+ fn func () func ()
731753 ci CI
732754 ok bool
733755 }{
734756 {
735- fn : func () {
757+ fn : func () func () {
758+ os .Setenv ("GITHUB_SHA" , "abcdefg" )
759+ os .Setenv ("GITHUB_REPOSITORY" , "mercari/tfnotify" )
760+ os .Setenv ("GITHUB_RUN_ID" , "12345" )
761+ os .Setenv ("GITHUB_EVENT_PATH" , "" )
762+
763+ return func () {}
764+ },
765+ ci : CI {
766+ PR : PullRequest {
767+ Revision : "abcdefg" ,
768+ Number : 0 ,
769+ },
770+ URL : "https://github.com/mercari/tfnotify/actions/runs/12345" ,
771+ },
772+ ok : true ,
773+ },
774+ {
775+ fn : func () func () {
736776 os .Setenv ("GITHUB_SHA" , "abcdefg" )
737777 os .Setenv ("GITHUB_REPOSITORY" , "mercari/tfnotify" )
738778 os .Setenv ("GITHUB_RUN_ID" , "12345" )
779+
780+ tempfile , _ := createTempfile ("{}" )
781+ os .Setenv ("GITHUB_EVENT_PATH" , tempfile .Name ())
782+
783+ return func () {
784+ os .Remove (tempfile .Name ())
785+ }
739786 },
740787 ci : CI {
741788 PR : PullRequest {
@@ -746,10 +793,93 @@ func TestGitHubActions(t *testing.T) {
746793 },
747794 ok : true ,
748795 },
796+ {
797+ fn : func () func () {
798+ os .Setenv ("GITHUB_SHA" , "abcdefg" )
799+ os .Setenv ("GITHUB_REPOSITORY" , "mercari/tfnotify" )
800+ os .Setenv ("GITHUB_RUN_ID" , "12345" )
801+
802+ tempfile , _ := createTempfile (`
803+ {
804+ "issue": {
805+ "number": 123
806+ }
807+ }
808+ ` )
809+ os .Setenv ("GITHUB_EVENT_PATH" , tempfile .Name ())
810+
811+ return func () {
812+ os .Remove (tempfile .Name ())
813+ }
814+ },
815+ ci : CI {
816+ PR : PullRequest {
817+ Revision : "abcdefg" ,
818+ Number : 123 ,
819+ },
820+ URL : "https://github.com/mercari/tfnotify/actions/runs/12345" ,
821+ },
822+ ok : true ,
823+ },
824+ {
825+ fn : func () func () {
826+ os .Setenv ("GITHUB_SHA" , "abcdefg" )
827+ os .Setenv ("GITHUB_REPOSITORY" , "mercari/tfnotify" )
828+ os .Setenv ("GITHUB_RUN_ID" , "12345" )
829+
830+ tempfile , _ := createTempfile (`
831+ {
832+ "pull_request": {
833+ "number": 234
834+ }
835+ }
836+ ` )
837+ os .Setenv ("GITHUB_EVENT_PATH" , tempfile .Name ())
838+
839+ return func () {
840+ os .Remove (tempfile .Name ())
841+ }
842+ },
843+ ci : CI {
844+ PR : PullRequest {
845+ Revision : "abcdefg" ,
846+ Number : 234 ,
847+ },
848+ URL : "https://github.com/mercari/tfnotify/actions/runs/12345" ,
849+ },
850+ ok : true ,
851+ },
852+ {
853+ fn : func () func () {
854+ os .Setenv ("GITHUB_SHA" , "abcdefg" )
855+ os .Setenv ("GITHUB_REPOSITORY" , "mercari/tfnotify" )
856+ os .Setenv ("GITHUB_RUN_ID" , "12345" )
857+
858+ tempfile , _ := createTempfile (`
859+ {
860+ "number": 345
861+ }
862+ ` )
863+ os .Setenv ("GITHUB_EVENT_PATH" , tempfile .Name ())
864+
865+ return func () {
866+ os .Remove (tempfile .Name ())
867+ }
868+ },
869+ ci : CI {
870+ PR : PullRequest {
871+ Revision : "abcdefg" ,
872+ Number : 345 ,
873+ },
874+ URL : "https://github.com/mercari/tfnotify/actions/runs/12345" ,
875+ },
876+ ok : true ,
877+ },
749878 }
750879
751880 for _ , testCase := range testCases {
752- testCase .fn ()
881+ teardown := testCase .fn ()
882+ defer teardown ()
753883 ci , err := githubActions ()
754884 if ! reflect .DeepEqual (ci , testCase .ci ) {
755885 t .Errorf ("got %q but want %q" , ci , testCase .ci )
0 commit comments