@@ -26,77 +26,120 @@ func TestNewKoolDeploy(t *testing.T) {
2626 }
2727}
2828
29- func fakeKoolDeploy () * KoolDeploy {
29+ func fakeKoolDeploy (pwd string ) * KoolDeploy {
3030 c := NewCloud ()
3131 c .Fake ()
3232 return & KoolDeploy {
3333 * (newDefaultKoolService ().Fake ()),
3434 c ,
35- setup .NewDefaultCloudSetupParser ("" ),
35+ setup .NewDefaultCloudSetupParser (pwd ),
3636 & KoolCloudDeployFlags {},
3737 environment .NewFakeEnvStorage (),
3838 nil ,
3939 }
4040}
4141
42- func TestHandleDeployEnv (t * testing.T ) {
43- fake := fakeKoolDeploy ()
42+ func TestCreateReleaseFileNoConfig (t * testing.T ) {
43+ fake := fakeKoolDeploy ("" )
4444
45- files := []string {}
45+ if _ , err := fake .createReleaseFile (); err == nil || ! strings .Contains (err .Error (), "no kool.cloud.yml config files found" ) {
46+ t .Errorf ("expected error on createReleaseFile when no kool.deploy.yml exists in current working directory; got: %v" , err )
47+ }
48+ }
4649
50+ func TestCreateReleaseFileCreatesTgz (t * testing.T ) {
4751 tmpDir := t .TempDir ()
52+ mockConfig (tmpDir , t , nil )
53+
54+ fake := fakeKoolDeploy (tmpDir )
4855 fake .env .Set ("PWD" , tmpDir )
4956
50- files = fake .handleDeployEnv (files )
57+ if err := fake .loadAndValidateConfig (); err != nil {
58+ t .Errorf ("unexpected error on loadAndValidateConfig; got: %v" , err )
59+ }
5160
52- if len (files ) != 0 {
53- t .Errorf ("expected files to continue empty - no kool.deploy.env exists" )
61+ if tg , err := fake .createReleaseFile (); err != nil {
62+ t .Errorf ("unexpected error on createReleaseFile; got: %v" , err )
63+ } else if _ , err := os .Stat (tg ); err != nil {
64+ t .Errorf ("expected tgz file to be created; got: %v" , err )
5465 }
66+ }
67+
68+ func TestCreateReleaseFileCreatesTgzWithEnvFile (t * testing.T ) {
69+ tmpDir := t .TempDir ()
70+ mockConfig (tmpDir , t , []byte ("services:\n foo:\n image: bar\n env:\n source: 'foo.env'\n " ))
5571
56- if err := os .WriteFile (filepath .Join (tmpDir , "kool.deploy .env" ), []byte ("FOO=BAR" ), os .ModePerm ); err != nil {
72+ if err := os .WriteFile (filepath .Join (tmpDir , "foo .env" ), []byte ("FOO=BAR" ), os .ModePerm ); err != nil {
5773 t .Fatal (err )
5874 }
5975
60- files = fake .handleDeployEnv (files )
76+ fake := fakeKoolDeploy (tmpDir )
77+ fake .env .Set ("PWD" , tmpDir )
6178
62- if len ( files ) != 1 {
63- t .Errorf ("expected files to have added kool.deploy.env" )
79+ if err := fake . loadAndValidateConfig (); err != nil {
80+ t .Errorf ("unexpected error on loadAndValidateConfig; got: %v" , err )
6481 }
6582
66- files = fake .handleDeployEnv (files )
83+ if tg , err := fake .createReleaseFile (); err != nil {
84+ t .Errorf ("unexpected error on createReleaseFile; got: %v" , err )
85+ } else if _ , err := os .Stat (tg ); err != nil {
86+ t .Errorf ("expected tgz file to be created; got: %v" , err )
87+ }
6788
68- if len (files ) != 1 {
69- t .Errorf ("expected files to continue since was already there kool.deploy.env" )
89+ if ! fake .shell .(* shell.FakeShell ).CalledPrintln {
90+ t .Error ("expected Println to have been called on shell" )
91+ } else if ! strings .Contains (fake .shell .(* shell.FakeShell ).OutLines [0 ], "Compressing files:" ) {
92+ t .Error ("expected to print 'Compressing files:'" )
93+ } else if ! strings .Contains (fake .shell .(* shell.FakeShell ).OutLines [1 ], "- " + filepath .Join (tmpDir , "kool.cloud.yml" )) {
94+ t .Error ("expected to print '- " + filepath .Join (tmpDir , "kool.cloud.yml" ) + "'" )
95+ } else if ! strings .Contains (fake .shell .(* shell.FakeShell ).OutLines [2 ], "- " + filepath .Join (tmpDir , "docker-compose.yml" )) {
96+ t .Error ("expected to print '- " + filepath .Join (tmpDir , "docker-compose.yml" ) + "'" )
97+ } else if ! strings .Contains (fake .shell .(* shell.FakeShell ).OutLines [3 ], "- " + filepath .Join (tmpDir , "foo.env" )) {
98+ t .Error ("expected to print '- " + filepath .Join (tmpDir , "foo.env" ) + "'" )
7099 }
71100}
72101
73- func TestCreateReleaseFile (t * testing.T ) {
74- fake := fakeKoolDeploy ()
75-
102+ func TestCreateReleaseFileCreatesTgzWithEnvironmentFile (t * testing.T ) {
76103 tmpDir := t .TempDir ()
77- fake . env . Set ( "PWD" , tmpDir )
104+ mockConfig ( tmpDir , t , [] byte ( "services: \n foo: \n image: bar \n environment: 'bar.env' \n " ) )
78105
79- if _ , err := fake . createReleaseFile (); err == nil || ! strings . Contains ( err . Error ( ), "no kool.cloud.yml config files found" ) {
80- t .Errorf ( "expected error on createReleaseFile when no kool.deploy.yml exists in current working directory; got: %v" , err )
106+ if err := os . WriteFile ( filepath . Join ( tmpDir , "bar.env" ), [] byte ( "BAR=FOO" ), os . ModePerm ); err != nil {
107+ t .Fatal ( err )
81108 }
82109
83- mockConfig (tmpDir , t , nil )
110+ fake := fakeKoolDeploy (tmpDir )
111+ fake .env .Set ("PWD" , tmpDir )
112+
113+ if err := fake .loadAndValidateConfig (); err != nil {
114+ t .Errorf ("unexpected error on loadAndValidateConfig; got: %v" , err )
115+ }
84116
85117 if tg , err := fake .createReleaseFile (); err != nil {
86118 t .Errorf ("unexpected error on createReleaseFile; got: %v" , err )
87119 } else if _ , err := os .Stat (tg ); err != nil {
88120 t .Errorf ("expected tgz file to be created; got: %v" , err )
89121 }
122+
123+ if ! fake .shell .(* shell.FakeShell ).CalledPrintln {
124+ t .Error ("expected Println to have been called on shell" )
125+ } else if ! strings .Contains (fake .shell .(* shell.FakeShell ).OutLines [0 ], "Compressing files:" ) {
126+ t .Error ("expected to print 'Compressing files:'" )
127+ } else if ! strings .Contains (fake .shell .(* shell.FakeShell ).OutLines [1 ], "- " + filepath .Join (tmpDir , "kool.cloud.yml" )) {
128+ t .Error ("expected to print '- " + filepath .Join (tmpDir , "kool.cloud.yml" ) + "'" )
129+ } else if ! strings .Contains (fake .shell .(* shell.FakeShell ).OutLines [2 ], "- " + filepath .Join (tmpDir , "docker-compose.yml" )) {
130+ t .Error ("expected to print '- " + filepath .Join (tmpDir , "docker-compose.yml" ) + "'" )
131+ } else if ! strings .Contains (fake .shell .(* shell.FakeShell ).OutLines [3 ], "- " + filepath .Join (tmpDir , "bar.env" )) {
132+ t .Error ("expected to print '- " + filepath .Join (tmpDir , "bar.env" ) + "'" )
133+ }
90134}
91135
92136func TestCleanupReleaseFile (t * testing.T ) {
93- fake := fakeKoolDeploy ()
94-
95137 tmpDir := t .TempDir ()
96- fake .env .Set ("PWD" , tmpDir )
97-
98138 mockConfig (tmpDir , t , nil )
99139
140+ fake := fakeKoolDeploy ("" )
141+ fake .env .Set ("PWD" , tmpDir )
142+
100143 f := filepath .Join (tmpDir , "kool.cloud.yml" )
101144 fake .cleanupReleaseFile (f )
102145 if _ , err := os .Stat (f ); ! os .IsNotExist (err ) {
@@ -113,7 +156,7 @@ func TestCleanupReleaseFile(t *testing.T) {
113156}
114157
115158func TestLoadAndValidateConfig (t * testing.T ) {
116- fake := fakeKoolDeploy ()
159+ fake := fakeKoolDeploy ("" )
117160
118161 tmpDir := t .TempDir ()
119162 fake .env .Set ("PWD" , tmpDir )
0 commit comments