1717package executor
1818
1919import (
20+ context2 "context"
2021 "fmt"
22+ "github.com/devtron-labs/ci-runner/executor/context"
2123 "github.com/devtron-labs/ci-runner/helper"
2224 "io/ioutil"
2325 "os"
@@ -28,6 +30,8 @@ import (
2830
2931func TestRunScripts (t * testing.T ) {
3032 t .SkipNow ()
33+ commandExecutorImpl := helper .NewCommandExecutorImpl ()
34+ scriptExecutorImpl := NewScriptExecutorImpl (commandExecutorImpl )
3135 type args struct {
3236 workDirectory string
3337 scriptFileName string
@@ -78,7 +82,8 @@ func TestRunScripts(t *testing.T) {
7882 }
7983 for _ , tt := range tests {
8084 t .Run (tt .name , func (t * testing.T ) {
81- got , err := RunScripts (tt .args .workDirectory , tt .args .scriptFileName , tt .args .script , tt .args .envVars , tt .args .outputVars )
85+ ciCtx := context .BuildCiContext (context2 .Background (), false )
86+ got , err := scriptExecutorImpl .RunScripts (ciCtx , tt .args .workDirectory , tt .args .scriptFileName , tt .args .script , tt .args .envVars , tt .args .outputVars )
8287 if (err != nil ) != tt .wantErr {
8388 t .Errorf ("RunScripts() error = %v, wantErr %v" , err , tt .wantErr )
8489 return
@@ -192,6 +197,9 @@ func TestRunScriptsInDocker(t *testing.T) {
192197 type args struct {
193198 executionConf * executionConf
194199 }
200+ commandExecutorImpl := helper .NewCommandExecutorImpl ()
201+ scriptExecutorImpl := NewScriptExecutorImpl (commandExecutorImpl )
202+ stageExecutorImpl := NewStageExecutorImpl (commandExecutorImpl , scriptExecutorImpl )
195203 tests := []struct {
196204 name string
197205 args args
@@ -219,7 +227,8 @@ func TestRunScriptsInDocker(t *testing.T) {
219227 }
220228 for _ , tt := range tests {
221229 t .Run (tt .name , func (t * testing.T ) {
222- got , err := RunScriptsInDocker (tt .args .executionConf )
230+ ciCtx := context .BuildCiContext (context2 .Background (), false )
231+ got , err := RunScriptsInDocker (ciCtx , stageExecutorImpl , tt .args .executionConf )
223232 if (err != nil ) != tt .wantErr {
224233 t .Errorf ("RunScriptsInDocker() error = %v, wantErr %v" , err , tt .wantErr )
225234 return
@@ -300,3 +309,91 @@ func Test_writeToEnvFile(t *testing.T) {
300309 })
301310 }
302311}
312+
313+ func Test_writeToEnvFile1 (t * testing.T ) {
314+ type args struct {
315+ envMap map [string ]string
316+ filename string
317+ }
318+ tests := []struct {
319+ name string
320+ args args
321+ wantErr bool
322+ fileContents []string
323+ }{
324+ {
325+ name : "empty_env_map" ,
326+ args : args {envMap : map [string ]string {}, filename : "test1.env" },
327+ wantErr : false ,
328+ fileContents : []string {},
329+ },
330+ {
331+ name : "single_env_var" ,
332+ args : args {envMap : map [string ]string {"FOO" : "BAR" }, filename : "test2.env" },
333+ wantErr : false ,
334+ fileContents : []string {`FOO="BAR"` },
335+ },
336+ {
337+ name : "multiple_env_vars" ,
338+ args : args {envMap : map [string ]string {"FOO" : "BAR" , "BAR" : "FOO" }, filename : "test3.env" },
339+ wantErr : false ,
340+ fileContents : []string {`FOO="BAR"` , `BAR="FOO"` },
341+ },
342+ {
343+ name : "error_creating_file" ,
344+ args : args {envMap : map [string ]string {"FOO" : "BAR" }, filename : "/dev/null/abcd" },
345+ wantErr : true ,
346+ fileContents : []string {},
347+ },
348+ {
349+ name : "multiline_env_vars" ,
350+ args : args {envMap : map [string ]string {"FOO" : "BAR\n \" BAZ\" %%s\n \t \b " , "BAR" : "FOO" }, filename : "test4.env" },
351+ wantErr : false ,
352+ fileContents : []string {`FOO="BAR\n\"BAZ\"%%s\n\t\b"` , `BAR="FOO"` },
353+ },
354+ {
355+ name : "emoji_env_vars" ,
356+ args : args {envMap : map [string ]string {"FOO" : "BA👍R\n \" BAZ\" %%s\n \t \b " , "BAR" : "FOO" }, filename : "test5.env" },
357+ wantErr : false ,
358+ fileContents : []string {`FOO="BA👍R\n\"BAZ\"%%s\n\t\b"` , `BAR="FOO"` },
359+ },
360+ {
361+ name : "pos_integer_env_vars" ,
362+ args : args {envMap : map [string ]string {"FOO" : "1" , "BAR" : "2" }, filename : "test6.env" },
363+ wantErr : false ,
364+ fileContents : []string {`FOO=1` , `BAR=2` },
365+ },
366+ {
367+ name : "neg_integer_env_vars" ,
368+ args : args {envMap : map [string ]string {"FOO" : "-1" , "BAR" : "-2" }, filename : "test7.env" },
369+ wantErr : false ,
370+ fileContents : []string {`FOO=-1` , `BAR=-2` },
371+ },
372+ {
373+ name : "float_env_vars" ,
374+ args : args {envMap : map [string ]string {"FOO" : "1.0" , "BAR" : "2.0" }, filename : "test8.env" },
375+ wantErr : false ,
376+ fileContents : []string {`FOO="1.0"` , `BAR="2.0"` },
377+ },
378+ }
379+ for _ , tt := range tests {
380+ t .Run (tt .name , func (t * testing.T ) {
381+ if err := writeToEnvFile (tt .args .envMap , tt .args .filename ); (err != nil ) != tt .wantErr {
382+ t .Errorf ("writeToEnvFile() error = %v, wantErr %v" , err , tt .wantErr )
383+ }
384+ content , err := os .ReadFile (tt .args .filename )
385+ if tt .wantErr && err == nil {
386+ t .Errorf ("writeToEnvFile(); wanted error = %v; got nil" , err )
387+ } else if ! tt .wantErr && err != nil {
388+ t .Errorf ("writeToEnvFile(); wanted no error; got = %v" , err )
389+ }
390+ if err == nil {
391+ for _ , line := range tt .fileContents {
392+ if ! strings .Contains (string (content ), line ) {
393+ t .Errorf ("writeToEnvFile() not found = %v, content = %v" , line , string (content ))
394+ }
395+ }
396+ }
397+ })
398+ }
399+ }
0 commit comments