55 "path/filepath"
66 "testing"
77
8- "github.com/spf13/cobra "
8+ "gopkg.in/yaml.v3 "
99 "gotest.tools/v3/assert"
1010 fnCmd "knative.dev/func/cmd"
1111 "knative.dev/func/cmd/common"
@@ -17,48 +17,63 @@ import (
1717func TestNewConfigCICmd_CISubcommandAndGithubOptionExist (t * testing.T ) {
1818 // leave 'ci --github' to make this test explicitly use this subcommand
1919 opts := opts {withFuncInTempDir : true , args : []string {"ci" , "--github" }}
20- cmd , _ := setupConfigCmd (t , opts )
20+ result := runConfigCiGithubCmd (t , opts )
2121
22- executeSuccess (t , cmd )
22+ assert . NilError (t , result . err )
2323}
2424
2525func TestNewConfigCICmd_FailsWhenNotInitialized (t * testing.T ) {
2626 expectedErrMsg := fn .NewErrNotInitialized (fnTest .Cwd ()).Error ()
27- cmd , _ := setupConfigCmd (t , opts {})
2827
29- err := cmd . Execute ( )
28+ result := runConfigCiGithubCmd ( t , opts {} )
3029
31- assert .Error (t , err , expectedErrMsg )
30+ assert .Error (t , result . err , expectedErrMsg )
3231}
3332
3433func TestNewConfigCICmd_SuccessWhenInitialized (t * testing.T ) {
35- cmd , _ := setupConfigCmd (t , opts {withFuncInTempDir : true })
34+ result := runConfigCiGithubCmd (t , opts {withFuncInTempDir : true })
3635
37- executeSuccess (t , cmd )
36+ assert . NilError (t , result . err )
3837}
3938
4039func TestNewConfigCICmd_CreatesGithubWorkflowDirectory (t * testing.T ) {
41- cmd , ta := setupConfigCmd (t , opts {withFuncInTempDir : true })
42- expectedWorkflowPath := filepath .Join (ta .f .Root , ta .ciConfig .GithubWorkflowDir )
43-
44- executeSuccess (t , cmd )
40+ result := runConfigCiGithubCmd (t , opts {withFuncInTempDir : true })
41+ assert .NilError (t , result .err )
4542
43+ expectedWorkflowPath := filepath .Join (result .f .Root , result .ciConfig .GithubWorkflowDir )
4644 _ , err := os .Stat (expectedWorkflowPath )
4745 assert .NilError (t , err )
4846}
4947
5048func TestNewConfigCICmd_GeneratesLocalWorkflowFile (t * testing.T ) {
51- cmd , ta := setupConfigCmd (t , opts {withFuncInTempDir : true })
52- expectedWorkflowPath := filepath .Join (ta .f .Root , ta .ciConfig .GithubWorkflowDir )
53- expectedWorkflowFile := filepath .Join (expectedWorkflowPath , ta .ciConfig .GithubWorkflowFile )
49+ result := runConfigCiGithubCmd (t , opts {withFuncInTempDir : true })
50+ assert .NilError (t , result .err )
5451
55- executeSuccess (t , cmd )
52+ _ = assertWorkflowFileExists (t , result )
53+ }
5654
57- _ , err := os .Stat (expectedWorkflowPath )
58- assert .NilError (t , err )
55+ func TestNewConfigCICmd_WorkflowYAMLHasCorrectStructure (t * testing.T ) {
56+ result := runConfigCiGithubCmd (t , opts {withFuncInTempDir : true })
57+ assert .NilError (t , result .err )
58+
59+ workflowFilepath := assertWorkflowFileExists (t , result )
5960
60- _ , err = os .Stat (expectedWorkflowFile )
61+ var expectedWorkflow fnCmd.GithubWorkflow
62+ workflowAsBytes , err := os .ReadFile (workflowFilepath )
6163 assert .NilError (t , err )
64+ err = yaml .Unmarshal (workflowAsBytes , & expectedWorkflow )
65+ assert .NilError (t , err )
66+ assert .Equal (t , expectedWorkflow .Name , "Remote Build and Deploy" )
67+ assert .Equal (t , expectedWorkflow .On .Push .Branches [0 ], "main" )
68+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].RunsOn , "ubuntu-latest" )
69+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].Steps [0 ].Name , "Checkout code" )
70+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].Steps [0 ].Uses , "actions/checkout@v4" )
71+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].Steps [1 ].Name , "Install func cli" )
72+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].Steps [1 ].Uses , "gauron99/knative-func-action@main" )
73+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].Steps [1 ].With ["version" ], "knative-v1.19.1" )
74+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].Steps [1 ].With ["name" ], "func" )
75+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].Steps [2 ].Name , "Deploy function" )
76+ assert .Equal (t , expectedWorkflow .Jobs ["deploy" ].Steps [2 ].Run , "func deploy --remote" )
6277}
6378
6479// START: Testing Framework
@@ -68,44 +83,51 @@ type opts struct {
6883 args []string // default: ci --github
6984}
7085
71- type testArtifacts struct {
86+ type result struct {
7287 f fn.Function
7388 ciConfig fnCmd.CIConfig
89+ err error
7490}
7591
76- func setupConfigCmd (
92+ func runConfigCiGithubCmd (
7793 t * testing.T ,
7894 opts opts ,
79- ) ( * cobra. Command , testArtifacts ) {
95+ ) result {
8096 t .Helper ()
8197
82- ta := testArtifacts {
83- fn.Function {},
84- fnCmd .NewDefaultCIConfig (),
85- }
86-
98+ f := fn.Function {}
8799 if opts .withFuncInTempDir {
88- ta . f = cmdTest .CreateFuncInTempDir (t , "github-ci-func" )
100+ f = cmdTest .CreateFuncInTempDir (t , "github-ci-func" )
89101 }
90102
91103 args := opts .args
92104 if len (opts .args ) == 0 {
93105 args = []string {"ci" , "--github" }
94106 }
95107
96- result := fnCmd .NewConfigCmd (
108+ ciConfig := fnCmd .NewDefaultCIConfig ()
109+ cmd := fnCmd .NewConfigCmd (
97110 common .DefaultLoaderSaver ,
98111 fnCmd .NewClient ,
99- ta . ciConfig ,
112+ ciConfig ,
100113 )
101- result .SetArgs (args )
114+ cmd .SetArgs (args )
115+
116+ err := cmd .Execute ()
102117
103- return result , ta
118+ return result {
119+ f ,
120+ ciConfig ,
121+ err ,
122+ }
104123}
105124
106- func executeSuccess (t * testing.T , cmd * cobra. Command ) {
125+ func assertWorkflowFileExists (t * testing.T , result result ) string {
107126 t .Helper ()
127+ filepath := filepath .Join (result .f .Root , result .ciConfig .GithubWorkflowDir , result .ciConfig .GithubWorkflowFile )
128+ exists , _ := fnTest .FileExists (t , filepath )
108129
109- err := cmd .Execute ()
110- assert .NilError (t , err )
130+ assert .Assert (t , exists , filepath + " does not exist" )
131+
132+ return filepath
111133}
0 commit comments