11package cmd_test
22
33import (
4+ "os"
5+ "path/filepath"
46 "testing"
57
68 "github.com/spf13/cobra"
@@ -12,62 +14,98 @@ import (
1214 fnTest "knative.dev/func/pkg/testing"
1315)
1416
15- func TestNewConfigCICmd_CommandExists (t * testing.T ) {
16- opts := ciOpts {withMockLoaderSaver : true , args : []string {"ci" , "--github" }}
17- cmd := configCIGithubCmd (t , opts )
17+ func TestNewConfigCICmd_CISubcommandAndGithubOptionExist (t * testing.T ) {
18+ // leave 'ci --github' to make this test explicitly use this subcommand
19+ opts := opts {withFuncInTempDir : true , args : []string {"ci" , "--github" }}
20+ cmd , _ := setupConfigCmd (t , opts )
1821
19- err := cmd .Execute ()
20-
21- assert .NilError (t , err )
22+ executeSuccess (t , cmd )
2223}
2324
2425func TestNewConfigCICmd_FailsWhenNotInitialized (t * testing.T ) {
2526 expectedErrMsg := fn .NewErrNotInitialized (fnTest .Cwd ()).Error ()
26- cmd := configCIGithubCmd (t , ciOpts {})
27+ cmd , _ := setupConfigCmd (t , opts {})
2728
2829 err := cmd .Execute ()
2930
3031 assert .Error (t , err , expectedErrMsg )
3132}
3233
3334func TestNewConfigCICmd_SuccessWhenInitialized (t * testing.T ) {
34- cmd := configCIGithubCmd (t , ciOpts {withFuncInTempDir : true })
35+ cmd , _ := setupConfigCmd (t , opts {withFuncInTempDir : true })
3536
36- err := cmd .Execute ()
37+ executeSuccess (t , cmd )
38+ }
39+
40+ func 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 )
45+
46+ _ , err := os .Stat (expectedWorkflowPath )
47+ assert .NilError (t , err )
48+ }
49+
50+ func 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 )
54+
55+ executeSuccess (t , cmd )
3756
57+ _ , err := os .Stat (expectedWorkflowPath )
58+ assert .NilError (t , err )
59+
60+ _ , err = os .Stat (expectedWorkflowFile )
3861 assert .NilError (t , err )
3962}
4063
4164// START: Testing Framework
4265// ------------------------
43- type ciOpts struct {
44- withMockLoaderSaver bool // default: false
45- withFuncInTempDir bool // default: false
46- args []string // default: ci --github
66+ type opts struct {
67+ withFuncInTempDir bool
68+ args []string // default: ci --github
69+ }
70+
71+ type testArtifacts struct {
72+ f fn.Function
73+ ciConfig fnCmd.CIConfig
4774}
4875
49- func configCIGithubCmd (
76+ func setupConfigCmd (
5077 t * testing.T ,
51- opts ciOpts ,
52- ) * cobra.Command {
78+ opts opts ,
79+ ) ( * cobra.Command , testArtifacts ) {
5380 t .Helper ()
5481
55- if opts .withFuncInTempDir {
56- _ = cmdTest .CreateFuncInTempDir (t , "github-ci-func" )
82+ ta := testArtifacts {
83+ fn.Function {},
84+ fnCmd .NewDefaultCIConfig (),
5785 }
5886
59- var loaderSaver common.FunctionLoaderSaver = common .DefaultLoaderSaver
60- if opts .withMockLoaderSaver {
61- loaderSaver = newMockLoaderSaver ()
87+ if opts .withFuncInTempDir {
88+ ta .f = cmdTest .CreateFuncInTempDir (t , "github-ci-func" )
6289 }
6390
6491 args := opts .args
6592 if len (opts .args ) == 0 {
6693 args = []string {"ci" , "--github" }
6794 }
6895
69- result := fnCmd .NewConfigCmd (loaderSaver , fnCmd .NewClient )
96+ result := fnCmd .NewConfigCmd (
97+ common .DefaultLoaderSaver ,
98+ fnCmd .NewClient ,
99+ ta .ciConfig ,
100+ )
70101 result .SetArgs (args )
71102
72- return result
103+ return result , ta
104+ }
105+
106+ func executeSuccess (t * testing.T , cmd * cobra.Command ) {
107+ t .Helper ()
108+
109+ err := cmd .Execute ()
110+ assert .NilError (t , err )
73111}
0 commit comments